#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
|