diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs index d3bdccfb..5a1bc271 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/AccountService.cs @@ -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("用户名不能包含除【字母】与【数字】的其他字符"); - } - } - /// /// 注册,需要验证码通过 /// @@ -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); } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs index e640fca0..c3bcab92 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs @@ -24,7 +24,7 @@ namespace Yi.Framework.Rbac.Application.Services.System /// /// User服务实现 /// - public class UserService : YiCrudAppService,IUserService + public class UserService : YiCrudAppService, IUserService //IUserService { public UserService(ISqlSugarRepository repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus, IDistributedCache 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 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 { returnEntity.Id }, input.RoleIds); - await _userManager.GiveUserSetPostAsync(new List { returnEntity.Id }, input.PostIds); - //uow.Commit(); + await _userManager.CreateAsync(entitiy); + await _userManager.GiveUserSetRoleAsync(new List { entitiy.Id }, input.RoleIds); + await _userManager.GiveUserSetPostAsync(new List { 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 MapToEntityAsync(UserCreateInputVo createInput) + { + var entitiy = await base.MapToEntityAsync(createInput); + entitiy.BuildPassword(); + return entitiy; + } + /// /// 单查 /// diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/AccountManager.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/AccountManager.cs index 0af828a7..a6953067 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/AccountManager.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/AccountManager.cs @@ -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 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); } - + /// + /// 注册用户,创建用户之后设置默认角色 + /// + /// + /// + /// + /// 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 { entity.Id }, new List { 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); } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs index dcc906b6..7d72138f 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs @@ -1,17 +1,19 @@ -using Mapster; -using Microsoft.AspNetCore.Authorization; +using System.Text.RegularExpressions; +using Mapster; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; 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 +24,14 @@ namespace Yi.Framework.Rbac.Domain.Managers public readonly ISqlSugarRepository _repository; public readonly ISqlSugarRepository _repositoryUserRole; public readonly ISqlSugarRepository _repositoryUserPost; + private readonly ISqlSugarRepository _roleRepository; private IDistributedCache _userCache; private readonly IGuidGenerator _guidGenerator; private IUserRepository _userRepository; - public UserManager(ISqlSugarRepository repository, ISqlSugarRepository repositoryUserRole, ISqlSugarRepository repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache userCache, IUserRepository userRepository) => - (_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) = - (repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository); + private ILocalEventBus _localEventBus; + public UserManager(ISqlSugarRepository repository, ISqlSugarRepository repositoryUserRole, ISqlSugarRepository repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache userCache, IUserRepository userRepository, ILocalEventBus localEventBus, ISqlSugarRepository roleRepository) => + (_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository, _localEventBus, _roleRepository) = + (repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository, localEventBus, roleRepository); /// /// 给用户设置角色 @@ -88,6 +92,74 @@ namespace Yi.Framework.Rbac.Domain.Managers } } + /// + /// 创建用户 + /// + /// + public async Task CreateAsync(UserEntity userEntity) + { + //校验用户名 + ValidateUserName(userEntity); + + if (userEntity.EncryPassword?.Password.Length < 6) + { + throw new UserFriendlyException("密码需大于等于6位!"); + } + + if (userEntity.Phone is not null) + { + if (await _repository.IsAnyAsync(x => x.Phone == userEntity.Phone)) + { + throw new UserFriendlyException("用户手机号已重复"); + + } + } + + var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName); + 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 { userId }, new List { 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("用户名不能包含除【字母】与【数字】的其他字符"); + } + } /// /// 查询用户信息,已缓存 @@ -95,7 +167,7 @@ namespace Yi.Framework.Rbac.Domain.Managers /// public async Task GetInfoAsync(Guid userId) { - + var output = await GetInfoByCacheAsync(userId); return output; } diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/Properties/launchSettings.json b/Yi.Abp.Net8/src/Yi.Abp.Web/Properties/launchSettings.json index 56770734..8f58fa9b 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/Properties/launchSettings.json +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/Properties/launchSettings.json @@ -5,8 +5,8 @@ "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { - //"ASPNETCORE_ENVIRONMENT": "Development" - ,"ASPNETCORE_ENVIRONMENT": "Staging" + //"ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_ENVIRONMENT": "Staging" }, "dotnetRunMessages": true, "applicationUrl": "http://localhost:19001"