LAPTOP-SNT8I5JK\Boounion
2024-11-13 c36c5060e2ecdbea56dc72bf576b4b19fca29ffa
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
#ifndef USER_MANAGER_H
#define USER_MANAGER_H
 
#include <string>
#include <memory>
#include <chrono>
#include <windows.h>
#include "Database.h"
 
// Óû§½ÇÉ«¶¨Òå
enum class UserRole {
    SuperAdmin = 0,     // ³¬¼¶¹ÜÀíÔ±
    Engineer,           // ¹¤³Ìʦ
    Operator            // ²Ù×÷Ô±
};
 
// Óû§¹ÜÀíÀ࣬²ÉÓõ¥Àýģʽ
class UserManager {
public:
    static UserManager& getInstance();
 
    UserManager(const UserManager&) = delete;
    UserManager& operator=(const UserManager&) = delete;
 
    // Óû§²Ù×÷
    bool login(const std::string& username, const std::string& password, bool rememberMe = false);
    void logout();
    bool isLoggedIn() const;
    bool isRememberMe() const;
    bool createUser(const std::string& username, const std::string& password, UserRole role,
        std::chrono::minutes timeout = std::chrono::minutes(30),
        std::chrono::hours expiration = std::chrono::hours(72));
    bool deleteUser(const std::string& username);
    std::vector<std::vector<std::string>> getUsers();
    bool setUsers(const std::vector<std::vector<std::string>>& usersData);
    bool changeUsername(const std::string& username, const std::string& newUsername);
    bool changePassword(const std::string& username, const std::string& newPassword);
    bool changeUserRole(const std::string& username, UserRole newRole);
    bool changeUserSessionTimeout(const std::string& username, int newTimeoutMinutes);
    bool changeUserSessionExpiration(const std::string& username, int newExpirationHours);
 
    // »á»°Îļþ²Ù×÷
    bool loadSession();     // ´Ó»á»°Îļþ¼ÓÔØ»á»°ÐÅÏ¢
    void saveSession();     // ±£´æ»á»°ÐÅÏ¢µ½Îļþ
    void clearSession();    // Çå³ý»á»°Îļþ
 
    // ÅäÖÃÎļþ¼Ð·¾¶¹ÜÀí
    static std::string getConfigFolderPath();
    static std::string getSessionFilePath();
    static std::string getDatabaseFilePath();
 
    // ¸üÐÂ×îºó»î¶¯Ê±¼ä£¨ÓÃÓÚÎÞ²Ù×÷³¬Ê±¼ì²â£©
    void updateActivityTime();
 
    // ÉèÖÃÓû§µÄÎÞ²Ù×÷³¬Ê±Ê±¼ä
    void setSessionTimeout(std::chrono::minutes timeout);
 
    // ¼ì²éÊÇ·ñÎÞ²Ù×÷³¬Ê±
    bool isInactiveTimeout() const;
 
    // ³õʼ»¯ÎÞ²Ù×÷¼ì²â£¨ÉèÖÃÈ«¾Ö¹³×ӺͶ¨Ê±Æ÷£©
    void initializeIdleDetection(HWND hwnd);
 
    // ÖÕÖ¹ÎÞ²Ù×÷¼ì²â£¨Çå³ý¹³×ӺͶ¨Ê±Æ÷£©
    void terminateIdleDetection();
 
    // »ñÈ¡µ±Ç°µÇ¼Óû§Ãû
    std::string getCurrentUser() const;
 
    // »ñÈ¡µ±Ç°µÇ¼Óû§ÃÜÂë
    std::string getCurrentPass() const;
 
    // »ñÈ¡µ±Ç°µÇ¼Óû§½ÇÉ«
    UserRole getCurrentUserRole() const;
 
    // »ñÈ¡µ±Ç°µÇ¼Óû§µÄÎÞ²Ù×÷³¬Ê±Ê±¼ä
    std::chrono::minutes getSessionTimeout() const;
 
    // »ñÈ¡µ±Ç°µÇ¼Óû§µÄ»á»°¹ýÆÚʱ¼ä
    std::chrono::hours getSessionExpiration() const;
 
private:
    UserManager();
    ~UserManager();
 
    // ³õʼ»¯Êý¾Ý¿âÁ¬½ÓºÍÓû§±í
    bool initializeDatabase();
 
    // ¹þÏ£ÃÜÂ룬ÓÃÓÚ¼ÓÃÜÓû§ÃÜÂë
    std::string hashPassword(const std::string& password);
 
    // ¼ÓÃܺͽâÃܺ¯Êý
    std::string simpleEncryptDecrypt(const std::string& data, const std::string& key);
 
    // ¼üÅ̺ÍÊó±ê¹³×Óº¯Êý
    static LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
 
    // ÊôÐÔ¶¨Òå
    std::string m_strCurrentUser;                     // µ±Ç°µÇ¼Óû§Ãû
    std::string m_strCurrentPass;                     // µ±Ç°µÇ¼ÃÜÂë
    UserRole m_enCurrentUserRole;                     // µ±Ç°µÇ¼Óû§½ÇÉ«
    bool m_isLoggedIn;                                // ÊÇ·ñÒѵǼ
    bool m_isRememberMe;                              // ÊÇ·ñ¼ÇסµÇ¼״̬
 
    std::chrono::time_point<std::chrono::system_clock> m_tpLastLogin;     // ÉϴεǼʱ¼ä
    std::chrono::time_point<std::chrono::system_clock> m_tpLastActivity;  // ×îºó»î¶¯Ê±¼ä
    std::chrono::minutes m_tmSessionTimeout;          // ÎÞ²Ù×÷³¬Ê±Ê±¼ä
    std::chrono::hours m_tmSessionExpiration;         // »á»°¹ýÆÚʱ¼ä
    HHOOK m_hMouseHook;                               // Êó±ê¹³×Ó¾ä±ú
    HHOOK m_hKeyboardHook;                            // ¼üÅ̹³×Ó¾ä±ú
 
    std::unique_ptr<BL::Database> m_pDB;              // Êý¾Ý¿â½Ó¿Ú
};
 
#endif // USER_MANAGER_H