using Bootstrap.Security; using Longbow.Cache; using Longbow.Data; using System; using System.Collections.Generic; namespace Bootstrap.DataAccess { /// /// 用户表相关操作类 /// public static class UserHelper { public const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers"; public const string RetrieveUsersByRoleIdDataKey = "UserHelper-RetrieveUsersByRoleId"; public const string RetrieveUsersByGroupIdDataKey = "UserHelper-RetrieveUsersByGroupId"; public const string RetrieveNewUsersDataKey = "UserHelper-RetrieveNewUsers"; public const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName"; /// /// 查询所有用户 /// /// /// public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key => DbContextManager.Create().Retrieves()); /// /// /// /// /// /// public static bool Authenticate(string userName, string password, Action config) { var loginUser = new LoginUser() { UserName = userName, LoginTime = DateTime.Now, Result = "登录失败" }; config(loginUser); var ret = DbContextManager.Create().Authenticate(userName, password); if (ret) loginUser.Result = "登录成功"; LoginHelper.Log(loginUser); return ret; } /// /// 查询所有的新注册用户 /// /// public static IEnumerable RetrieveNewUsers() => CacheManager.GetOrAdd(RetrieveNewUsersDataKey, key => DbContextManager.Create().RetrieveNewUsers()); /// /// 删除用户 /// /// public static bool Delete(IEnumerable value) { var ret = DbContextManager.Create().Delete(value); if (ret) CacheCleanUtility.ClearCache(userIds: value); return ret; } /// /// 保存用户默认App /// /// /// /// public static bool SaveApp(string userName, string app) { var ret = DbContextManager.Create().SaveApp(userName, app); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// 保存新建 /// /// /// public static bool Save(User p) { var ret = DbContextManager.Create().Save(p); if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); return ret; } /// /// /// /// /// /// /// public static bool Update(string id, string password, string displayName) { var ret = DbContextManager.Create().Update(id, password, displayName); if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(id) ? new List() : new List() { id }); return ret; } /// /// /// /// /// /// public static bool Approve(string id, string approvedBy) { var ret = DbContextManager.Create().Approve(id, approvedBy); if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// /// /// /// /// /// /// public static bool ChangePassword(string userName, string password, string newPass) => DbContextManager.Create().ChangePassword(userName, password, newPass); /// /// /// /// /// /// public static bool ResetPassword(string userName, string password) => DbContextManager.Create().ResetPassword(userName, password); /// /// /// /// public static bool ForgotPassword(ResetUser user) => DbContextManager.Create().ForgotPassword(user); /// /// /// /// /// /// /// public static bool Reject(string id, string rejectBy) { var ret = DbContextManager.Create().Reject(id, rejectBy); if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// /// 通过roleId获取所有用户 /// /// /// public static IEnumerable RetrievesByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByRoleIdDataKey, roleId), k => DbContextManager.Create().RetrievesByRoleId(roleId), RetrieveUsersByRoleIdDataKey); /// /// 通过角色ID保存当前授权用户(插入) /// /// 角色ID /// 用户ID数组 /// public static bool SaveByRoleId(string roleId, IEnumerable userIds) { var ret = DbContextManager.Create().SaveByRoleId(roleId, userIds); if (ret) CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List() { roleId }); return ret; } /// /// 通过groupId获取所有用户 /// /// /// public static IEnumerable RetrievesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByGroupIdDataKey, groupId), k => DbContextManager.Create().RetrievesByGroupId(groupId), RetrieveUsersByRoleIdDataKey); /// /// 通过部门ID保存当前授权用户(插入) /// /// GroupID /// 用户ID数组 /// public static bool SaveByGroupId(string groupId, IEnumerable userIds) { var ret = DbContextManager.Create().SaveByGroupId(groupId, userIds); if (ret) CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List() { groupId }); return ret; } /// /// 根据用户名修改用户头像 /// /// /// /// public static bool SaveUserIconByName(string userName, string iconName) { var ret = DbContextManager.Create().SaveUserIconByName(userName, iconName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// /// /// /// /// public static bool SaveDisplayName(string userName, string displayName) { var ret = DbContextManager.Create().SaveDisplayName(userName, displayName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// 根据用户名更改用户皮肤 /// /// /// /// public static bool SaveUserCssByName(string userName, string cssName) { var ret = DbContextManager.Create().SaveUserCssByName(userName, cssName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// /// /// /// public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbContextManager.Create().RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey); /// /// 通过登录账号获得用户信息 /// /// /// public static ResetUser RetrieveResetUserByUserName(string userName) => DbContextManager.Create().RetrieveUserByUserName(userName); /// /// 通过登录账户获得重置密码原因 /// /// /// public static IEnumerable> RetrieveResetReasonsByUserName(string userName) => DbContextManager.Create().RetrieveResetReasonsByUserName(userName); } }