feat: 支持邮箱注册功能

This commit is contained in:
ccnetcore
2025-07-13 21:26:46 +08:00
parent 020d674ca2
commit 43dc962606
18 changed files with 483 additions and 214 deletions

View File

@@ -17,14 +17,15 @@ namespace Yi.Framework.Rbac.Domain.Entities
{
public UserAggregateRoot()
{
}
public UserAggregateRoot(string userName, string password, long? phone, string? nick = null)
public UserAggregateRoot(string userName, string password, long? phone, string? email, string? nick = null)
{
UserName = userName;
EncryPassword.Password = password;
Phone = phone;
Nick =string.IsNullOrWhiteSpace(nick)?"萌新-"+userName:nick.Trim();
Email = email;
Nick = string.IsNullOrWhiteSpace(nick) ? "萌新-" + userName : nick.Trim();
BuildPassword();
}
@@ -185,8 +186,10 @@ namespace Yi.Framework.Rbac.Domain.Entities
{
throw new ArgumentNullException(nameof(EncryPassword.Password));
}
password = EncryPassword.Password;
}
EncryPassword.Salt = MD5Helper.GenerateSalt();
EncryPassword.Password = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
return this;
@@ -203,14 +206,14 @@ namespace Yi.Framework.Rbac.Domain.Entities
{
throw new ArgumentNullException(EncryPassword.Salt);
}
var p = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
if (EncryPassword.Password == MD5Helper.SHA2Encode(password, EncryPassword.Salt))
{
return true;
}
return false;
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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);
}

View File

@@ -23,6 +23,7 @@
<ItemGroup>
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="Volo.Abp.DistributedLocking" Version="$(AbpVersion)" />
<PackageReference Include="Volo.Abp.MailKit" Version="$(AbpVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\Yi.Framework.Caching.FreeRedis\Yi.Framework.Caching.FreeRedis.csproj" />

View File

@@ -7,10 +7,10 @@ using Volo.Abp.AspNetCore.SignalR;
using Volo.Abp.Caching;
using Volo.Abp.DistributedLocking;
using Volo.Abp.Domain;
using Volo.Abp.Emailing;
using Volo.Abp.Imaging;
using Volo.Abp.Modularity;
using Volo.Abp.MailKit;
using Yi.Framework.Caching.FreeRedis;
using Yi.Framework.Mapster;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Operlog;
using Yi.Framework.Rbac.Domain.Shared;
@@ -26,7 +26,8 @@ namespace Yi.Framework.Rbac.Domain
typeof(AbpDddDomainModule),
typeof(AbpCachingModule),
typeof(AbpImagingImageSharpModule),
typeof(AbpDistributedLockingModule)
typeof(AbpDistributedLockingModule),
typeof(AbpMailKitModule)
)]
public class YiFrameworkRbacDomainModule : AbpModule
{