From 6eacf60891a6ac09f96985af042df3c4ed3da000 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期五, 15 十一月 2024 18:00:22 +0800
Subject: [PATCH] 1. 修改控件库的字符集 2. 添加用户管理界面 3.修改密码的加密方式(对称加密)

---
 SourceCode/Bond/BondEq/DBManager/UserManager.cpp |   27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/SourceCode/Bond/BondEq/DBManager/UserManager.cpp b/SourceCode/Bond/BondEq/DBManager/UserManager.cpp
index c58f219..126a7dd 100644
--- a/SourceCode/Bond/BondEq/DBManager/UserManager.cpp
+++ b/SourceCode/Bond/BondEq/DBManager/UserManager.cpp
@@ -39,7 +39,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 +52,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 +178,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 +233,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 +262,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,7 +292,7 @@
             return false;
         }
 
-        std::string insertQuery = "INSERT INTO users (username, password_hash, role, session_timeout, session_expiration, last_login) VALUES ('" +
+        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] + "')";
 
         if (!m_pDB->executeQuery(insertQuery)) {
@@ -328,7 +333,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);
 

--
Gitblit v1.9.3