| | |
| | | terminateIdleDetection(); |
| | | } |
| | | |
| | | // 提供数据库连接 |
| | | std::unique_ptr<BL::Database>& UserManager::getDatabaseInstance() { |
| | | return m_pDB; |
| | | } |
| | | |
| | | // 初始化数据库,创建用户表并插入初始管理员用户 |
| | | bool UserManager::initializeDatabase() { |
| | | std::string dbFilePath = getDatabaseFilePath(); |
| | |
| | | role INT NOT NULL, |
| | | session_timeout INT DEFAULT 30, |
| | | session_expiration INT DEFAULT 72, |
| | | last_login TIMESTAMP |
| | | last_login DATETIME DEFAULT (datetime('now', 'localtime')) |
| | | ) |
| | | )"; |
| | | m_pDB->executeQuery(createTableQuery); |
| | |
| | | } |
| | | |
| | | std::string insertQuery = "INSERT INTO users (username, password, role, session_timeout, session_expiration, last_login) VALUES ('" + |
| | | user[0] + "', '" + user[1] + "', " + user[2] + ", " + user[3] + ", " + user[4] + ", '" + user[5] + "')"; |
| | | user[0] + "', '" + simpleEncryptDecrypt(user[1], "BandKey") + "', " + user[2] + ", " + user[3] + ", " + user[4] + ", '" + user[5] + "')"; |
| | | |
| | | if (!m_pDB->executeQuery(insertQuery)) { |
| | | std::cerr << "Failed to insert user: " << user[0] << std::endl; |
| | |
| | | return success; |
| | | } |
| | | |
| | | // 获取所有用户名称 |
| | | std::vector<std::string> UserManager::getUsernames() { |
| | | std::vector<std::string> usernames; |
| | | std::string query = "SELECT username FROM users"; |
| | | auto results = m_pDB->fetchResults(query); |
| | | |
| | | for (const auto& row : results) { |
| | | if (!row.empty()) { |
| | | usernames.push_back(row[0]); // 获取用户名列的值 |
| | | } |
| | | } |
| | | |
| | | return usernames; |
| | | } |
| | | |
| | | // 获取指定用户名的用户信息 |
| | | std::vector<std::string> UserManager::getUserInfo(const std::string& username) |
| | | { |
| | | // 构建查询语句 |
| | | std::ostringstream query; |
| | | query << "SELECT username, password, role, session_timeout, session_expiration, last_login " |
| | | << "FROM users WHERE username = '" << username << "'"; |
| | | |
| | | // 执行查询并获取结果 |
| | | auto results = m_pDB->fetchResults(query.str()); |
| | | if (results.empty()) { |
| | | return {}; |
| | | } |
| | | |
| | | // 返回查询到的第一行数据 |
| | | return results[0]; |
| | | } |
| | | |
| | | // 更新最后活动时间,用于无操作超时检测 |
| | | void UserManager::updateActivityTime() { |
| | | m_tpLastActivity = std::chrono::system_clock::now(); |
| | |
| | | return m_strCurrentUser; |
| | | } |
| | | |
| | | // 修改当前登录用户名 |
| | | void UserManager::setCurrentUser(const std::string& strName) { |
| | | m_strCurrentUser = strName; |
| | | } |
| | | |
| | | // 获取当前登录用户密码 |
| | | std::string UserManager::getCurrentPass() const { |
| | | return m_strCurrentPass; |
| | | } |
| | | |
| | | // 修改当前登录用户密码 |
| | | void UserManager::setCurrentPass(const std::string& strPass) { |
| | | m_strCurrentPass = strPass; |
| | | } |
| | | |
| | | // 获取当前登录用户角色 |
| | | UserRole UserManager::getCurrentUserRole() const { |
| | | return m_enCurrentUserRole; |
| | | } |
| | | |
| | | // 修改当前登录用户角色 |
| | | void UserManager::setCurrentUserRole(UserRole emRole) { |
| | | m_enCurrentUserRole = emRole; |
| | | } |
| | | |
| | | // 获取当前登录用户的无操作超时时间 |
| | | std::chrono::minutes UserManager::getSessionTimeout() const { |
| | | return m_tmSessionTimeout; |