143 lines
3.7 KiB
C#
143 lines
3.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 用户数据库结构
|
|||
|
|
/// 支持版本管理和用户集合
|
|||
|
|
/// </summary>
|
|||
|
|
[System.Serializable]
|
|||
|
|
public class UserDatabase
|
|||
|
|
{
|
|||
|
|
public string Version = "1.0";
|
|||
|
|
public List<UserInfo> Users = new List<UserInfo>();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 查找活跃用户
|
|||
|
|
/// 用户名区分大小写
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="username">用户名</param>
|
|||
|
|
/// <returns>用户信息,如果不存在或不活跃则返回null</returns>
|
|||
|
|
public UserInfo FindUser(string username)
|
|||
|
|
{
|
|||
|
|
// 修改为区分大小写的比较
|
|||
|
|
return Users.Find(u => u.Username == username && u.IsActive);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取所有活跃用户
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>活跃用户列表</returns>
|
|||
|
|
public List<UserInfo> GetActiveUsers()
|
|||
|
|
{
|
|||
|
|
return Users.FindAll(u => u.IsActive);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取管理员用户列表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>管理员用户列表</returns>
|
|||
|
|
public List<UserInfo> GetAdminUsers()
|
|||
|
|
{
|
|||
|
|
return Users.FindAll(u => u.IsActive && u.Role == UserRole.Admin);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 扩展的用户信息类
|
|||
|
|
/// 包含完整的用户数据和时间戳
|
|||
|
|
/// </summary>
|
|||
|
|
[System.Serializable]
|
|||
|
|
public class UserInfo
|
|||
|
|
{
|
|||
|
|
public string Username;
|
|||
|
|
public string Password;
|
|||
|
|
public UserRole Role;
|
|||
|
|
public DateTime CreatedTime;
|
|||
|
|
public DateTime LastLoginTime;
|
|||
|
|
public DateTime PasswordChangedTime;
|
|||
|
|
public bool IsActive = true;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取用户显示名称
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>包含角色信息的显示名称</returns>
|
|||
|
|
public string GetDisplayName()
|
|||
|
|
{
|
|||
|
|
string displayName = Username;
|
|||
|
|
if (Role == UserRole.Admin)
|
|||
|
|
{
|
|||
|
|
displayName += " [管理员]";
|
|||
|
|
}
|
|||
|
|
return displayName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查密码是否需要更新(例如:超过指定天数)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="maxDays">最大天数</param>
|
|||
|
|
/// <returns>是否需要更新</returns>
|
|||
|
|
public bool IsPasswordExpired(int maxDays = 90)
|
|||
|
|
{
|
|||
|
|
if (PasswordChangedTime == default(DateTime))
|
|||
|
|
{
|
|||
|
|
// 如果从未修改过密码,使用创建时间
|
|||
|
|
return (DateTime.Now - CreatedTime).Days > maxDays;
|
|||
|
|
}
|
|||
|
|
return (DateTime.Now - PasswordChangedTime).Days > maxDays;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 用户操作日志事件
|
|||
|
|
/// 记录用户的各种操作行为
|
|||
|
|
/// </summary>
|
|||
|
|
[System.Serializable]
|
|||
|
|
public class UserLogEvent
|
|||
|
|
{
|
|||
|
|
public int Id;
|
|||
|
|
public string Username;
|
|||
|
|
public string Operation; // 操作类型:登录、登出、修改密码、创建用户等
|
|||
|
|
public string OperationDetails; // 操作详情
|
|||
|
|
public DateTime Timestamp;
|
|||
|
|
public string IPAddress; // IP地址(如果需要)
|
|||
|
|
public bool IsSuccessful; // 操作是否成功
|
|||
|
|
|
|||
|
|
public UserLogEvent()
|
|||
|
|
{
|
|||
|
|
Timestamp = DateTime.Now;
|
|||
|
|
IsSuccessful = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public UserLogEvent(string username, string operation, string details = null, bool successful = true)
|
|||
|
|
{
|
|||
|
|
Username = username;
|
|||
|
|
Operation = operation;
|
|||
|
|
OperationDetails = details;
|
|||
|
|
IsSuccessful = successful;
|
|||
|
|
Timestamp = DateTime.Now;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 系统使用日志事件(保持向后兼容)
|
|||
|
|
/// </summary>
|
|||
|
|
[System.Serializable]
|
|||
|
|
public class UseLogEvent
|
|||
|
|
{
|
|||
|
|
public int Id;
|
|||
|
|
public string OperationContentText;
|
|||
|
|
public DateTime Time;
|
|||
|
|
public string Account;
|
|||
|
|
|
|||
|
|
public UseLogEvent()
|
|||
|
|
{
|
|||
|
|
Time = DateTime.Now;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public UseLogEvent(string account, string operation)
|
|||
|
|
{
|
|||
|
|
Account = account;
|
|||
|
|
OperationContentText = operation;
|
|||
|
|
Time = DateTime.Now;
|
|||
|
|
}
|
|||
|
|
}
|