Browse Source

feat: 增加重置用户密码api

pull/613/head
cKey 4 years ago
parent
commit
0114b31fe0
  1. 42
      aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs
  2. 12
      aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs
  3. 14
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserSetPasswordInput.cs
  4. 3
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs
  5. 1
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissionDefinitionProvider.cs
  6. 1
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IdentityPermissions.cs
  7. 13
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs
  8. 1
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json
  9. 1
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json
  10. 6
      aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs

42
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 LINGYUN.Abp.WeChat.OpenId;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Account;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.Clients;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.Settings; using Volo.Abp.Settings;
using Volo.Abp.Validation; using Volo.Abp.Validation;
@ -26,6 +29,7 @@ namespace LINGYUN.Abp.Account
protected IIdentityUserRepository UserRepository { get; } protected IIdentityUserRepository UserRepository { get; }
protected IUserSecurityCodeSender SecurityCodeSender { get; } protected IUserSecurityCodeSender SecurityCodeSender { get; }
protected IWeChatOpenIdFinder WeChatOpenIdFinder { get; } protected IWeChatOpenIdFinder WeChatOpenIdFinder { get; }
protected IdentitySecurityLogManager IdentitySecurityLogManager { get; }
protected AbpWeChatMiniProgramOptionsFactory MiniProgramOptionsFactory { get; } protected AbpWeChatMiniProgramOptionsFactory MiniProgramOptionsFactory { get; }
protected IDistributedCache<SmsSecurityTokenCacheItem> SecurityTokenCache { get; } protected IDistributedCache<SmsSecurityTokenCacheItem> SecurityTokenCache { get; }
@ -35,7 +39,8 @@ namespace LINGYUN.Abp.Account
IIdentityUserRepository userRepository, IIdentityUserRepository userRepository,
IUserSecurityCodeSender securityCodeSender, IUserSecurityCodeSender securityCodeSender,
IDistributedCache<SmsSecurityTokenCacheItem> securityTokenCache, IDistributedCache<SmsSecurityTokenCacheItem> securityTokenCache,
AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory) AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory,
IdentitySecurityLogManager identitySecurityLogManager)
{ {
TotpService = totpService; TotpService = totpService;
UserRepository = userRepository; UserRepository = userRepository;
@ -43,6 +48,7 @@ namespace LINGYUN.Abp.Account
SecurityCodeSender = securityCodeSender; SecurityCodeSender = securityCodeSender;
SecurityTokenCache = securityTokenCache; SecurityTokenCache = securityTokenCache;
MiniProgramOptionsFactory = miniProgramOptionsFactory; MiniProgramOptionsFactory = miniProgramOptionsFactory;
IdentitySecurityLogManager = identitySecurityLogManager;
} }
public virtual async Task RegisterAsync(WeChatRegisterDto input) 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); var userLogin = new UserLoginInfo(AbpWeChatMiniProgramConsts.ProviderName, wehchatOpenId.OpenId, AbpWeChatGlobalConsts.DisplayName);
(await UserManager.AddLoginAsync(user, userLogin)).CheckErrors(); (await UserManager.AddLoginAsync(user, userLogin)).CheckErrors();
await IdentitySecurityLogManager.SaveAsync(
new IdentitySecurityLogContext
{
Action = "WeChatRegister",
ClientId = await FindClientIdAsync(),
Identity = "Account",
UserName = user.UserName
});
await CurrentUnitOfWork.SaveChangesAsync(); await CurrentUnitOfWork.SaveChangesAsync();
} }
@ -155,6 +170,15 @@ namespace LINGYUN.Abp.Account
await SecurityTokenCache.RemoveAsync(securityTokenCacheKey); await SecurityTokenCache.RemoveAsync(securityTokenCacheKey);
await IdentitySecurityLogManager.SaveAsync(
new IdentitySecurityLogContext
{
Action = "PhoneNumberRegister",
ClientId = await FindClientIdAsync(),
Identity = "Account",
UserName = user.UserName
});
await CurrentUnitOfWork.SaveChangesAsync(); await CurrentUnitOfWork.SaveChangesAsync();
return; return;
@ -231,6 +255,15 @@ namespace LINGYUN.Abp.Account
// 移除缓存项 // 移除缓存项
await SecurityTokenCache.RemoveAsync(securityTokenCacheKey); await SecurityTokenCache.RemoveAsync(securityTokenCacheKey);
await IdentitySecurityLogManager.SaveAsync(
new IdentitySecurityLogContext
{
Action = "ResetPassword",
ClientId = await FindClientIdAsync(),
Identity = "Account",
UserName = user.UserName
});
await CurrentUnitOfWork.SaveChangesAsync(); await CurrentUnitOfWork.SaveChangesAsync();
} }
@ -290,6 +323,13 @@ namespace LINGYUN.Abp.Account
} }
} }
protected virtual Task<string> FindClientIdAsync()
{
var client = LazyServiceProvider.LazyGetRequiredService<ICurrentClient>();
return Task.FromResult(client.Id);
}
private void ThowIfInvalidEmailAddress(string inputEmail) private void ThowIfInvalidEmailAddress(string inputEmail)
{ {
if (!inputEmail.IsNullOrWhiteSpace() && if (!inputEmail.IsNullOrWhiteSpace() &&

12
aspnet-core/modules/account/LINGYUN.Abp.Account.HttpApi/LINGYUN/Abp/Account/AccountController.cs

@ -20,42 +20,42 @@ namespace LINGYUN.Abp.Account
[HttpPost] [HttpPost]
[Route("wechat/register")] [Route("wechat/register")]
public virtual async Task RegisterAsync(WeChatRegisterDto input) public async virtual Task RegisterAsync(WeChatRegisterDto input)
{ {
await AccountAppService.RegisterAsync(input); await AccountAppService.RegisterAsync(input);
} }
[HttpPost] [HttpPost]
[Route("phone/register")] [Route("phone/register")]
public virtual async Task RegisterAsync(PhoneRegisterDto input) public async virtual Task RegisterAsync(PhoneRegisterDto input)
{ {
await AccountAppService.RegisterAsync(input); await AccountAppService.RegisterAsync(input);
} }
[HttpPut] [HttpPut]
[Route("phone/reset-password")] [Route("phone/reset-password")]
public virtual async Task ResetPasswordAsync(PhoneResetPasswordDto input) public async virtual Task ResetPasswordAsync(PhoneResetPasswordDto input)
{ {
await AccountAppService.ResetPasswordAsync(input); await AccountAppService.ResetPasswordAsync(input);
} }
[HttpPost] [HttpPost]
[Route("phone/send-signin-code")] [Route("phone/send-signin-code")]
public virtual async Task SendPhoneSigninCodeAsync(SendPhoneSigninCodeDto input) public async virtual Task SendPhoneSigninCodeAsync(SendPhoneSigninCodeDto input)
{ {
await AccountAppService.SendPhoneSigninCodeAsync(input); await AccountAppService.SendPhoneSigninCodeAsync(input);
} }
[HttpPost] [HttpPost]
[Route("phone/send-register-code")] [Route("phone/send-register-code")]
public virtual async Task SendPhoneRegisterCodeAsync(SendPhoneRegisterCodeDto input) public async virtual Task SendPhoneRegisterCodeAsync(SendPhoneRegisterCodeDto input)
{ {
await AccountAppService.SendPhoneRegisterCodeAsync(input); await AccountAppService.SendPhoneRegisterCodeAsync(input);
} }
[HttpPost] [HttpPost]
[Route("phone/send-password-reset-code")] [Route("phone/send-password-reset-code")]
public virtual async Task SendPhoneResetPasswordCodeAsync(SendPhoneResetPasswordCodeDto input) public async virtual Task SendPhoneResetPasswordCodeAsync(SendPhoneResetPasswordCodeDto input)
{ {
await AccountAppService.SendPhoneResetPasswordCodeAsync(input); await AccountAppService.SendPhoneResetPasswordCodeAsync(input);
} }

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

3
aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IIdentityUserAppService.cs

@ -44,8 +44,7 @@ namespace LINGYUN.Abp.Identity
/// <param name="id"></param> /// <param name="id"></param>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
/// TODO: 移除api,改为重置用户密码 Task ChangePasswordAsync(Guid id, IdentityUserSetPasswordInput input);
// Task ChangePasswordAsync(Guid id, ChangePasswordInput input);
/// <summary> /// <summary>
/// 锁定 /// 锁定
/// </summary> /// </summary>

1
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); var userPermission = identityGroup.GetPermissionOrNull(Volo.Abp.Identity.IdentityPermissions.Users.Default);
if (userPermission != null) if (userPermission != null)
{ {
userPermission.AddChild(IdentityPermissions.Users.ResetPassword, L("Permission:ResetPassword"));
userPermission.AddChild(IdentityPermissions.Users.ManageClaims, L("Permission:ManageClaims")); userPermission.AddChild(IdentityPermissions.Users.ManageClaims, L("Permission:ManageClaims"));
userPermission.AddChild(IdentityPermissions.Users.ManageOrganizationUnits, L("Permission:ManageOrganizationUnits")); userPermission.AddChild(IdentityPermissions.Users.ManageOrganizationUnits, L("Permission:ManageOrganizationUnits"));
} }

1
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 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 ManageClaims = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ManageClaims";
public const string ManageOrganizationUnits = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ManageOrganizationUnits"; public const string ManageOrganizationUnits = Volo.Abp.Identity.IdentityPermissions.Users.Default + ".ManageOrganizationUnits";
} }

13
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 Microsoft.Extensions.Options;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp; using Volo.Abp;
@ -107,6 +106,18 @@ namespace LINGYUN.Abp.Identity
#endregion #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)] [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)]
public virtual async Task ChangeTwoFactorEnabledAsync(Guid id, TwoFactorEnabledDto input) public virtual async Task ChangeTwoFactorEnabledAsync(Guid id, TwoFactorEnabledDto input)
{ {

1
aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/en.json

@ -2,6 +2,7 @@
"culture": "en", "culture": "en",
"texts": { "texts": {
"Permission:OrganizationUnitManagement": "Organization unit management", "Permission:OrganizationUnitManagement": "Organization unit management",
"Permission:ResetPassword": "Reset Password",
"Permission:ManageRoles": "Management roles", "Permission:ManageRoles": "Management roles",
"Permission:ManageUsers": "Management users", "Permission:ManageUsers": "Management users",
"Permission:ManageClaims": "Management claims", "Permission:ManageClaims": "Management claims",

1
aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain.Shared/LINGYUN/Abp/Identity/Localization/zh-Hans.json

@ -2,6 +2,7 @@
"culture": "zh-Hans", "culture": "zh-Hans",
"texts": { "texts": {
"Permission:OrganizationUnitManagement": "组织机构管理", "Permission:OrganizationUnitManagement": "组织机构管理",
"Permission:ResetPassword": "重置密码",
"Permission:ManageRoles": "管理角色", "Permission:ManageRoles": "管理角色",
"Permission:ManageUsers": "管理用户", "Permission:ManageUsers": "管理用户",
"Permission:ManageClaims": "管理声明", "Permission:ManageClaims": "管理声明",

6
aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/IdentityUserController.cs

@ -78,6 +78,12 @@ namespace LINGYUN.Abp.Identity
#endregion #endregion
[HttpPut]
[Route("change-password")]
public async virtual Task ChangePasswordAsync(Guid id, IdentityUserSetPasswordInput input)
{
await UserAppService.ChangePasswordAsync(id, input);
}
[HttpPut] [HttpPut]
[Route("change-two-factor")] [Route("change-two-factor")]

Loading…
Cancel
Save