using Bootstrap.Security;
using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
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";
private static bool UserChecker(User user)
{
if (user.Description?.Length > 500) user.Description = user.Description.Substring(0, 500);
if (user.UserName?.Length > 16) user.UserName = user.UserName.Substring(0, 16);
if (user.Password?.Length > 16) user.Password = user.Password.Substring(0, 16);
if (user.DisplayName?.Length > 20) user.DisplayName = user.DisplayName.Substring(0, 20);
var pattern = @"^[a-zA-Z0-9_@.]*$";
return user.UserName.IsNullOrEmpty() || Regex.IsMatch(user.UserName, pattern);
}
///
/// 查询所有用户
///
///
public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key => DbContextManager.Create().Retrieves());
///
///
///
///
///
///
///
public static bool Authenticate(string userName, string password, Action config)
{
if (!UserChecker(new User { UserName = userName, Password = password })) return false;
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());
private static IEnumerable RetrieveConstUsers()
{
var users = new string[] { "Admin", "User" };
return Retrieves().Where(u => users.Any(usr => usr.Equals(u.UserName, StringComparison.OrdinalIgnoreCase)));
}
///
/// 删除用户
///
///
public static bool Delete(IEnumerable value)
{
var admins = RetrieveConstUsers();
value = value.Where(v => !admins.Any(u => u.Id == v));
if (!value.Any()) return true;
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: $"{RetrieveUsersByNameDataKey}*");
return ret;
}
///
/// 保存新建
///
///
///
public static bool Save(User user)
{
if (!UserChecker(user)) return false;
if (DictHelper.RetrieveSystemModel() && !user.Id.IsNullOrEmpty())
{
var admins = RetrieveConstUsers();
if (admins.Any(v => v.Id == user.Id)) return true;
}
var ret = DbContextManager.Create().Save(user);
if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(user.Id) ? new List() : new List() { user.Id });
return ret;
}
///
///
///
///
///
///
///
public static bool Update(string id, string password, string displayName)
{
if (!UserChecker(new User { Password = password, DisplayName = displayName })) return false;
if (DictHelper.RetrieveSystemModel())
{
var admins = RetrieveConstUsers();
if (admins.Any(v => v.Id == id)) return true;
}
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)
{
if (!UserChecker(new User { UserName = userName, Password = password })) return false;
if (DictHelper.RetrieveSystemModel()
&& RetrieveConstUsers().Any(u => userName.Equals(u.UserName, StringComparison.OrdinalIgnoreCase)))
return true;
return DbContextManager.Create().ChangePassword(userName, password, newPass);
}
///
///
///
///
///
///
public static bool ResetPassword(string userName, string password)
{
if (!UserChecker(new User { UserName = userName, Password = password })) return false;
if (DictHelper.RetrieveSystemModel() && RetrieveConstUsers().Any(u => userName.Equals(u.UserName, StringComparison.OrdinalIgnoreCase))) return true;
return 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: $"{RetrieveUsersByNameDataKey}*");
return ret;
}
///
///
///
///
///
///
public static bool SaveDisplayName(string userName, string displayName)
{
if (!UserChecker(new User { UserName = userName, DisplayName = displayName })) return false;
var ret = DbContextManager.Create().SaveDisplayName(userName, displayName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersByNameDataKey}*");
return ret;
}
///
/// 根据用户名更改用户皮肤
///
///
///
///
public static bool SaveUserCssByName(string userName, string cssName)
{
var ret = DbContextManager.Create().SaveUserCssByName(userName, cssName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersByNameDataKey}*");
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);
}
}