2024-01-07 13:34:50 +08:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Mapster;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2024-01-18 16:28:09 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
using Volo.Abp.Caching;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.Domain.Entities;
|
|
|
|
|
|
using Volo.Abp.Domain.Services;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
using Volo.Abp.EventBus.Local;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp.Security.Claims;
|
|
|
|
|
|
using Yi.Framework.Core.Helper;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Entities;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Repositories;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Caches;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
2024-01-18 16:28:09 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Model;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Options;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Rbac.Domain.Managers
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用户领域服务
|
|
|
|
|
|
/// </summary>
|
2024-01-07 13:34:50 +08:00
|
|
|
|
public class AccountManager : DomainService, IAccountManager
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-01-07 13:34:50 +08:00
|
|
|
|
private readonly IUserRepository _repository;
|
|
|
|
|
|
private readonly ILocalEventBus _localEventBus;
|
|
|
|
|
|
private readonly JwtOptions _jwtOptions;
|
2024-02-08 19:48:35 +08:00
|
|
|
|
private readonly RbacOptions _options;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
private UserManager _userManager;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<RoleAggregateRoot> _roleRepository;
|
2024-01-23 23:35:38 +08:00
|
|
|
|
private RefreshJwtOptions _refreshJwtOptions;
|
2024-02-18 11:41:43 +08:00
|
|
|
|
|
2024-01-07 13:34:50 +08:00
|
|
|
|
public AccountManager(IUserRepository repository
|
|
|
|
|
|
, IOptions<JwtOptions> jwtOptions
|
|
|
|
|
|
, ILocalEventBus localEventBus
|
|
|
|
|
|
, UserManager userManager
|
2024-01-24 11:26:44 +08:00
|
|
|
|
, IOptions<RefreshJwtOptions> refreshJwtOptions
|
2024-05-22 14:35:08 +08:00
|
|
|
|
, ISqlSugarRepository<RoleAggregateRoot> roleRepository
|
2024-02-08 19:48:35 +08:00
|
|
|
|
, IOptions<RbacOptions> options)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
2024-01-07 13:34:50 +08:00
|
|
|
|
_jwtOptions = jwtOptions.Value;
|
2024-01-18 16:28:09 +08:00
|
|
|
|
_localEventBus = localEventBus;
|
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
|
_roleRepository = roleRepository;
|
2024-01-24 11:26:44 +08:00
|
|
|
|
_refreshJwtOptions = refreshJwtOptions.Value;
|
2024-02-08 19:48:35 +08:00
|
|
|
|
_options = options.Value;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-07 13:34:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据用户id获取token
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId"></param>
|
2024-09-05 23:10:40 +08:00
|
|
|
|
/// <param name="getUserInfo"></param>
|
2024-01-07 13:34:50 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
2024-09-05 23:10:40 +08:00
|
|
|
|
public async Task<string> GetTokenByUserIdAsync(Guid userId,Action<UserRoleMenuDto>? getUserInfo=null)
|
2024-01-07 13:34:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
//获取用户信息
|
2024-04-09 17:45:12 +08:00
|
|
|
|
var userInfo = await _userManager.GetInfoAsync(userId);
|
2024-01-07 13:34:50 +08:00
|
|
|
|
|
|
|
|
|
|
//判断用户状态
|
|
|
|
|
|
if (userInfo.User.State == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException(UserConst.State_Is_State);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (userInfo.RoleCodes.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException(UserConst.No_Role);
|
|
|
|
|
|
}
|
2024-09-05 23:10:40 +08:00
|
|
|
|
if (!userInfo.PermissionCodes.Any())
|
2024-04-28 11:45:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException(UserConst.No_Permission);
|
|
|
|
|
|
}
|
2024-09-05 23:10:40 +08:00
|
|
|
|
|
|
|
|
|
|
if (getUserInfo is not null)
|
2024-01-07 13:34:50 +08:00
|
|
|
|
{
|
2024-09-05 23:10:40 +08:00
|
|
|
|
getUserInfo(userInfo);
|
2024-01-07 13:34:50 +08:00
|
|
|
|
}
|
2024-09-05 23:10:40 +08:00
|
|
|
|
|
2024-02-18 11:41:43 +08:00
|
|
|
|
var accessToken = CreateToken(this.UserInfoToClaim(userInfo));
|
2024-01-07 13:34:50 +08:00
|
|
|
|
//将用户信息添加到缓存中,需要考虑的是更改了用户、角色、菜单等整个体系都需要将缓存进行刷新,看具体业务进行选择
|
|
|
|
|
|
return accessToken;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建令牌
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="kvs"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private string CreateToken(List<KeyValuePair<string, string>> kvs)
|
|
|
|
|
|
{
|
|
|
|
|
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtOptions.SecurityKey));
|
|
|
|
|
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
|
|
|
|
var claims = kvs.Select(x => new Claim(x.Key, x.Value.ToString())).ToList();
|
|
|
|
|
|
var token = new JwtSecurityToken(
|
|
|
|
|
|
issuer: _jwtOptions.Issuer,
|
|
|
|
|
|
audience: _jwtOptions.Audience,
|
|
|
|
|
|
claims: claims,
|
2024-01-23 17:49:49 +08:00
|
|
|
|
expires: DateTime.Now.AddMinutes(_jwtOptions.ExpiresMinuteTime),
|
2024-01-07 13:34:50 +08:00
|
|
|
|
notBefore: DateTime.Now,
|
|
|
|
|
|
signingCredentials: creds);
|
|
|
|
|
|
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
|
|
|
|
|
|
|
|
|
|
|
return returnToken;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-24 11:26:44 +08:00
|
|
|
|
public string CreateRefreshToken(Guid userId)
|
2024-01-23 23:35:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_refreshJwtOptions.SecurityKey));
|
|
|
|
|
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
2024-01-24 11:26:44 +08:00
|
|
|
|
//添加用户id,及刷新token的标识
|
|
|
|
|
|
var claims = new List<Claim> {
|
|
|
|
|
|
new Claim(AbpClaimTypes.UserId,userId.ToString()),
|
|
|
|
|
|
new Claim(TokenTypeConst.Refresh, "true")
|
|
|
|
|
|
};
|
2024-01-23 23:35:38 +08:00
|
|
|
|
var token = new JwtSecurityToken(
|
|
|
|
|
|
issuer: _refreshJwtOptions.Issuer,
|
|
|
|
|
|
audience: _refreshJwtOptions.Audience,
|
|
|
|
|
|
claims: claims,
|
|
|
|
|
|
expires: DateTime.Now.AddMinutes(_refreshJwtOptions.ExpiresMinuteTime),
|
|
|
|
|
|
notBefore: DateTime.Now,
|
|
|
|
|
|
signingCredentials: creds);
|
|
|
|
|
|
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
2024-01-07 13:34:50 +08:00
|
|
|
|
|
2024-01-23 23:35:38 +08:00
|
|
|
|
return returnToken;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
2024-03-07 11:32:49 +08:00
|
|
|
|
/// 登录校验
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
|
/// <param name="userAction"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-05-22 14:35:08 +08:00
|
|
|
|
public async Task LoginValidationAsync(string userName, string password, Action<UserAggregateRoot> userAction = null)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-05-22 14:35:08 +08:00
|
|
|
|
var user = new UserAggregateRoot();
|
2023-04-13 21:12:06 +08:00
|
|
|
|
if (await ExistAsync(userName, o => user = o))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (userAction is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
userAction.Invoke(user);
|
|
|
|
|
|
}
|
2024-04-22 18:06:09 +08:00
|
|
|
|
if (user.EncryPassword.Password == MD5Helper.SHA2Encode(password, user.EncryPassword.Salt))
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-10-04 23:16:07 +08:00
|
|
|
|
throw new UserFriendlyException(UserConst.Login_Error);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2023-10-04 23:16:07 +08:00
|
|
|
|
throw new UserFriendlyException(UserConst.Login_User_No_Exist);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断账户合法存在
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
|
/// <param name="userAction"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-05-22 14:35:08 +08:00
|
|
|
|
public async Task<bool> ExistAsync(string userName, Action<UserAggregateRoot> userAction = null)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
var user = await _repository.GetFirstAsync(u => u.UserName == userName && u.State == true);
|
|
|
|
|
|
if (userAction is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
userAction.Invoke(user);
|
|
|
|
|
|
}
|
2024-03-07 11:32:49 +08:00
|
|
|
|
//这里为了兼容解决数据库开启了大小写不敏感问题,还要将用户名进行二次校验
|
2024-04-29 14:22:45 +08:00
|
|
|
|
if (user != null && user.UserName == userName)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2024-02-18 09:52:11 +08:00
|
|
|
|
return true;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2024-02-18 09:52:11 +08:00
|
|
|
|
return false;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2024-10-03 01:10:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 令牌转换
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dto"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
2023-12-22 18:11:14 +08:00
|
|
|
|
public List<KeyValuePair<string, string>> UserInfoToClaim(UserRoleMenuDto dto)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
2023-12-22 18:11:14 +08:00
|
|
|
|
var claims = new List<KeyValuePair<string, string>>();
|
2024-01-18 16:28:09 +08:00
|
|
|
|
AddToClaim(claims, AbpClaimTypes.UserId, dto.User.Id.ToString());
|
|
|
|
|
|
AddToClaim(claims, AbpClaimTypes.UserName, dto.User.UserName);
|
2023-05-22 12:57:27 +08:00
|
|
|
|
if (dto.User.DeptId is not null)
|
|
|
|
|
|
{
|
2024-01-18 16:28:09 +08:00
|
|
|
|
AddToClaim(claims, TokenTypeConst.DeptId, dto.User.DeptId.ToString());
|
2023-05-22 12:57:27 +08:00
|
|
|
|
}
|
2023-04-13 21:12:06 +08:00
|
|
|
|
if (dto.User.Email is not null)
|
|
|
|
|
|
{
|
2024-01-18 16:28:09 +08:00
|
|
|
|
AddToClaim(claims, AbpClaimTypes.Email, dto.User.Email);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.User.Phone is not null)
|
|
|
|
|
|
{
|
2024-01-18 16:28:09 +08:00
|
|
|
|
AddToClaim(claims, AbpClaimTypes.PhoneNumber, dto.User.Phone.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (dto.Roles.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
AddToClaim(claims, TokenTypeConst.RoleInfo, JsonConvert.SerializeObject(dto.Roles.Select(x => new RoleTokenInfoModel { Id = x.Id, DataScope = x.DataScope })));
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (UserConst.Admin.Equals(dto.User.UserName))
|
|
|
|
|
|
{
|
2024-01-18 16:28:09 +08:00
|
|
|
|
AddToClaim(claims, TokenTypeConst.Permission, UserConst.AdminPermissionCode);
|
|
|
|
|
|
AddToClaim(claims, TokenTypeConst.Roles, UserConst.AdminRolesCode);
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-12-22 18:11:14 +08:00
|
|
|
|
dto.PermissionCodes?.ForEach(per => AddToClaim(claims, TokenTypeConst.Permission, per));
|
|
|
|
|
|
dto.RoleCodes?.ForEach(role => AddToClaim(claims, AbpClaimTypes.Role, role));
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
2024-04-29 14:22:45 +08:00
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
return claims;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-22 18:11:14 +08:00
|
|
|
|
|
|
|
|
|
|
private void AddToClaim(List<KeyValuePair<string, string>> claims, string key, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
claims.Add(new KeyValuePair<string, string>(key, value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新密码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
|
/// <param name="newPassword"></param>
|
|
|
|
|
|
/// <param name="oldPassword"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task UpdatePasswordAsync(Guid userId, string newPassword, string oldPassword)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
var user = await _repository.GetByIdAsync(userId);
|
|
|
|
|
|
|
|
|
|
|
|
if (!user.JudgePassword(oldPassword))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("无效更新!原密码错误!");
|
|
|
|
|
|
}
|
2024-04-22 18:06:09 +08:00
|
|
|
|
user.EncryPassword.Password = newPassword;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
user.BuildPassword();
|
|
|
|
|
|
await _repository.UpdateAsync(user);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-03 01:10:32 +08:00
|
|
|
|
/// 重置密码,也可以是找回密码
|
2023-04-13 21:12:06 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task<bool> RestPasswordAsync(Guid userId, string password)
|
2023-04-13 21:12:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
var user = await _repository.GetByIdAsync(userId);
|
2024-04-22 18:06:09 +08:00
|
|
|
|
user.EncryPassword.Password = password;
|
2023-04-13 21:12:06 +08:00
|
|
|
|
user.BuildPassword();
|
|
|
|
|
|
return await _repository.UpdateAsync(user);
|
|
|
|
|
|
}
|
2024-01-07 13:34:50 +08:00
|
|
|
|
|
2024-04-29 14:22:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 注册用户,创建用户之后设置默认角色
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
|
/// <param name="phone"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-02 23:25:29 +08:00
|
|
|
|
public async Task RegisterAsync(string userName, string password, long phone,string? nick)
|
2024-01-07 13:34:50 +08:00
|
|
|
|
{
|
2024-10-02 23:25:29 +08:00
|
|
|
|
var user = new UserAggregateRoot(userName, password, phone,nick);
|
2024-04-29 14:22:45 +08:00
|
|
|
|
await _userManager.CreateAsync(user);
|
|
|
|
|
|
await _userManager.SetDefautRoleAsync(user.Id);
|
2024-01-07 13:34:50 +08:00
|
|
|
|
}
|
2023-04-13 21:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|