From b93ce73039f8a08739081e1ec5f4cdfb06505183 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:58:48 +0800 Subject: [PATCH] feat(identity): profile interface added setting user claim --- .../Dto/IdentityUserClaimCreateDto.cs | 23 ++--- .../Dto/IdentityUserClaimCreateOrUpdateDto.cs | 16 ++++ .../Identity/Dto/IdentityUserClaimSetDto.cs | 6 ++ .../Dto/IdentityUserClaimUpdateDto.cs | 24 +++-- .../Abp/Identity/IMyProfileAppService.cs | 66 +++++++------ .../Abp/Identity/MyProfileAppService.cs | 24 +++++ .../Abp/Identity/MyProfileController.cs | 96 ++++++++++--------- 7 files changed, 151 insertions(+), 104 deletions(-) create mode 100644 aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateOrUpdateDto.cs create mode 100644 aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimSetDto.cs diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateDto.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateDto.cs index e9f2f1498..c8ecd8204 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateDto.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateDto.cs @@ -1,17 +1,6 @@ -using System.ComponentModel.DataAnnotations; -using Volo.Abp.Identity; -using Volo.Abp.Validation; - -namespace LINGYUN.Abp.Identity -{ - public class IdentityUserClaimCreateDto - { - [Required] - [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimTypeLength))] - public string ClaimType { get; set; } - - [Required] - [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimValueLength))] - public string ClaimValue { get; set; } - } -} +namespace LINGYUN.Abp.Identity +{ + public class IdentityUserClaimCreateDto: IdentityUserClaimCreateOrUpdateDto + { + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateOrUpdateDto.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateOrUpdateDto.cs new file mode 100644 index 000000000..363bed550 --- /dev/null +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimCreateOrUpdateDto.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using Volo.Abp.Identity; +using Volo.Abp.Validation; + +namespace LINGYUN.Abp.Identity +{ + public abstract class IdentityUserClaimCreateOrUpdateDto + { + [Required] + [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimTypeLength))] + public string ClaimType { get; set; } + + [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimValueLength))] + public string ClaimValue { get; set; } + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimSetDto.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimSetDto.cs new file mode 100644 index 000000000..de64b2afe --- /dev/null +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimSetDto.cs @@ -0,0 +1,6 @@ +namespace LINGYUN.Abp.Identity +{ + public class IdentityUserClaimSetDto : IdentityUserClaimCreateDto + { + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimUpdateDto.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimUpdateDto.cs index a15db9ddc..78125b595 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimUpdateDto.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/Dto/IdentityUserClaimUpdateDto.cs @@ -1,13 +1,11 @@ -using System.ComponentModel.DataAnnotations; -using Volo.Abp.Identity; -using Volo.Abp.Validation; - -namespace LINGYUN.Abp.Identity -{ - public class IdentityUserClaimUpdateDto : IdentityUserClaimCreateDto - { - [Required] - [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimValueLength))] - public string NewClaimValue { get; set; } - } -} +using Volo.Abp.Identity; +using Volo.Abp.Validation; + +namespace LINGYUN.Abp.Identity +{ + public class IdentityUserClaimUpdateDto : IdentityUserClaimCreateOrUpdateDto + { + [DynamicMaxLength(typeof(IdentityUserClaimConsts), nameof(IdentityUserClaimConsts.MaxClaimValueLength))] + public string NewClaimValue { get; set; } + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IMyProfileAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IMyProfileAppService.cs index 16f5c65ed..35a514e10 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IMyProfileAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application.Contracts/LINGYUN/Abp/Identity/IMyProfileAppService.cs @@ -1,30 +1,36 @@ -using System.Threading.Tasks; -using Volo.Abp.Application.Services; - -namespace LINGYUN.Abp.Identity -{ - public interface IMyProfileAppService : IApplicationService - { - /// - /// 改变二次认证 - /// - /// - /// - Task ChangeTwoFactorEnabledAsync(ChangeTwoFactorEnabledDto input); - /// - /// 发送改变手机号验证码 - /// - /// - /// - Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input); - /// - /// 改变手机绑定 - /// - /// - /// - /// - /// 需二次认证,主要是为了无法用到重定向页面修改相关信息的地方(点名微信小程序) - /// - Task ChangePhoneNumberAsync(ChangePhoneNumberDto input); - } -} +using System.Threading.Tasks; +using Volo.Abp.Application.Services; + +namespace LINGYUN.Abp.Identity +{ + public interface IMyProfileAppService : IApplicationService + { + /// + /// 设置声明 + /// + /// + /// + Task SetClaimAsync(IdentityUserClaimSetDto input); + /// + /// 改变二次认证 + /// + /// + /// + Task ChangeTwoFactorEnabledAsync(ChangeTwoFactorEnabledDto input); + /// + /// 发送改变手机号验证码 + /// + /// + /// + Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input); + /// + /// 改变手机绑定 + /// + /// + /// + /// + /// 需二次认证,主要是为了无法用到重定向页面修改相关信息的地方(点名微信小程序) + /// + Task ChangePhoneNumberAsync(ChangePhoneNumberDto input); + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs index a411f2953..6693350f0 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; using System; +using System.Security.Claims; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Caching; @@ -35,6 +36,29 @@ namespace LINGYUN.Abp.Identity SecurityTokenCache = securityTokenCache; } + public virtual async Task SetClaimAsync(IdentityUserClaimSetDto input) + { + await IdentityOptions.SetAsync(); + var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); + + var newClaim = new Claim(input.ClaimType, input.ClaimValue); + var currentClaim = user.FindClaim(newClaim); + if (currentClaim != null) + { + // Replace With Claim Value Empty? + // (await UserManager.ReplaceClaimAsync(user, currentClaim.ToClaim(), newClaim)).CheckErrors(); + user.ReplaceClaim(currentClaim.ToClaim(), newClaim); + } + else + { + // (await UserManager.AddClaimAsync(user, newClaim)).CheckErrors(); + user.AddClaim(GuidGenerator, newClaim); + } + (await UserManager.UpdateAsync(user)).CheckErrors(); + + await CurrentUnitOfWork.CompleteAsync(); + } + public virtual async Task ChangeTwoFactorEnabledAsync(ChangeTwoFactorEnabledDto input) { // Removed See: https://github.com/abpframework/abp/pull/7719 diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/MyProfileController.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/MyProfileController.cs index 0a8238426..4f4970500 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/MyProfileController.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.HttpApi/LINGYUN/Abp/Identity/MyProfileController.cs @@ -1,44 +1,52 @@ -using Microsoft.AspNetCore.Mvc; -using System.Threading.Tasks; -using Volo.Abp; -using Volo.Abp.AspNetCore.Mvc; -using Volo.Abp.Identity; - -namespace LINGYUN.Abp.Identity -{ - [RemoteService(Name = IdentityRemoteServiceConsts.RemoteServiceName)] - [Area("identity")] - [ControllerName("Profile")] - [Route("/api/identity/my-profile")] - public class MyProfileController : AbpController, IMyProfileAppService - { - protected IMyProfileAppService MyProfileAppService { get; } - - public MyProfileController( - IMyProfileAppService myProfileAppService) - { - MyProfileAppService = myProfileAppService; - } - - [HttpPut] - [Route("change-two-factor")] - public virtual async Task ChangeTwoFactorEnabledAsync(ChangeTwoFactorEnabledDto input) - { - await MyProfileAppService.ChangeTwoFactorEnabledAsync(input); - } - - [HttpPut] - [Route("send-phone-number-change-code")] - public virtual async Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input) - { - await MyProfileAppService.SendChangePhoneNumberCodeAsync(input); - } - - [HttpPut] - [Route("change-phone-number")] - public virtual async Task ChangePhoneNumberAsync(ChangePhoneNumberDto input) - { - await MyProfileAppService.ChangePhoneNumberAsync(input); - } - } -} +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.Identity; + +namespace LINGYUN.Abp.Identity +{ + [RemoteService(Name = IdentityRemoteServiceConsts.RemoteServiceName)] + [Area("identity")] + [ControllerName("Profile")] + [Route("/api/identity/my-profile")] + public class MyProfileController : AbpController, IMyProfileAppService + { + protected IMyProfileAppService MyProfileAppService { get; } + + public MyProfileController( + IMyProfileAppService myProfileAppService) + { + MyProfileAppService = myProfileAppService; + } + + [HttpPut] + [Route("/claims")] + public virtual async Task SetClaimAsync(IdentityUserClaimSetDto input) + { + await MyProfileAppService.SetClaimAsync(input); + } + + + [HttpPut] + [Route("change-two-factor")] + public virtual async Task ChangeTwoFactorEnabledAsync(ChangeTwoFactorEnabledDto input) + { + await MyProfileAppService.ChangeTwoFactorEnabledAsync(input); + } + + [HttpPut] + [Route("send-phone-number-change-code")] + public virtual async Task SendChangePhoneNumberCodeAsync(SendChangePhoneNumberCodeDto input) + { + await MyProfileAppService.SendChangePhoneNumberCodeAsync(input); + } + + [HttpPut] + [Route("change-phone-number")] + public virtual async Task ChangePhoneNumberAsync(ChangePhoneNumberDto input) + { + await MyProfileAppService.ChangePhoneNumberAsync(input); + } + } +}