#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, // 超级管理员:系统最高权限,管理所有用户和权限
|
ProcessEngineer, // 制程工程师:负责工艺制定与优化
|
EquipmentEngineer,// 设备工程师:负责设备维护与技术支持
|
Operator, // 操作员:执行日常生产操作
|
Unknown // 未知角色:默认或未识别的角色
|
};
|
|
// 用户管理类,采用单例模式
|
class UserManager {
|
public:
|
static UserManager& getInstance();
|
|
UserManager(const UserManager&) = delete;
|
UserManager& operator=(const UserManager&) = delete;
|
|
// 提供数据库连接
|
std::unique_ptr<BL::Database>& getDatabaseInstance();
|
|
// 用户操作
|
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);
|
std::vector<std::string> getUsernames();
|
std::vector<std::string> getUserInfo(const std::string& username);
|
|
// 会话文件操作
|
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;
|
|
// 修改当前登录用户名
|
void setCurrentUser(const std::string& strName);
|
|
// 获取当前登录用户密码
|
std::string getCurrentPass() const;
|
|
// 修改当前登录用户密码
|
void setCurrentPass(const std::string& strPass);
|
|
// 获取当前登录用户角色
|
UserRole getCurrentUserRole() const;
|
|
// 修改当前登录用户角色
|
void setCurrentUserRole(UserRole emRole);
|
|
// 获取当前登录用户的无操作超时时间
|
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
|