Browse Source

Add `RemoveClaimFromAllUsers` to `IIdentityUserRepository`.

pull/19853/head
maliming 2 years ago
parent
commit
54e52eaafd
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 6
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs
  2. 21
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs
  3. 15
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
  4. 14
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  5. 44
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs

6
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs

@ -44,6 +44,12 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
Task RemoveClaimFromAllUsers(
string claimType,
bool autoSave = false,
CancellationToken cancellationToken = default
);
Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync( Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName, string normalizedRoleName,
bool includeDetails = false, bool includeDetails = false,

21
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs

@ -1,4 +1,6 @@
using System.Threading.Tasks; using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
namespace Volo.Abp.Identity; namespace Volo.Abp.Identity;
@ -6,10 +8,12 @@ namespace Volo.Abp.Identity;
public class IdentityClaimTypeManager : DomainService public class IdentityClaimTypeManager : DomainService
{ {
protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; } protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; }
protected IIdentityUserRepository IdentityUserRepository { get; }
public IdentityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository) public IdentityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository, IIdentityUserRepository identityUserRepository)
{ {
IdentityClaimTypeRepository = identityClaimTypeRepository; IdentityClaimTypeRepository = identityClaimTypeRepository;
IdentityUserRepository = identityUserRepository;
} }
public virtual async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType) public virtual async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType)
@ -37,4 +41,17 @@ public class IdentityClaimTypeManager : DomainService
return await IdentityClaimTypeRepository.UpdateAsync(claimType); return await IdentityClaimTypeRepository.UpdateAsync(claimType);
} }
public virtual async Task DeleteAsync(Guid id)
{
var claimType = await IdentityClaimTypeRepository.GetAsync(id);
if (claimType.IsStatic)
{
throw new AbpException($"Can not delete a static ClaimType.");
}
//Remove claim of this type from all users
await IdentityUserRepository.RemoveClaimFromAllUsers(claimType.Name);
await IdentityClaimTypeRepository.DeleteAsync(id);
}
} }

15
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs

@ -145,6 +145,21 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task RemoveClaimFromAllUsers(string claimType, bool autoSave, CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync();
var userClaims = await dbContext.Set<IdentityUserClaim>().Where(uc => uc.ClaimType == claimType).ToListAsync(cancellationToken: cancellationToken);
if (userClaims.Any())
{
(await GetDbContextAsync()).Set<IdentityUserClaim>().RemoveRange(userClaims);
}
if (autoSave)
{
await dbContext.SaveChangesAsync(GetCancellationToken(cancellationToken));
}
}
public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync( public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName, string normalizedRoleName,
bool includeDetails = false, bool includeDetails = false,

14
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs

@ -108,6 +108,20 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task RemoveClaimFromAllUsers(string claimType, bool autoSave, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.Claims.Any(c => c.ClaimType == claimType))
.ToListAsync(GetCancellationToken(cancellationToken));
foreach (var user in users)
{
user.Claims.RemoveAll(c => c.ClaimType == claimType);
}
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}
public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync( public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName, string normalizedRoleName,
bool includeDetails = false, bool includeDetails = false,

44
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs

@ -1,12 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Shouldly; using Shouldly;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Xunit; using Xunit;
namespace Volo.Abp.Identity; namespace Volo.Abp.Identity;
@ -16,11 +18,19 @@ public abstract class IdentityClaimTypeRepository_Tests<TStartupModule> : AbpIde
{ {
protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } protected IIdentityClaimTypeRepository ClaimTypeRepository { get; }
protected IGuidGenerator GuidGenerator { get; } protected IGuidGenerator GuidGenerator { get; }
protected IUnitOfWorkManager UnitOfWorkManager { get; }
protected IIdentityUserRepository UserRepository { get; }
protected IdentityUserManager IdentityUserManager { get; }
protected IdentityTestData IdentityTestData { get; }
public IdentityClaimTypeRepository_Tests() public IdentityClaimTypeRepository_Tests()
{ {
ClaimTypeRepository = ServiceProvider.GetRequiredService<IIdentityClaimTypeRepository>(); ClaimTypeRepository = ServiceProvider.GetRequiredService<IIdentityClaimTypeRepository>();
GuidGenerator = ServiceProvider.GetRequiredService<IGuidGenerator>(); GuidGenerator = ServiceProvider.GetRequiredService<IGuidGenerator>();
UnitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
IdentityUserManager = ServiceProvider.GetRequiredService<IdentityUserManager>();
UserRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
IdentityTestData = ServiceProvider.GetRequiredService<IdentityTestData>();
} }
[Fact] [Fact]
@ -50,4 +60,38 @@ public abstract class IdentityClaimTypeRepository_Tests<TStartupModule> : AbpIde
result.Count.ShouldBe(2); result.Count.ShouldBe(2);
} }
[Fact]
public async Task DeleteAsync()
{
var ageClaim = await ClaimTypeRepository.FindAsync(IdentityTestData.AgeClaimId);
ageClaim.ShouldNotBeNull();
using (var uow = UnitOfWorkManager.Begin())
{
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");
await uow.CompleteAsync();
}
await ClaimTypeRepository.DeleteAsync(ageClaim.Id);
await UserRepository.RemoveClaimFromAllUsers(ageClaim.Name);
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");
await uow.CompleteAsync();
}
}
} }

Loading…
Cancel
Save