chenluhua1980
2026-01-19 44360bc2cdeee16be72f9cc4bfb42e0ac26b5b44
SourceCode/Bond/BondEq/DBManager/UserManager.cpp
@@ -29,6 +29,11 @@
    terminateIdleDetection();
}
// 提供数据库连接
std::unique_ptr<BL::Database>& UserManager::getDatabaseInstance() {
    return m_pDB;
}
// 初始化数据库,创建用户表并插入初始管理员用户
bool UserManager::initializeDatabase() {
    std::string dbFilePath = getDatabaseFilePath();
@@ -39,11 +44,11 @@
    std::string createTableQuery = R"(
        CREATE TABLE IF NOT EXISTS users (
            username VARCHAR(50) PRIMARY KEY,
            password_hash VARCHAR(255) NOT NULL,
            password VARCHAR(255) NOT NULL,
            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);
@@ -52,8 +57,8 @@
    auto result = m_pDB->fetchResults(checkAdminQuery);
    if (result.empty() || result[0][0] == "0") {
        std::string insertAdminQuery = "INSERT INTO users (username, password_hash, role, session_timeout, session_expiration) VALUES ('" +
            INITIAL_ADMIN_USERNAME + "', '" + hashPassword(INITIAL_ADMIN_PASSWORD) + "', 0, 30, 72)";
        std::string insertAdminQuery = "INSERT INTO users (username, password, role, session_timeout, session_expiration) VALUES ('" +
            INITIAL_ADMIN_USERNAME + "', '" + simpleEncryptDecrypt(INITIAL_ADMIN_PASSWORD, "BandKey") + "', 0, 30, 72)";
        m_pDB->executeQuery(insertAdminQuery);
    }
@@ -178,10 +183,10 @@
// 登录方法
bool UserManager::login(const std::string& username, const std::string& password, bool rememberMeFlag) {
    std::string query = "SELECT username, password_hash, role, session_timeout, session_expiration FROM users WHERE username = '" + username + "'";
    std::string query = "SELECT username, password, role, session_timeout, session_expiration FROM users WHERE username = '" + username + "'";
    auto result = m_pDB->fetchResults(query);
    if (result.empty() || result[0][1] != hashPassword(password)) {
    if (result.empty() || result[0][1] != simpleEncryptDecrypt(password, "BandKey")) {
        std::cerr << "Login failed: Invalid username or password." << std::endl;
        return false;
    }
@@ -233,8 +238,8 @@
        return false;
    }
    std::string query = "INSERT INTO users (username, password_hash, role, session_timeout, session_expiration) VALUES ('" +
        username + "', '" + hashPassword(password) + "', " + std::to_string(static_cast<int>(role)) + ", " +
    std::string query = "INSERT INTO users (username, password, role, session_timeout, session_expiration) VALUES ('" +
        username + "', '" + simpleEncryptDecrypt(password, "BandKey") + "', " + std::to_string(static_cast<int>(role)) + ", " +
        std::to_string(timeout.count()) + ", " + std::to_string(expiration.count()) + ")";
    return m_pDB->executeQuery(query);
}
@@ -262,8 +267,13 @@
    }
    // 查询整个用户表
    std::string query = "SELECT username, password_hash, role, session_timeout, session_expiration, last_login FROM users";
    return m_pDB->fetchResults(query);
    std::string query = "SELECT username, password, role, session_timeout, session_expiration, last_login FROM users";
    std::vector<std::vector<std::string>> results = m_pDB->fetchResults(query);
    for (auto& row : results) {
        row[1] = simpleEncryptDecrypt(row[1], "BandKey");
    }
    return results;
}
// 设置整个用户表的数据,仅超级管理员有权限
@@ -287,8 +297,8 @@
            return false;
        }
        std::string insertQuery = "INSERT INTO users (username, password_hash, role, session_timeout, session_expiration, last_login) VALUES ('" +
            user[0] + "', '" + user[1] + "', " + user[2] + ", " + user[3] + ", " + user[4] + ", '" + user[5] + "')";
        std::string insertQuery = "INSERT INTO users (username, password, role, session_timeout, session_expiration, last_login) VALUES ('" +
            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;
@@ -328,7 +338,7 @@
        return false;
    }
    std::string query = "UPDATE users SET password_hash = '" + hashPassword(newPassword) +
    std::string query = "UPDATE users SET password = '" + simpleEncryptDecrypt(newPassword, "BandKey") +
        "' WHERE username = '" + username + "'";
    bool success = m_pDB->executeQuery(query);
@@ -401,6 +411,39 @@
    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();
@@ -445,16 +488,31 @@
   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;