using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Services;
using Volo.Abp.Security.Claims;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Domain.Managers
{
///
/// 用户领域服务
///
public class AccountManager : DomainService
{
private readonly ISqlSugarRepository _repository;
public AccountManager(ISqlSugarRepository repository)
{
_repository = repository;
}
///
/// 登录效验
///
///
///
///
///
public async Task LoginValidationAsync(string userName, string password, Action userAction = null)
{
var user = new UserEntity();
if (await ExistAsync(userName, o => user = o))
{
if (userAction is not null)
{
userAction.Invoke(user);
}
if (user.Password == MD5Helper.SHA2Encode(password, user.Salt))
{
return;
}
throw new UserFriendlyException(UserConst.Login_Error);
}
throw new UserFriendlyException(UserConst.Login_User_No_Exist);
}
///
/// 判断账户合法存在
///
///
///
///
public async Task ExistAsync(string userName, Action userAction = null)
{
var user = await _repository.GetFirstAsync(u => u.UserName == userName && u.State == true);
if (userAction is not null)
{
userAction.Invoke(user);
}
if (user == null)
{
return false;
}
return true;
}
///
/// 令牌转换
///
///
///
public Dictionary UserInfoToClaim(UserRoleMenuDto dto)
{
var claims = new Dictionary();
claims.Add(AbpClaimTypes.UserId, dto.User.Id);
claims.Add(AbpClaimTypes.UserName, dto.User.UserName);
if (dto.User.DeptId is not null)
{
claims.Add(TokenTypeConst.DeptId, dto.User.DeptId);
}
if (dto.User.Email is not null)
{
claims.Add(AbpClaimTypes.Email, dto.User.Email);
}
if (dto.User.Phone is not null)
{
claims.Add(AbpClaimTypes.PhoneNumber, dto.User.Phone);
}
if (UserConst.Admin.Equals(dto.User.UserName))
{
claims.Add(TokenTypeConst.Permission, UserConst.AdminPermissionCode);
claims.Add(TokenTypeConst.Roles, UserConst.AdminRolesCode);
}
else
{
claims.Add(TokenTypeConst.Permission, dto.PermissionCodes.Where(x => !string.IsNullOrEmpty(x)));
claims.Add(AbpClaimTypes.Role, dto.RoleCodes.Where(x => !string.IsNullOrEmpty(x)));
}
return claims;
}
///
/// 更新密码
///
///
///
///
///
///
public async Task UpdatePasswordAsync(Guid userId, string newPassword, string oldPassword)
{
var user = await _repository.GetByIdAsync(userId);
if (!user.JudgePassword(oldPassword))
{
throw new UserFriendlyException("无效更新!原密码错误!");
}
user.Password = newPassword;
user.BuildPassword();
await _repository.UpdateAsync(user);
}
///
/// 重置密码
///
///
///
///
public async Task RestPasswordAsync(Guid userId, string password)
{
var user = await _repository.GetByIdAsync(userId);
EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
user.Password = password;
user.BuildPassword();
return await _repository.UpdateAsync(user);
}
}
}