From 0114b31fe0c0d08762f13be6d71fd1da3212a06c Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:41:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=AF=86=E7=A0=81api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LINGYUN/Abp/Account/AccountAppService.cs | 42 ++++++++++++++++++- .../LINGYUN/Abp/Account/AccountController.cs | 12 +++--- .../Dto/IdentityUserSetPasswordInput.cs | 14 +++++++ .../Abp/Identity/IIdentityUserAppService.cs | 3 +- .../IdentityPermissionDefinitionProvider.cs | 1 + .../Abp/Identity/IdentityPermissions.cs | 1 + .../Abp/Identity/IdentityUserAppService.cs | 13 +++++- .../LINGYUN/Abp/Identity/Localization/en.json | 1 + .../Abp/Identity/Localization/zh-Hans.json | 1 + .../Abp/Identity/IdentityUserController.cs | 6 +++ 10 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserSetPasswordInput.cs diff --git a/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs b/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs index fa789f53c..897dec411 100644 --- a/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs +++ b/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs @@ -6,13 +6,16 @@ using LINGYUN.Abp.WeChat.MiniProgram; using LINGYUN.Abp.WeChat.OpenId; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System; using System.ComponentModel.DataAnnotations; using System.Text; using System.Threading.Tasks; using Volo.Abp; +using Volo.Abp.Account; using Volo.Abp.Caching; +using Volo.Abp.Clients; using Volo.Abp.Identity; using Volo.Abp.Settings; using Volo.Abp.Validation; @@ -26,6 +29,7 @@ namespace LINGYUN.Abp.Account protected IIdentityUserRepository UserRepository { get; } protected IUserSecurityCodeSender SecurityCodeSender { get; } protected IWeChatOpenIdFinder WeChatOpenIdFinder { get; } + protected IdentitySecurityLogManager IdentitySecurityLogManager { get; } protected AbpWeChatMiniProgramOptionsFactory MiniProgramOptionsFactory { get; } protected IDistributedCache SecurityTokenCache { get; } @@ -35,7 +39,8 @@ namespace LINGYUN.Abp.Account IIdentityUserRepository userRepository, IUserSecurityCodeSender securityCodeSender, IDistributedCache securityTokenCache, - AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory) + AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory, + IdentitySecurityLogManager identitySecurityLogManager) { TotpService = totpService; UserRepository = userRepository; @@ -43,6 +48,7 @@ namespace LINGYUN.Abp.Account SecurityCodeSender = securityCodeSender; SecurityTokenCache = securityTokenCache; MiniProgramOptionsFactory = miniProgramOptionsFactory; + IdentitySecurityLogManager = identitySecurityLogManager; } public virtual async Task RegisterAsync(WeChatRegisterDto input) @@ -82,6 +88,15 @@ namespace LINGYUN.Abp.Account var userLogin = new UserLoginInfo(AbpWeChatMiniProgramConsts.ProviderName, wehchatOpenId.OpenId, AbpWeChatGlobalConsts.DisplayName); (await UserManager.AddLoginAsync(user, userLogin)).CheckErrors(); + await IdentitySecurityLogManager.SaveAsync( + new IdentitySecurityLogContext + { + Action = "WeChatRegister", + ClientId = await FindClientIdAsync(), + Identity = "Account", + UserName = user.UserName + }); + await CurrentUnitOfWork.SaveChangesAsync(); } @@ -155,6 +170,15 @@ namespace LINGYUN.Abp.Account await SecurityTokenCache.RemoveAsync(securityTokenCacheKey); + await IdentitySecurityLogManager.SaveAsync( + new IdentitySecurityLogContext + { + Action = "PhoneNumberRegister", + ClientId = await FindClientIdAsync(), + Identity = "Account", + UserName = user.UserName + }); + await CurrentUnitOfWork.SaveChangesAsync(); return; @@ -231,6 +255,15 @@ namespace LINGYUN.Abp.Account // 移除缓存项 await SecurityTokenCache.RemoveAsync(securityTokenCacheKey); + await IdentitySecurityLogManager.SaveAsync( + new IdentitySecurityLogContext + { + Action = "ResetPassword", + ClientId = await FindClientIdAsync(), + Identity = "Account", + UserName = user.UserName + }); + await CurrentUnitOfWork.SaveChangesAsync(); } @@ -290,6 +323,13 @@ namespace LINGYUN.Abp.Account } } + protected virtual Task FindClientIdAsync() + { + var client = LazyServiceProvider.LazyGetRequiredService(); + + return Task.FromResult(client.Id); + } + private void ThowIfInvalidEmailAddress(string inputEmail) { if (!inputEmail.IsNullOrWhiteSpace() && diff --git a/aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs b/aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs index e223617e6..03d051e3f 100644 --- a/aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs +++ b/aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs @@ -20,42 +20,42 @@ namespace LINGYUN.Abp.Account [HttpPost] [Route("wechat/register")] - public virtual async Task RegisterAsync(WeChatRegisterDto input) + public async virtual Task RegisterAsync(WeChatRegisterDto input) { await AccountAppService.RegisterAsync(input); } [HttpPost] [Route("phone/register")] - public virtual async Task RegisterAsync(PhoneRegisterDto input) + public async virtual Task RegisterAsync(PhoneRegisterDto input) { await AccountAppService.RegisterAsync(input); } [HttpPut] [Route("phone/reset-password")] - public virtual async Task ResetPasswordAsync(PhoneResetPasswordDto input) + public async virtual Task ResetPasswordAsync(PhoneResetPasswordDto input) { await AccountAppService.ResetPasswordAsync(input); } [HttpPost] [Route("phone/send-signin-code")] - public virtual async Task SendPhoneSigninCodeAsync(SendPhoneSigninCodeDto input) + public async virtual Task SendPhoneSigninCodeAsync(SendPhoneSigninCodeDto input) { await AccountAppService.SendPhoneSigninCodeAsync(input); } [HttpPost] [Route("phone/send-register-code")] - public virtual async Task SendPhoneRegisterCodeAsync(SendPhoneRegisterCodeDto input) + public async virtual Task SendPhoneRegisterCodeAsync(SendPhoneRegisterCodeDto input) { await AccountAppService.SendPhoneRegisterCodeAsync(input); } [HttpPost] [Route("phone/send-password-reset-code")] - public virtual async Task SendPhoneResetPasswordCodeAsync(SendPhoneResetPasswordCodeDto input) + public async virtual Task SendPhoneResetPasswordCodeAsync(SendPhoneResetPasswordCodeDto input) { await AccountAppService.SendPhoneResetPasswordCodeAsync(input); } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserSetPasswordInput.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserSetPasswordInput.cs new file mode 100644 index 000000000..8fcb47e69 --- /dev/null +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserSetPasswordInput.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using Volo.Abp.Auditing; +using Volo.Abp.Identity; +using Volo.Abp.Validation; + +namespace LINGYUN.Abp.Identity; + +public class IdentityUserSetPasswordInput +{ + [Required] + [DisableAuditing] + [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))] + public string Password { get; set; } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs index 3de5fb423..682e2653d 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs @@ -44,8 +44,7 @@ namespace LINGYUN.Abp.Identity /// /// /// - /// TODO: 移除api,改为重置用户密码 - // Task ChangePasswordAsync(Guid id, ChangePasswordInput input); + Task ChangePasswordAsync(Guid id, IdentityUserSetPasswordInput input); /// /// 锁定 /// diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissionDefinitionProvider.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissionDefinitionProvider.cs index 2901e661b..a4df4cab1 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissionDefinitionProvider.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissionDefinitionProvider.cs @@ -15,6 +15,7 @@ namespace LINGYUN.Abp.Identity var userPermission = identityGroup.GetPermissionOrNull(Volo.Abp.Identity.IdentityPermissions.Users.Default); if (userPermission != null) { + userPermission.AddChild(IdentityPermissions.Users.ResetPassword, L("Permission:ResetPassword")); userPermission.AddChild(IdentityPermissions.Users.ManageClaims, L("Permission:ManageClaims")); userPermission.AddChild(IdentityPermissions.Users.ManageOrganizationUnits, L("Permission:ManageOrganizationUnits")); } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissions.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissions.cs index af54cac5f..9c4ea91cd 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissions.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissions.cs @@ -12,6 +12,7 @@ namespace LINGYUN.Abp.Identity public static class Users { + public const string ResetPassword = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ResetPassword"; public const string ManageClaims = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ManageClaims"; public const string ManageOrganizationUnits = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ManageOrganizationUnits"; } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs index 06c85eade..d089541eb 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; -using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Volo.Abp; @@ -107,6 +106,18 @@ namespace LINGYUN.Abp.Identity #endregion + [Authorize(IdentityPermissions.Users.ResetPassword)] + public async virtual Task ChangePasswordAsync(Guid id, IdentityUserSetPasswordInput input) + { + var user = await GetUserAsync(id); + + var token = await UserManager.GeneratePasswordResetTokenAsync(user); + + (await UserManager.ResetPasswordAsync(user, token, input.Password)).CheckErrors(); + + await CurrentUnitOfWork.SaveChangesAsync(); + } + [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)] public virtual async Task ChangeTwoFactorEnabledAsync(Guid id, TwoFactorEnabledDto input) { diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json index 8766016cf..bce5b9dc3 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json @@ -2,6 +2,7 @@ "culture": "en", "texts": { "Permission:OrganizationUnitManagement": "Organization unit management", + "Permission:ResetPassword": "Reset Password", "Permission:ManageRoles": "Management roles", "Permission:ManageUsers": "Management users", "Permission:ManageClaims": "Management claims", diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json index e641f79ff..e332ca4a2 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json @@ -2,6 +2,7 @@ "culture": "zh-Hans", "texts": { "Permission:OrganizationUnitManagement": "组织机构管理", + "Permission:ResetPassword": "重置密码", "Permission:ManageRoles": "管理角色", "Permission:ManageUsers": "管理用户", "Permission:ManageClaims": "管理声明", diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs index 258b5a172..a69425434 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs @@ -78,6 +78,12 @@ namespace LINGYUN.Abp.Identity #endregion + [HttpPut] + [Route("change-password")] + public async virtual Task ChangePasswordAsync(Guid id, IdentityUserSetPasswordInput input) + { + await UserAppService.ChangePasswordAsync(id, input); + } [HttpPut] [Route("change-two-factor")]