From f253c336a758fb76bd0645d7e248ddb1340123a3 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 17 May 2024 20:21:47 +0800 Subject: [PATCH] Add `RemoveClaimFromAllRolesAsync` method to `IIdentityRoleRepository`. --- .../Abp/Identity/IIdentityRoleRepository.cs | 8 ++++++- .../Abp/Identity/IdentityClaimTypeManager.cs | 12 +++++++--- .../EfCoreIdentityRoleRepository.cs | 24 +++++++++++++++---- .../MongoDB/MongoIdentityRoleRepository.cs | 18 ++++++++++++-- .../IdentityClaimTypeRepository_Tests.cs | 23 ++++++++++++++---- 5 files changed, 69 insertions(+), 16 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs index d5f51ae71b..a9eefe019e 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs @@ -22,7 +22,7 @@ public interface IIdentityRoleRepository : IBasicRepository bool includeDetails = false, CancellationToken cancellationToken = default ); - + Task> GetListAsync( string sorting = null, int maxResultCount = int.MaxValue, @@ -45,4 +45,10 @@ public interface IIdentityRoleRepository : IBasicRepository string filter = null, CancellationToken cancellationToken = default ); + + Task RemoveClaimFromAllRolesAsync( + string claimType, + bool autoSave = false, + CancellationToken cancellationToken = default + ); } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs index 0ccca2d458..bc8ab16d9f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs @@ -9,11 +9,16 @@ public class IdentityClaimTypeManager : DomainService { protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; } protected IIdentityUserRepository IdentityUserRepository { get; } + protected IIdentityRoleRepository IdentityRoleRepository { get; } - public IdentityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository, IIdentityUserRepository identityUserRepository) + public IdentityClaimTypeManager( + IIdentityClaimTypeRepository identityClaimTypeRepository, + IIdentityUserRepository identityUserRepository, + IIdentityRoleRepository identityRoleRepository) { IdentityClaimTypeRepository = identityClaimTypeRepository; IdentityUserRepository = identityUserRepository; + IdentityRoleRepository = identityRoleRepository; } public virtual async Task CreateAsync(IdentityClaimType claimType) @@ -38,7 +43,6 @@ public class IdentityClaimTypeManager : DomainService throw new AbpException($"Can not update a static ClaimType."); } - return await IdentityClaimTypeRepository.UpdateAsync(claimType); } @@ -50,8 +54,10 @@ public class IdentityClaimTypeManager : DomainService throw new AbpException($"Can not delete a static ClaimType."); } - //Remove claim of this type from all users + //Remove claim of this type from all users and roles await IdentityUserRepository.RemoveClaimFromAllUsersAsync(claimType.Name); + await IdentityRoleRepository.RemoveClaimFromAllRolesAsync(claimType.Name); + await IdentityClaimTypeRepository.DeleteAsync(id); } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index 57843adad0..293b786eca 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -29,26 +29,26 @@ public class EfCoreIdentityRoleRepository : EfCoreRepository> GetListWithUserCountAsync( - string sorting = null, + string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, string filter = null, - bool includeDetails = false, + bool includeDetails = false, CancellationToken cancellationToken = default) { var roles = await GetListInternalAsync(sorting, maxResultCount, skipCount, filter, includeDetails, cancellationToken: cancellationToken); - + var roleIds = roles.Select(x => x.Id).ToList(); var userCount = await (await GetDbContextAsync()).Set() .Where(userRole => roleIds.Contains(userRole.RoleId)) .GroupBy(userRole => userRole.RoleId) - .Select(x => new + .Select(x => new { RoleId = x.Key, Count = x.Count() }) .ToListAsync(GetCancellationToken(cancellationToken)); - + return roles.Select(role => new IdentityRoleWithUserCount(role, userCount.FirstOrDefault(x => x.RoleId == role.Id)?.Count ?? 0)).ToList(); } @@ -92,6 +92,20 @@ public class EfCoreIdentityRoleRepository : EfCoreRepository().Where(uc => uc.ClaimType == claimType).ToListAsync(cancellationToken: cancellationToken); + if (roleClaims.Any()) + { + (await GetDbContextAsync()).Set().RemoveRange(roleClaims); + if (autoSave) + { + await dbContext.SaveChangesAsync(GetCancellationToken(cancellationToken)); + } + } + } + protected virtual async Task> GetListInternalAsync( string sorting = null, int maxResultCount = int.MaxValue, diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs index e94bacb92c..ca67864975 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs @@ -42,13 +42,13 @@ public class MongoIdentityRoleRepository : MongoDbRepository user.Roles.Any(role => roleIds.Contains(role.RoleId))) .SelectMany(user => user.Roles) .GroupBy(userRole => userRole.RoleId) - .Select(x => new + .Select(x => new { RoleId = x.Key, Count = x.Count() }) .ToListAsync(GetCancellationToken(cancellationToken)); - + return roles.Select(role => new IdentityRoleWithUserCount(role, userCount.FirstOrDefault(x => x.RoleId == role.Id)?.Count ?? 0)).ToList(); } @@ -99,6 +99,20 @@ public class MongoIdentityRoleRepository : MongoDbRepository r.Claims.Any(c => c.ClaimType == claimType)) + .ToListAsync(GetCancellationToken(cancellationToken)); + + foreach (var role in roles) + { + role.Claims.RemoveAll(c => c.ClaimType == claimType); + } + + await UpdateManyAsync(roles, cancellationToken: cancellationToken); + } + protected virtual async Task> GetListInternalAsync( string sorting = null, int maxResultCount = int.MaxValue, diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs index 1fd7f4f227..d8f79b91bf 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs @@ -17,19 +17,25 @@ public abstract class IdentityClaimTypeRepository_Tests : AbpIde where TStartupModule : IAbpModule { protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } + protected IdentityClaimTypeManager IdentityClaimTypeManager { get; } protected IGuidGenerator GuidGenerator { get; } protected IUnitOfWorkManager UnitOfWorkManager { get; } protected IIdentityUserRepository UserRepository { get; } protected IdentityUserManager IdentityUserManager { get; } + protected IIdentityRoleRepository RoleRepository { get; } + protected IdentityRoleManager IdentityRoleManager { get; } protected IdentityTestData IdentityTestData { get; } public IdentityClaimTypeRepository_Tests() { ClaimTypeRepository = ServiceProvider.GetRequiredService(); + IdentityClaimTypeManager = ServiceProvider.GetRequiredService(); GuidGenerator = ServiceProvider.GetRequiredService(); UnitOfWorkManager = ServiceProvider.GetRequiredService(); IdentityUserManager = ServiceProvider.GetRequiredService(); UserRepository = ServiceProvider.GetRequiredService(); + RoleRepository = ServiceProvider.GetRequiredService(); + IdentityRoleManager = ServiceProvider.GetRequiredService(); IdentityTestData = ServiceProvider.GetRequiredService(); } @@ -72,25 +78,32 @@ public abstract class IdentityClaimTypeRepository_Tests : AbpIde var john = await UserRepository.FindAsync(IdentityTestData.UserJohnId); john.ShouldNotBeNull(); await IdentityUserManager.AddClaimAsync(john, new Claim(ageClaim.Name, "18")); - var userClaims = await IdentityUserManager.GetClaimsAsync(john); userClaims.ShouldContain(c => c.Type == ageClaim.Name && c.Value == "18"); + var saleRole = await RoleRepository.FindAsync(IdentityTestData.RoleSaleId); + saleRole.ShouldNotBeNull(); + await IdentityRoleManager.AddClaimAsync(saleRole, new Claim(ageClaim.Name, "18")); + var roleClaims = await IdentityRoleManager.GetClaimsAsync(saleRole); + roleClaims.ShouldContain(c => c.Type == ageClaim.Name && c.Value == "18"); + await uow.CompleteAsync(); } - await ClaimTypeRepository.DeleteAsync(ageClaim.Id); - await UserRepository.RemoveClaimFromAllUsersAsync(ageClaim.Name); + await IdentityClaimTypeManager.DeleteAsync(ageClaim.Id); using (var uow = UnitOfWorkManager.Begin()) { var john = await UserRepository.FindAsync(IdentityTestData.UserJohnId); john.ShouldNotBeNull(); - var userClaims = await IdentityUserManager.GetClaimsAsync(john); - userClaims.ShouldNotContain(c => c.Type == ageClaim.Name && c.Value == "18"); + var saleRole = await RoleRepository.FindAsync(IdentityTestData.RoleSaleId); + saleRole.ShouldNotBeNull(); + var roleClaims = await IdentityRoleManager.GetClaimsAsync(saleRole); + roleClaims.ShouldNotContain(c => c.Type == ageClaim.Name && c.Value == "18"); + await uow.CompleteAsync(); } }