using Bootstrap.Security; using Longbow.Cache; using Longbow.Data; 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 RetrieveUsers() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key => DbAdapterManager.Create().RetrieveUsers()); /// /// /// /// /// /// public static bool Authenticate(string userName, string password) => DbAdapterManager.Create().Authenticate(userName, password); /// /// 查询所有的新注册用户 /// /// public static IEnumerable RetrieveNewUsers() => CacheManager.GetOrAdd(RetrieveNewUsersDataKey, key => DbAdapterManager.Create().RetrieveNewUsers()); /// /// 删除用户 /// /// public static bool DeleteUser(IEnumerable value) { var ret = DbAdapterManager.Create().DeleteUser(value); if (ret) CacheCleanUtility.ClearCache(userIds: value); return ret; } /// /// 保存新建 /// /// /// public static bool SaveUser(User p) { var ret = DbAdapterManager.Create().SaveUser(p); if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); return ret; } /// /// /// /// /// /// /// public static bool UpdateUser(string id, string password, string displayName) { var ret = DbAdapterManager.Create().UpdateUser(id, password, displayName); if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(id) ? new List() : new List() { id }); return ret; } /// /// /// /// /// /// public static bool ApproveUser(string id, string approvedBy) { var ret = DbAdapterManager.Create().ApproveUser(id, approvedBy); if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// /// /// /// /// /// /// public static bool ChangePassword(string userName, string password, string newPass) => DbAdapterManager.Create().ChangePassword(userName, password, newPass); /// /// /// /// /// /// /// public static bool RejectUser(string id, string rejectBy) { var ret = DbAdapterManager.Create().RejectUser(id, rejectBy); if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// /// 通过roleId获取所有用户 /// /// /// public static IEnumerable RetrieveUsersByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByRoleIdDataKey, roleId), k => DbAdapterManager.Create().RetrieveUsersByRoleId(roleId), RetrieveUsersByRoleIdDataKey); /// /// 通过角色ID保存当前授权用户(插入) /// /// 角色ID /// 用户ID数组 /// public static bool SaveUsersByRoleId(string roleId, IEnumerable userIds) { var ret = DbAdapterManager.Create().SaveUsersByRoleId(roleId, userIds); if (ret) CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List() { roleId }); return ret; } /// /// 通过groupId获取所有用户 /// /// /// public static IEnumerable RetrieveUsersByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByGroupIdDataKey, groupId), k => DbAdapterManager.Create().RetrieveUsersByGroupId(groupId), RetrieveUsersByRoleIdDataKey); /// /// 通过部门ID保存当前授权用户(插入) /// /// GroupID /// 用户ID数组 /// public static bool SaveUsersByGroupId(string groupId, IEnumerable userIds) { var ret = DbAdapterManager.Create().SaveUsersByGroupId(groupId, userIds); if (ret) CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List() { groupId }); return ret; } /// 根据用户名修改用户头像 /// /// /// /// public static bool SaveUserIconByName(string userName, string iconName) { var ret = DbAdapterManager.Create().SaveUserIconByName(userName, iconName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// /// /// /// /// public static bool SaveDisplayName(string userName, string displayName) { var ret = DbAdapterManager.Create().SaveDisplayName(userName, displayName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); return ret; } /// /// 根据用户名更改用户皮肤 /// /// /// /// public static bool SaveUserCssByName(string userName, string cssName) { var ret = DbAdapterManager.Create().SaveUserCssByName(userName, cssName); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*"); return ret; } /// /// /// /// /// public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbAdapterManager.Create().RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey); } }