feat: 用户account领域与user领域关系
This commit is contained in:
@@ -203,18 +203,6 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
throw new UserFriendlyException("验证码错误");
|
||||
}
|
||||
|
||||
private void ValidateUserName(RegisterDto input)
|
||||
{
|
||||
// 正则表达式,匹配只包含数字和字母的字符串
|
||||
string pattern = @"^[a-zA-Z0-9]+$";
|
||||
|
||||
bool isMatch = Regex.IsMatch(input.UserName, pattern);
|
||||
if (!isMatch)
|
||||
{
|
||||
throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册,需要验证码通过
|
||||
/// </summary>
|
||||
@@ -228,28 +216,9 @@ namespace Yi.Framework.Rbac.Application.Services
|
||||
{
|
||||
throw new UserFriendlyException("该系统暂未开放注册功能");
|
||||
}
|
||||
|
||||
if (input.UserName == UserConst.Admin)
|
||||
{
|
||||
throw new UserFriendlyException("用户名无效注册!");
|
||||
}
|
||||
|
||||
if (input.UserName.Length < 2)
|
||||
{
|
||||
throw new UserFriendlyException("账号名需大于等于2位!");
|
||||
}
|
||||
if (input.Password.Length < 6)
|
||||
{
|
||||
throw new UserFriendlyException("密码需大于等于6位!");
|
||||
}
|
||||
|
||||
//校验用户名
|
||||
ValidateUserName(input);
|
||||
|
||||
//校验验证码,根据电话号码获取 value,比对验证码已经uuid
|
||||
await ValidationPhoneCaptchaAsync(input);
|
||||
|
||||
|
||||
//注册领域逻辑
|
||||
await _accountManager.RegisterAsync(input.UserName, input.Password, input.Phone);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
||||
/// <summary>
|
||||
/// User服务实现
|
||||
/// </summary>
|
||||
public class UserService : YiCrudAppService<UserEntity, UserGetOutputDto, UserGetListOutputDto, Guid, UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>,IUserService
|
||||
public class UserService : YiCrudAppService<UserEntity, UserGetOutputDto, UserGetListOutputDto, Guid, UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>, IUserService
|
||||
//IUserService
|
||||
{
|
||||
public UserService(ISqlSugarRepository<UserEntity, Guid> repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache) : base(repository)
|
||||
@@ -82,7 +82,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
||||
|
||||
protected override UserEntity MapToEntity(UserCreateInputVo createInput)
|
||||
{
|
||||
var output= base.MapToEntity(createInput);
|
||||
var output = base.MapToEntity(createInput);
|
||||
output.EncryPassword = new Domain.Entities.ValueObjects.EncryPasswordValueObject(createInput.Password);
|
||||
return output;
|
||||
}
|
||||
@@ -96,37 +96,24 @@ namespace Yi.Framework.Rbac.Application.Services.System
|
||||
[Permission("system:user:add")]
|
||||
public async override Task<UserGetOutputDto> CreateAsync(UserCreateInputVo input)
|
||||
{
|
||||
if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.Name_Not_Allowed);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(input.Password))
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.Login_Passworld_Error);
|
||||
}
|
||||
if (await _repository.IsAnyAsync(u => input.UserName.Equals(u.UserName)))
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.User_Exist);
|
||||
}
|
||||
var entitiy = await MapToEntityAsync(input);
|
||||
|
||||
entitiy.BuildPassword();
|
||||
|
||||
//using (var uow = _unitOfWorkManager.CreateContext())
|
||||
//{
|
||||
var returnEntity = await _repository.InsertReturnEntityAsync(entitiy);
|
||||
await _userManager.GiveUserSetRoleAsync(new List<Guid> { returnEntity.Id }, input.RoleIds);
|
||||
await _userManager.GiveUserSetPostAsync(new List<Guid> { returnEntity.Id }, input.PostIds);
|
||||
//uow.Commit();
|
||||
await _userManager.CreateAsync(entitiy);
|
||||
await _userManager.GiveUserSetRoleAsync(new List<Guid> { entitiy.Id }, input.RoleIds);
|
||||
await _userManager.GiveUserSetPostAsync(new List<Guid> { entitiy.Id }, input.PostIds);
|
||||
|
||||
var result = await MapToGetOutputDtoAsync(returnEntity);
|
||||
|
||||
|
||||
await _localEventBus.PublishAsync(new UserCreateEventArgs(returnEntity.Id));
|
||||
var result = await MapToGetOutputDtoAsync(entitiy);
|
||||
return result;
|
||||
//}
|
||||
}
|
||||
|
||||
protected override async Task<UserEntity> MapToEntityAsync(UserCreateInputVo createInput)
|
||||
{
|
||||
var entitiy = await base.MapToEntityAsync(createInput);
|
||||
entitiy.BuildPassword();
|
||||
return entitiy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单查
|
||||
/// </summary>
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.No_Role);
|
||||
}
|
||||
if (userInfo.PermissionCodes.Count() ==0)
|
||||
if (userInfo.PermissionCodes.Count() == 0)
|
||||
{
|
||||
throw new UserFriendlyException(UserConst.No_Permission);
|
||||
}
|
||||
@@ -181,7 +181,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
userAction.Invoke(user);
|
||||
}
|
||||
//这里为了兼容解决数据库开启了大小写不敏感问题,还要将用户名进行二次校验
|
||||
if (user != null&&user.UserName==userName)
|
||||
if (user != null && user.UserName == userName)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -225,7 +225,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
dto.PermissionCodes?.ForEach(per => AddToClaim(claims, TokenTypeConst.Permission, per));
|
||||
dto.RoleCodes?.ForEach(role => AddToClaim(claims, AbpClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
|
||||
return claims;
|
||||
}
|
||||
|
||||
@@ -265,34 +265,24 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
public async Task<bool> RestPasswordAsync(Guid userId, string password)
|
||||
{
|
||||
var user = await _repository.GetByIdAsync(userId);
|
||||
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
|
||||
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
|
||||
user.EncryPassword.Password = password;
|
||||
user.BuildPassword();
|
||||
return await _repository.UpdateAsync(user);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 注册用户,创建用户之后设置默认角色
|
||||
/// </summary>
|
||||
/// <param name="userName"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <param name="phone"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RegisterAsync(string userName, string password, long phone)
|
||||
{
|
||||
//输入的用户名与电话号码都不能在数据库中存在
|
||||
UserEntity user = new();
|
||||
var isExist = await _repository.IsAnyAsync(x => x.UserName == userName || x.Phone == phone);
|
||||
if (isExist)
|
||||
{
|
||||
throw new UserFriendlyException("用户已存在,注册失败");
|
||||
}
|
||||
|
||||
var newUser = new UserEntity(userName, password, phone);
|
||||
|
||||
var entity = await _repository.InsertReturnEntityAsync(newUser);
|
||||
//赋上一个初始角色
|
||||
var role = await _roleRepository.GetFirstAsync(x => x.RoleCode == UserConst.DefaultRoleCode);
|
||||
if (role is not null)
|
||||
{
|
||||
await _userManager.GiveUserSetRoleAsync(new List<Guid> { entity.Id }, new List<Guid> { role.Id });
|
||||
}
|
||||
|
||||
await _localEventBus.PublishAsync(new UserCreateEventArgs(entity.Id));
|
||||
var user = new UserEntity(userName, password, phone);
|
||||
await _userManager.CreateAsync(user);
|
||||
await _userManager.SetDefautRoleAsync(user.Id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
using Mapster;
|
||||
using System.Text.RegularExpressions;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using TencentCloud.Ame.V20190916.Models;
|
||||
using Volo.Abp.Authorization;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Domain.Services;
|
||||
using Volo.Abp.EventBus.Local;
|
||||
using Volo.Abp.Guids;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Repositories;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Caches;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Etos;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Options;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
@@ -22,12 +27,14 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
public readonly ISqlSugarRepository<UserEntity> _repository;
|
||||
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
|
||||
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
|
||||
private readonly ISqlSugarRepository<RoleEntity> _roleRepository;
|
||||
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
|
||||
private readonly IGuidGenerator _guidGenerator;
|
||||
private IUserRepository _userRepository;
|
||||
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository) =>
|
||||
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) =
|
||||
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository);
|
||||
private ILocalEventBus _localEventBus;
|
||||
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository, ILocalEventBus localEventBus, ISqlSugarRepository<RoleEntity> roleRepository) =>
|
||||
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository, _localEventBus, _roleRepository) =
|
||||
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository, localEventBus, roleRepository);
|
||||
|
||||
/// <summary>
|
||||
/// 给用户设置角色
|
||||
@@ -88,6 +95,65 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建用户
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task CreateAsync(UserEntity userEntity)
|
||||
{
|
||||
//校验用户名
|
||||
ValidateUserName(userEntity);
|
||||
|
||||
if (userEntity.EncryPassword?.Password.Length < 6)
|
||||
{
|
||||
throw new UserFriendlyException("密码需大于等于6位!");
|
||||
}
|
||||
|
||||
var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName || x.Phone == userEntity.Phone);
|
||||
if (isExist)
|
||||
{
|
||||
throw new UserFriendlyException("用户已存在,创建失败");
|
||||
}
|
||||
|
||||
var entity = await _repository.InsertReturnEntityAsync(userEntity);
|
||||
|
||||
userEntity = entity;
|
||||
await _localEventBus.PublishAsync(new UserCreateEventArgs(entity.Id));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task SetDefautRoleAsync(Guid userId)
|
||||
{
|
||||
var role = await _roleRepository.GetFirstAsync(x => x.RoleCode == UserConst.DefaultRoleCode);
|
||||
if (role is not null)
|
||||
{
|
||||
await GiveUserSetRoleAsync(new List<Guid> { userId }, new List<Guid> { role.Id });
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateUserName(UserEntity input)
|
||||
{
|
||||
if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
|
||||
{
|
||||
throw new UserFriendlyException("用户名无效注册!");
|
||||
}
|
||||
|
||||
if (input.UserName.Length < 2)
|
||||
{
|
||||
throw new UserFriendlyException("账号名需大于等于2位!");
|
||||
}
|
||||
|
||||
// 正则表达式,匹配只包含数字和字母的字符串
|
||||
string pattern = @"^[a-zA-Z0-9]+$";
|
||||
|
||||
bool isMatch = Regex.IsMatch(input.UserName, pattern);
|
||||
if (!isMatch)
|
||||
{
|
||||
throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询用户信息,已缓存
|
||||
@@ -95,7 +161,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
|
||||
/// <returns></returns>
|
||||
public async Task<UserRoleMenuDto> GetInfoAsync(Guid userId)
|
||||
{
|
||||
|
||||
|
||||
var output = await GetInfoByCacheAsync(userId);
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user