You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

#include "auth_manager.h"
#include <string>
#include <utility>
#include <unordered_map>
AuthManager::AuthManager() {
// 初始化预设的教师账户信息,包括小学、初中、高中各三个账号
accounts_ = {
{"张三1", {"123", "primary"}},
{"张三2", {"123", "primary"}},
{"张三3", {"123", "primary"}},
{"李四1", {"123", "junior"}},
{"李四2", {"123", "junior"}},
{"李四3", {"123", "junior"}},
{"王五1", {"123", "senior"}},
{"王五2", {"123", "senior"}},
{"王五3", {"123", "senior"}}
};
}
bool AuthManager::Authenticate(const std::string& username,
const std::string& password,
std::string* user_type) {
// 验证输入不包含非法字符
if (username.find(' ') != std::string::npos ||
password.find(' ') != std::string::npos) {
return false;
}
auto it = accounts_.find(username);
if (it != accounts_.end() && it->second.first == password) {
*user_type = it->second.second;
return true;
}
return false;
}
bool AuthManager::IsValidUser(const std::string& username,
const std::string& password) {
std::string user_type;
return Authenticate(username, password, &user_type);
}