mrDarker
2025-06-20 7f55a7c6cef156e553866d0012464e4697cb1849
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include "stdafx.h"
#include "UserManager.h"
#include <chrono>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
 
const std::string SESSION_FILE = R"(session.dat)";
const std::string DATABASE_FILE = R"(UserManager.db)";
 
const std::string INITIAL_ADMIN_USERNAME = "admin";
const std::string INITIAL_ADMIN_PASSWORD = "admin";
 
// »ñÈ¡µ¥ÀýʵÀý
UserManager& UserManager::getInstance() {
    static UserManager instance;
    return instance;
}
 
UserManager::UserManager()
    : m_isLoggedIn(false), m_isRememberMe(false), m_tmSessionTimeout(std::chrono::minutes(30)),
    m_tmSessionExpiration(std::chrono::hours(72)), m_hMouseHook(nullptr), m_hKeyboardHook(nullptr),
    m_pDB(std::make_unique<BL::SQLiteDatabase>()) {
    initializeDatabase();
}
 
UserManager::~UserManager() {
    terminateIdleDetection();
}
 
// ÌṩÊý¾Ý¿âÁ¬½Ó
std::unique_ptr<BL::Database>& UserManager::getDatabaseInstance() {
    return m_pDB;
}
 
// ³õʼ»¯Êý¾Ý¿â£¬´´½¨Óû§±í²¢²åÈë³õʼ¹ÜÀíÔ±Óû§
bool UserManager::initializeDatabase() {
    std::string dbFilePath = getDatabaseFilePath();
    if (!m_pDB->connect(dbFilePath, true)) {
        throw std::runtime_error("Failed to connect to database.");
    }
 
    std::string createTableQuery = R"(
        CREATE TABLE IF NOT EXISTS users (
            username VARCHAR(50) PRIMARY KEY,
            password VARCHAR(255) NOT NULL,
            role INT NOT NULL,
            session_timeout INT DEFAULT 30,
            session_expiration INT DEFAULT 72,
            last_login DATETIME DEFAULT (datetime('now', 'localtime'))
        )
    )";
    m_pDB->executeQuery(createTableQuery);
 
    std::string checkAdminQuery = "SELECT COUNT(*) FROM users WHERE role = 0";
    auto result = m_pDB->fetchResults(checkAdminQuery);
 
    if (result.empty() || result[0][0] == "0") {
        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);
    }
 
    return true;
}
 
// ¶ÔÃÜÂë½øÐйþÏ£´¦Àí
std::string UserManager::hashPassword(const std::string& password) {
    return std::to_string(std::hash<std::string>{}(password));
}
 
// ¼òµ¥µÄ¼ÓÃܺͽâÃܺ¯Êý
std::string UserManager::simpleEncryptDecrypt(const std::string& data, const std::string& key) {
    std::string result = data;
    for (size_t i = 0; i < data.size(); ++i) {
        result[i] ^= key[i % key.size()];  // ¼òµ¥Òì»ò¼ÓÃÜ
    }
    return result;
}
 
// ´Ó»á»°Îļþ¼ÓÔØ»á»°ÐÅÏ¢
bool UserManager::loadSession() {
    std::ifstream sessionFile(getSessionFilePath(), std::ios::binary);
    if (!sessionFile.is_open()) {
        return false;
    }
 
    // ´ÓÎļþ¶ÁÈ¡¼ÓÃÜÊý¾Ý
    std::string encryptedData((std::istreambuf_iterator<char>(sessionFile)), std::istreambuf_iterator<char>());
    sessionFile.close();
 
    // ½âÃÜÊý¾Ý
    std::string decryptedData = simpleEncryptDecrypt(encryptedData, "my_secret_key");
 
    // ½âÎö½âÃܵÄÊý¾Ý
    std::istringstream sessionData(decryptedData);
    std::string username;
    std::string password;
    std::time_t lastLoginTime;
    int timeoutMinutes;
    int expirationHours;
 
    sessionData >> username >> password >> lastLoginTime >> timeoutMinutes >> expirationHours;
 
    // Ñé֤ʱ¼ä´ÁÓÐЧÐÔ
    auto now = std::chrono::system_clock::now();
    auto lastLogin = std::chrono::system_clock::from_time_t(lastLoginTime);
    auto sessionDuration = std::chrono::duration_cast<std::chrono::hours>(now - lastLogin);
 
    if (sessionDuration > std::chrono::hours(expirationHours)) {
        clearSession();
        return false;
    }
 
    // »Ö¸´»á»°Êý¾Ý
    m_strCurrentUser = username;
    m_strCurrentPass = password;
    m_tpLastLogin = lastLogin;
    m_tmSessionTimeout = std::chrono::minutes(timeoutMinutes);
    m_tmSessionExpiration = std::chrono::hours(expirationHours);
    m_isLoggedIn = true;
    m_isRememberMe = true;
    updateActivityTime();
 
    return true;
}
 
// ±£´æ»á»°ÐÅÏ¢µ½Îļþ
void UserManager::saveSession() {
    if (!m_isRememberMe) {
        clearSession();
        return;
    }
 
    // Ô­Ê¼»á»°Êý¾Ý
    std::stringstream sessionData;
    std::time_t lastLoginTime = std::chrono::system_clock::to_time_t(m_tpLastLogin);
    sessionData << m_strCurrentUser << " " << m_strCurrentPass << " " << lastLoginTime << " "
        << m_tmSessionTimeout.count() << " " << m_tmSessionExpiration.count();
 
    // ¼ÓÃÜÊý¾Ý
    std::string encryptedData = simpleEncryptDecrypt(sessionData.str(), "my_secret_key");
 
    // Ð´Èë¼ÓÃÜÊý¾Ýµ½Îļþ
    std::ofstream sessionFile(getSessionFilePath(), std::ios::binary);
    if (sessionFile.is_open()) {
        sessionFile << encryptedData;
        sessionFile.close();
    }
}
 
// Çå³ý»á»°Îļþ
void UserManager::clearSession() {
    std::remove(getSessionFilePath().c_str());
}
 
// »ñÈ¡³ÌÐò·¾¶ÏµÄconfigÎļþ¼Ð·¾¶
std::string UserManager::getConfigFolderPath() {
    char szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, MAX_PATH);
    std::string exePath(szPath);
    std::string dbDir = exePath.substr(0, exePath.find_last_of("\\/")) + "\\DB\\";
 
    // ¼ì²é²¢´´½¨configÎļþ¼Ð
    DWORD fileAttr = GetFileAttributes(dbDir.c_str());
    if (fileAttr == INVALID_FILE_ATTRIBUTES) {
        CreateDirectory(dbDir.c_str(), NULL);
    }
 
    return dbDir;
}
 
// »ñÈ¡session.datÎļþ·¾¶
std::string UserManager::getSessionFilePath() {
    return getConfigFolderPath() + SESSION_FILE;
}
 
// »ñÈ¡Êý¾Ý¿âÎļþ·¾¶
std::string UserManager::getDatabaseFilePath() {
    return getConfigFolderPath() + DATABASE_FILE;
}
 
// µÇ¼·½·¨
bool UserManager::login(const std::string& username, const std::string& password, bool rememberMeFlag) {
    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] != simpleEncryptDecrypt(password, "BandKey")) {
        std::cerr << "Login failed: Invalid username or password." << std::endl;
        return false;
    }
 
    m_strCurrentUser = username;
    m_strCurrentPass = password;
    m_enCurrentUserRole = static_cast<UserRole>(std::stoi(result[0][2]));
    m_tmSessionTimeout = std::chrono::minutes(std::stoi(result[0][3]));
    m_tmSessionExpiration = std::chrono::hours(std::stoi(result[0][4]));
    m_isLoggedIn = true;
    m_isRememberMe = rememberMeFlag;
    updateActivityTime();
    m_tpLastLogin = std::chrono::system_clock::now();
 
    std::string updateLoginTime = "UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE username = '" + username + "'";
    m_pDB->executeQuery(updateLoginTime);
 
    saveSession();
    return true;
}
 
// µÇ³ö·½·¨
void UserManager::logout() {
    if (m_isLoggedIn) {
        std::cout << "User logged out: " << m_strCurrentUser << std::endl;
        m_strCurrentUser.clear();
        m_strCurrentPass.clear();
        m_isLoggedIn = false;
        m_isRememberMe = false;
        clearSession();
    }
}
 
// ·µ»Øµ±Ç°Óû§µÄµÇ¼״̬
bool UserManager::isLoggedIn() const {
    return m_isLoggedIn;
}
 
// ·µ»Øµ±Ç°Óû§µÄ¼ÇסµÇ¼״̬
bool UserManager::isRememberMe() const {
    return m_isRememberMe;
}
 
// ´´½¨ÐÂÓû§£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::createUser(const std::string& username, const std::string& password, UserRole role,
    std::chrono::minutes timeout, std::chrono::hours expiration) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can create new users." << std::endl;
        return false;
    }
 
    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);
}
 
// É¾³ýÓû§£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ£¬ÇÒ²»ÄÜɾ³ý×Ô¼º
bool UserManager::deleteUser(const std::string& username) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can delete users." << std::endl;
        return false;
    }
    if (username == m_strCurrentUser) {
        std::cerr << "SuperAdmin cannot delete their own account." << std::endl;
        return false;
    }
 
    std::string query = "DELETE FROM users WHERE username = '" + username + "'";
    return m_pDB->executeQuery(query);
}
 
// »ñÈ¡ËùÓÐÓû§ÐÅÏ¢£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
std::vector<std::vector<std::string>> UserManager::getUsers() {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can retrieve user data." << std::endl;
        return {};
    }
 
    // ²éѯÕû¸öÓû§±í
    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;
}
 
// ÉèÖÃÕû¸öÓû§±íµÄÊý¾Ý£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::setUsers(const std::vector<std::vector<std::string>>& usersData) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can set user data." << std::endl;
        return false;
    }
 
    // Çå¿ÕÓû§±í
    std::string deleteQuery = "DELETE FROM users";
    if (!m_pDB->executeQuery(deleteQuery)) {
        std::cerr << "Failed to clear the users table." << std::endl;
        return false;
    }
 
    // ²åÈëеÄÓû§Êý¾Ý
    for (const auto& user : usersData) {
        if (user.size() != 6) {
            std::cerr << "Invalid data format for user. Each user must have 6 fields." << std::endl;
            return false;
        }
 
        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;
            return false;
        }
    }
 
    return true;
}
 
// ÐÞ¸ÄÓû§Ãû£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::changeUsername(const std::string& username, const std::string& newUsername) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can change usernames." << std::endl;
        return false;
    }
 
    std::string query = "UPDATE users SET username = '" + newUsername + "' WHERE username = '" + username + "'";
    bool success = m_pDB->executeQuery(query);
 
    // Èç¹ûÊǵ±Ç°µÇ¼Óû§ÐÞ¸Ä×Ô¼ºµÄÓû§Ãû£¬¸üгÉÔ±±äÁ¿²¢±£´æ»á»°Îļþ
    if (success && m_strCurrentUser == username) {
        m_strCurrentUser = newUsername;
 
        // Èç¹û¡°¼ÇסÃÜÂ롱ÒÑÆôÓ㬸üлỰÎļþ
        if (m_isRememberMe) {
            saveSession();
        }
    }
    return success;
}
 
// ÐÞ¸ÄÓû§ÃÜÂ루½öÔÊÐíµ±Ç°Óû§»ò³¬¼¶¹ÜÀíÔ±£©
bool UserManager::changePassword(const std::string& username, const std::string& newPassword) {
    if (username != m_strCurrentUser && m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Permission denied: Only the user or SuperAdmin can change passwords." << std::endl;
        return false;
    }
 
    std::string query = "UPDATE users SET password = '" + simpleEncryptDecrypt(newPassword, "BandKey") +
        "' WHERE username = '" + username + "'";
    bool success = m_pDB->executeQuery(query);
 
    // Èç¹ûÊǵ±Ç°Óû§ÐÞ¸Ä×Ô¼ºµÄÃÜÂ룬Í˳öµÇ¼²¢Çå³ý»á»°Îļþ
    if (success && m_strCurrentUser == username) {
        logout();
        std::cout << "Password changed successfully. Please log in again." << std::endl;
    }
 
    return success;
}
 
// ¸ü¸ÄÓû§½ÇÉ«£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::changeUserRole(const std::string& username, UserRole newRole) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can change user roles." << std::endl;
        return false;
    }
 
    // ·ÀÖ¹¹ÜÀíÔ±¸ü¸Ä×Ô¼ºµÄ½ÇÉ«
    if (m_strCurrentUser == username) {
        std::cerr << "SuperAdmin cannot change their own role." << std::endl;
        return false;
    }
 
    std::string query = "UPDATE users SET role = " + std::to_string(static_cast<int>(newRole)) +
        " WHERE username = '" + username + "'";
    return m_pDB->executeQuery(query);
}
 
// ÐÞ¸ÄÓû§µÄ session_timeout£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::changeUserSessionTimeout(const std::string& username, int newTimeoutMinutes) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can change session timeout." << std::endl;
        return false;
    }
 
    std::string query = "UPDATE users SET session_timeout = " + std::to_string(newTimeoutMinutes) +
        " WHERE username = '" + username + "'";
    bool success = m_pDB->executeQuery(query);
    // Èç¹ûÊǵ±Ç°µÇ¼Óû§ÐÞ¸Ä×Ô¼ºµÄ³¬Ê±ÉèÖ㬸üгÉÔ±±äÁ¿
    if (success && m_strCurrentUser == username) {
        m_tmSessionTimeout = std::chrono::minutes(newTimeoutMinutes);
 
        if (m_isRememberMe) {
            saveSession();
        }
    }
    return success;
}
 
// ÐÞ¸ÄÓû§µÄ session_expiration£¬½ö³¬¼¶¹ÜÀíÔ±ÓÐȨÏÞ
bool UserManager::changeUserSessionExpiration(const std::string& username, int newExpirationHours) {
    if (m_enCurrentUserRole != UserRole::SuperAdmin) {
        std::cerr << "Only SuperAdmin can change session expiration." << std::endl;
        return false;
    }
 
    std::string query = "UPDATE users SET session_expiration = " + std::to_string(newExpirationHours) +
        " WHERE username = '" + username + "'";
    bool success = m_pDB->executeQuery(query);
    // Èç¹ûÊǵ±Ç°µÇ¼Óû§ÐÞ¸Ä×Ô¼ºµÄ¹ýÆÚÉèÖ㬸üгÉÔ±±äÁ¿
    if (success && m_strCurrentUser == username) {
        m_tmSessionExpiration = std::chrono::hours(newExpirationHours);
 
        if (m_isRememberMe) {
            saveSession();
        }
    }
    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();
    std::cout << "Activity updated at: " << std::chrono::system_clock::to_time_t(m_tpLastActivity) << std::endl;
}
 
// ÉèÖÃÎÞ²Ù×÷³¬Ê±Ê±¼ä
void UserManager::setSessionTimeout(std::chrono::minutes timeout) {
    m_tmSessionTimeout = timeout;
}
 
// ¼ì²éÊÇ·ñ³¬¹ýÎÞ²Ù×÷³¬Ê±Ê±¼ä
bool UserManager::isInactiveTimeout() const {
    auto now = std::chrono::system_clock::now();
    auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>(now - m_tpLastActivity).count();
    return elapsedSeconds > m_tmSessionTimeout.count() * 60;
}
 
// ³õʼ»¯ÎÞ²Ù×÷¼ì²â£¬°üÀ¨ÉèÖÃÈ«¾ÖÊó±êºÍ¼üÅ̹³×Ó
void UserManager::initializeIdleDetection(HWND hwnd) {
    updateActivityTime();
    m_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, (HINSTANCE) nullptr, 0);
    m_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, (HINSTANCE) nullptr, 0);
    ::SetTimer(hwnd, 1, 60000, nullptr);
}
 
// ÖÕÖ¹ÎÞ²Ù×÷¼ì²â£¬Çå³ýÊó±êºÍ¼üÅ̹³×Ó
void UserManager::terminateIdleDetection() {
    if (m_hMouseHook) {
        UnhookWindowsHookEx(m_hMouseHook);
        m_hMouseHook = nullptr;
    }
    if (m_hKeyboardHook) {
        UnhookWindowsHookEx(m_hKeyboardHook);
        m_hKeyboardHook = nullptr;
    }
    ::KillTimer(nullptr, 1);
}
 
// »ñÈ¡µ±Ç°µÇ¼Óû§Ãû
std::string UserManager::getCurrentUser() const {
    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;
}
 
// »ñÈ¡µ±Ç°µÇ¼Óû§µÄ»á»°¹ýÆÚʱ¼ä
std::chrono::hours UserManager::getSessionExpiration() const {
    return m_tmSessionExpiration;
}
 
// È«¾ÖÊó±ê¹³×ӻص÷£¬¼Ç¼»î¶¯Ê±¼ä
LRESULT CALLBACK UserManager::LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        UserManager::getInstance().updateActivityTime();
        std::cout << "Mouse event detected. Activity time updated." << std::endl;
    }
    return CallNextHookEx(nullptr, nCode, wParam, lParam);
}
 
// È«¾Ö¼üÅ̹³×ӻص÷£¬¼Ç¼»î¶¯Ê±¼ä
LRESULT CALLBACK UserManager::LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        UserManager::getInstance().updateActivityTime();
        std::cout << "Keyboard event detected. Activity time updated." << std::endl;
    }
    return CallNextHookEx(nullptr, nCode, wParam, lParam);
}