feat: 支持邮箱注册功能
This commit is contained in:
@@ -24,7 +24,6 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 用户领域服务
|
||||
/// </summary>
|
||||
@@ -62,7 +61,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
/// <param name="getUserInfo"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
public async Task<string> GetTokenByUserIdAsync(Guid userId,Action<UserRoleMenuDto>? getUserInfo=null)
|
||||
public async Task<string> GetTokenByUserIdAsync(Guid userId, Action<UserRoleMenuDto>? getUserInfo = null)
|
||||
{
|
||||
//获取用户信息
|
||||
var userInfo = await _userManager.GetInfoAsync(userId);
|
||||
@@ -77,6 +76,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.No_Role);
|
||||
}
|
||||
|
||||
if (!userInfo.PermissionCodes.Any())
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.No_Permission);
|
||||
@@ -86,7 +86,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
getUserInfo(userInfo);
|
||||
}
|
||||
|
||||
|
||||
var accessToken = CreateToken(this.UserInfoToClaim(userInfo));
|
||||
//将用户信息添加到缓存中,需要考虑的是更改了用户、角色、菜单等整个体系都需要将缓存进行刷新,看具体业务进行选择
|
||||
return accessToken;
|
||||
@@ -103,12 +103,12 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
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,
|
||||
expires: DateTime.Now.AddSeconds(_jwtOptions.ExpiresSecondTime),
|
||||
notBefore: DateTime.Now,
|
||||
signingCredentials: creds);
|
||||
issuer: _jwtOptions.Issuer,
|
||||
audience: _jwtOptions.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.Now.AddSeconds(_jwtOptions.ExpiresSecondTime),
|
||||
notBefore: DateTime.Now,
|
||||
signingCredentials: creds);
|
||||
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
|
||||
return returnToken;
|
||||
@@ -119,22 +119,23 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_refreshJwtOptions.SecurityKey));
|
||||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
//添加用户id,及刷新token的标识
|
||||
var claims = new List<Claim> {
|
||||
new Claim(AbpClaimTypes.UserId,userId.ToString()),
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(AbpClaimTypes.UserId, userId.ToString()),
|
||||
new Claim(TokenTypeConst.Refresh, "true")
|
||||
};
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: _refreshJwtOptions.Issuer,
|
||||
audience: _refreshJwtOptions.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.Now.AddSeconds(_refreshJwtOptions.ExpiresSecondTime),
|
||||
notBefore: DateTime.Now,
|
||||
signingCredentials: creds);
|
||||
issuer: _refreshJwtOptions.Issuer,
|
||||
audience: _refreshJwtOptions.Audience,
|
||||
claims: claims,
|
||||
expires: DateTime.Now.AddSeconds(_refreshJwtOptions.ExpiresSecondTime),
|
||||
notBefore: DateTime.Now,
|
||||
signingCredentials: creds);
|
||||
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
|
||||
|
||||
return returnToken;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录校验
|
||||
/// </summary>
|
||||
@@ -142,7 +143,8 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
/// <param name="password"></param>
|
||||
/// <param name="userAction"></param>
|
||||
/// <returns></returns>
|
||||
public async Task LoginValidationAsync(string userName, string password, Action<UserAggregateRoot> userAction = null)
|
||||
public async Task LoginValidationAsync(string userName, string password,
|
||||
Action<UserAggregateRoot> userAction = null)
|
||||
{
|
||||
var user = new UserAggregateRoot();
|
||||
if (await ExistAsync(userName, o => user = o))
|
||||
@@ -151,12 +153,15 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
userAction.Invoke(user);
|
||||
}
|
||||
|
||||
if (user.EncryPassword.Password == MD5Helper.SHA2Encode(password, user.EncryPassword.Salt))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
throw new UserFriendlyException(UserConst.Login_Error);
|
||||
}
|
||||
|
||||
throw new UserFriendlyException(UserConst.Login_User_No_Exist);
|
||||
}
|
||||
|
||||
@@ -173,22 +178,22 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
userAction.Invoke(user);
|
||||
}
|
||||
|
||||
//这里为了兼容解决数据库开启了大小写不敏感问题,还要将用户名进行二次校验
|
||||
if (user != null && user.UserName == userName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 令牌转换
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public List<KeyValuePair<string, string>> UserInfoToClaim(UserRoleMenuDto dto)
|
||||
{
|
||||
var claims = new List<KeyValuePair<string, string>>();
|
||||
@@ -198,18 +203,24 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
AddToClaim(claims, TokenTypeConst.DeptId, dto.User.DeptId.ToString());
|
||||
}
|
||||
|
||||
if (dto.User.Email is not null)
|
||||
{
|
||||
AddToClaim(claims, AbpClaimTypes.Email, dto.User.Email);
|
||||
}
|
||||
|
||||
if (dto.User.Phone is not null)
|
||||
{
|
||||
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 })));
|
||||
AddToClaim(claims, TokenTypeConst.RoleInfo,
|
||||
JsonConvert.SerializeObject(dto.Roles.Select(x => new RoleTokenInfoModel
|
||||
{ Id = x.Id, DataScope = x.DataScope })));
|
||||
}
|
||||
|
||||
if (UserConst.Admin.Equals(dto.User.UserName))
|
||||
{
|
||||
AddToClaim(claims, TokenTypeConst.Permission, UserConst.AdminPermissionCode);
|
||||
@@ -246,6 +257,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
throw new UserFriendlyException("无效更新!原密码错误!");
|
||||
}
|
||||
|
||||
user.EncryPassword.Password = newPassword;
|
||||
user.BuildPassword();
|
||||
await _repository.UpdateAsync(user);
|
||||
@@ -271,14 +283,21 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="phone"></param>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="nick"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Guid> RegisterAsync(string userName, string password, long? phone,string? nick)
|
||||
public async Task<Guid> RegisterAsync(string userName, string password, long? phone, string? email,
|
||||
string? nick)
|
||||
{
|
||||
var user = new UserAggregateRoot(userName, password, phone,nick);
|
||||
var userId=await _userManager.CreateAsync(user);
|
||||
if (phone is null && string.IsNullOrWhiteSpace(email))
|
||||
{
|
||||
throw new UserFriendlyException("注册时,电话与邮箱不能同时为空");
|
||||
}
|
||||
|
||||
var user = new UserAggregateRoot(userName, password, phone, email, nick);
|
||||
var userId = await _userManager.CreateAsync(user);
|
||||
await _userManager.SetDefautRoleAsync(user.Id);
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
string CreateRefreshToken(Guid userId);
|
||||
Task<string> GetTokenByUserIdAsync(Guid userId,Action<UserRoleMenuDto>? getUserInfo=null);
|
||||
Task LoginValidationAsync(string userName, string password, Action<UserAggregateRoot>? userAction = null);
|
||||
Task<Guid> RegisterAsync(string userName, string password, long? phone,string? nick);
|
||||
Task<Guid> RegisterAsync(string userName, string password, long? phone, string? email, string? nick);
|
||||
Task<bool> RestPasswordAsync(Guid userId, string password);
|
||||
Task UpdatePasswordAsync(Guid userId, string newPassword, string oldPassword);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user