From df5224cf12dd0b084125f9537a7c73634ad92781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Sun, 28 Apr 2024 15:10:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=9D=83=E9=99=90=E7=89=B9?= =?UTF-8?q?=E6=80=A7=E6=94=AF=E6=8C=81=E5=A4=9A=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PermissionGlobalAttribute.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/PermissionGlobalAttribute.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/PermissionGlobalAttribute.cs index 6183629d..3d5644cc 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/PermissionGlobalAttribute.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/PermissionGlobalAttribute.cs @@ -20,12 +20,24 @@ namespace Yi.Framework.Rbac.Domain.Authorization public override void OnActionExecuting(ActionExecutingContext context) { if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return; - PermissionAttribute? perAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) - .FirstOrDefault(a => a.GetType().Equals(typeof(PermissionAttribute))) as PermissionAttribute; + List? perAttributes = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true) + .Where(a => a.GetType().Equals(typeof(PermissionAttribute))) + .Select(x => x as PermissionAttribute) + .ToList()!; //空对象直接返回 - if (perAttribute is null) return; + if (perAttributes.Count==0) return; + + var result = false; + foreach (var perAttribute in perAttributes) + { + result = _permissionHandler.IsPass(perAttribute.Code); + //存在有一个不满,直接跳出 + if (!result) + { + break; + } + } - var result = _permissionHandler.IsPass(perAttribute.Code); if (!result) { From cabdd55cf522669b53de74a0ec0b504e122b4fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Mon, 29 Apr 2024 14:22:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7account=E9=A2=86?= =?UTF-8?q?=E5=9F=9F=E4=B8=8Euser=E9=A2=86=E5=9F=9F=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AccountService.cs | 31 -------- .../Services/System/UserService.cs | 41 ++++------ .../Managers/AccountManager.cs | 38 ++++------ .../Managers/UserManager.cs | 76 +++++++++++++++++-- 4 files changed, 99 insertions(+), 87 deletions(-) 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..64388c27 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,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 _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 +95,65 @@ namespace Yi.Framework.Rbac.Domain.Managers } } + /// + /// 创建用户 + /// + /// + 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 { 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 +161,7 @@ namespace Yi.Framework.Rbac.Domain.Managers /// public async Task GetInfoAsync(Guid userId) { - + var output = await GetInfoByCacheAsync(userId); return output; } From 547c7d1b6d57d8dbe1ac58080d836613df71aea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= <454313500@qq.com> Date: Mon, 29 Apr 2024 14:42:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Managers/UserManager.cs | 14 ++++++++++---- .../src/Yi.Abp.Web/Properties/launchSettings.json | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) 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 64388c27..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,11 +1,8 @@ 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; @@ -109,7 +106,16 @@ namespace Yi.Framework.Rbac.Domain.Managers throw new UserFriendlyException("密码需大于等于6位!"); } - var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName || x.Phone == userEntity.Phone); + 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("用户已存在,创建失败"); 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"