From 8dbd14952aa622587a92866dc01943869ea4f9dc Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期三, 20 十一月 2024 11:28:02 +0800
Subject: [PATCH] 1. 添加系统运行记录表 2. 完善用户管理的部分细节

---
 SourceCode/Bond/BondEq/DBManager/UserManager.cpp |   49 +++++++++++++++++++++++++++++++++++++------------
 1 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/SourceCode/Bond/BondEq/DBManager/UserManager.cpp b/SourceCode/Bond/BondEq/DBManager/UserManager.cpp
index c58f219..bd3f4df 100644
--- a/SourceCode/Bond/BondEq/DBManager/UserManager.cpp
+++ b/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,7 +44,7 @@
     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,
@@ -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);
 
@@ -445,16 +455,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;

--
Gitblit v1.9.3