Browse Source

Merge pull request #19853 from abpframework/RemoveClaimFromAllUsers

Add `RemoveClaimFromAllUsersAsync` to `IIdentityUserRepository`.
pull/19863/head
oykuermann 2 years ago
committed by GitHub
parent
commit
6aa243fedf
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs
  2. 6
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs
  3. 29
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityClaimTypeManager.cs
  4. 24
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs
  5. 24
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
  6. 18
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs
  7. 34
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  8. 59
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs

8
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs

@ -22,7 +22,7 @@ public interface IIdentityRoleRepository : IBasicRepository<IdentityRole, Guid>
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
Task<List<IdentityRole>> GetListAsync( Task<List<IdentityRole>> GetListAsync(
string sorting = null, string sorting = null,
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,
@ -45,4 +45,10 @@ public interface IIdentityRoleRepository : IBasicRepository<IdentityRole, Guid>
string filter = null, string filter = null,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
Task RemoveClaimFromAllRolesAsync(
string claimType,
bool autoSave = false,
CancellationToken cancellationToken = default
);
} }

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 RemoveClaimFromAllUsersAsync(
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,

29
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,17 @@ namespace Volo.Abp.Identity;
public class IdentityClaimTypeManager : DomainService public class IdentityClaimTypeManager : DomainService
{ {
protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; } protected IIdentityClaimTypeRepository IdentityClaimTypeRepository { get; }
protected IIdentityUserRepository IdentityUserRepository { get; }
protected IIdentityRoleRepository IdentityRoleRepository { get; }
public IdentityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository) public IdentityClaimTypeManager(
IIdentityClaimTypeRepository identityClaimTypeRepository,
IIdentityUserRepository identityUserRepository,
IIdentityRoleRepository identityRoleRepository)
{ {
IdentityClaimTypeRepository = identityClaimTypeRepository; IdentityClaimTypeRepository = identityClaimTypeRepository;
IdentityUserRepository = identityUserRepository;
IdentityRoleRepository = identityRoleRepository;
} }
public virtual async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType) public virtual async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType)
@ -34,7 +43,21 @@ public class IdentityClaimTypeManager : DomainService
throw new AbpException($"Can not update a static ClaimType."); throw new AbpException($"Can not update a static ClaimType.");
} }
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 and roles
await IdentityUserRepository.RemoveClaimFromAllUsersAsync(claimType.Name);
await IdentityRoleRepository.RemoveClaimFromAllRolesAsync(claimType.Name);
await IdentityClaimTypeRepository.DeleteAsync(id);
}
} }

24
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs

@ -29,26 +29,26 @@ public class EfCoreIdentityRoleRepository : EfCoreRepository<IIdentityDbContext,
} }
public virtual async Task<List<IdentityRoleWithUserCount>> GetListWithUserCountAsync( public virtual async Task<List<IdentityRoleWithUserCount>> GetListWithUserCountAsync(
string sorting = null, string sorting = null,
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,
int skipCount = 0, int skipCount = 0,
string filter = null, string filter = null,
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var roles = await GetListInternalAsync(sorting, maxResultCount, skipCount, filter, includeDetails, cancellationToken: cancellationToken); var roles = await GetListInternalAsync(sorting, maxResultCount, skipCount, filter, includeDetails, cancellationToken: cancellationToken);
var roleIds = roles.Select(x => x.Id).ToList(); var roleIds = roles.Select(x => x.Id).ToList();
var userCount = await (await GetDbContextAsync()).Set<IdentityUserRole>() var userCount = await (await GetDbContextAsync()).Set<IdentityUserRole>()
.Where(userRole => roleIds.Contains(userRole.RoleId)) .Where(userRole => roleIds.Contains(userRole.RoleId))
.GroupBy(userRole => userRole.RoleId) .GroupBy(userRole => userRole.RoleId)
.Select(x => new .Select(x => new
{ {
RoleId = x.Key, RoleId = x.Key,
Count = x.Count() Count = x.Count()
}) })
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
return roles.Select(role => new IdentityRoleWithUserCount(role, userCount.FirstOrDefault(x => x.RoleId == role.Id)?.Count ?? 0)).ToList(); 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<IIdentityDbContext,
.LongCountAsync(GetCancellationToken(cancellationToken)); .LongCountAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task RemoveClaimFromAllRolesAsync(string claimType, bool autoSave = false, CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync();
var roleClaims = await dbContext.Set<IdentityRoleClaim>().Where(uc => uc.ClaimType == claimType).ToListAsync(cancellationToken: cancellationToken);
if (roleClaims.Any())
{
(await GetDbContextAsync()).Set<IdentityRoleClaim>().RemoveRange(roleClaims);
if (autoSave)
{
await dbContext.SaveChangesAsync(GetCancellationToken(cancellationToken));
}
}
}
protected virtual async Task<List<IdentityRole>> GetListInternalAsync( protected virtual async Task<List<IdentityRole>> GetListInternalAsync(
string sorting = null, string sorting = null,
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,

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

@ -74,7 +74,7 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
{ {
Id = gp.Key, RoleNames = gp.Select(x => x.Name).ToArray() Id = gp.Key, RoleNames = gp.Select(x => x.Name).ToArray()
}).ToListAsync(cancellationToken: cancellationToken); }).ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoles = await (from userOu in dbContext.Set<IdentityUserOrganizationUnit>() var orgUnitRoles = await (from userOu in dbContext.Set<IdentityUserOrganizationUnit>()
join roleOu in dbContext.Set<OrganizationUnitRole>() on userOu.OrganizationUnitId equals roleOu.OrganizationUnitId join roleOu in dbContext.Set<OrganizationUnitRole>() on userOu.OrganizationUnitId equals roleOu.OrganizationUnitId
join role in dbContext.Roles on roleOu.RoleId equals role.Id join role in dbContext.Roles on roleOu.RoleId equals role.Id
@ -89,7 +89,7 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
{ {
Id = gp.Key, RoleNames = gp.Select(x => x.Name).ToArray() Id = gp.Key, RoleNames = gp.Select(x => x.Name).ToArray()
}).ToListAsync(cancellationToken: cancellationToken); }).ToListAsync(cancellationToken: cancellationToken);
return userRoles.Concat(orgUnitRoles).GroupBy(x => x.Id).Select(x => new IdentityUserIdWithRoleNames {Id = x.Key, RoleNames = x.SelectMany(y => y.RoleNames).Distinct().ToArray()}).ToList(); return userRoles.Concat(orgUnitRoles).GroupBy(x => x.Id).Select(x => new IdentityUserIdWithRoleNames {Id = x.Key, RoleNames = x.SelectMany(y => y.RoleNames).Distinct().ToArray()}).ToList();
} }
@ -145,6 +145,20 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task RemoveClaimFromAllUsersAsync(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,
@ -216,7 +230,7 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
minModifitionTime, minModifitionTime,
cancellationToken cancellationToken
); );
return await query.IncludeDetails(includeDetails) return await query.IncludeDetails(includeDetails)
.OrderBy(sorting.IsNullOrWhiteSpace() ? nameof(IdentityUser.UserName) : sorting) .OrderBy(sorting.IsNullOrWhiteSpace() ? nameof(IdentityUser.UserName) : sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
@ -437,14 +451,14 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
{ {
var upperFilter = filter?.ToUpperInvariant(); var upperFilter = filter?.ToUpperInvariant();
var query = await GetQueryableAsync(); var query = await GetQueryableAsync();
if (roleId.HasValue) if (roleId.HasValue)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var organizationUnitIds = await dbContext.Set<OrganizationUnitRole>().Where(q => q.RoleId == roleId.Value).Select(q => q.OrganizationUnitId).ToArrayAsync(cancellationToken: cancellationToken); var organizationUnitIds = await dbContext.Set<OrganizationUnitRole>().Where(q => q.RoleId == roleId.Value).Select(q => q.OrganizationUnitId).ToArrayAsync(cancellationToken: cancellationToken);
query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId))); query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId)));
} }
return query return query
.WhereIf( .WhereIf(
!filter.IsNullOrWhiteSpace(), !filter.IsNullOrWhiteSpace(),

18
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs

@ -42,13 +42,13 @@ public class MongoIdentityRoleRepository : MongoDbRepository<IAbpIdentityMongoDb
.Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId))) .Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId)))
.SelectMany(user => user.Roles) .SelectMany(user => user.Roles)
.GroupBy(userRole => userRole.RoleId) .GroupBy(userRole => userRole.RoleId)
.Select(x => new .Select(x => new
{ {
RoleId = x.Key, RoleId = x.Key,
Count = x.Count() Count = x.Count()
}) })
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
return roles.Select(role => new IdentityRoleWithUserCount(role, userCount.FirstOrDefault(x => x.RoleId == role.Id)?.Count ?? 0)).ToList(); 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<IAbpIdentityMongoDb
.LongCountAsync(GetCancellationToken(cancellationToken)); .LongCountAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task RemoveClaimFromAllRolesAsync(string claimType, bool autoSave = false, CancellationToken cancellationToken = default)
{
var roles = await (await GetMongoQueryableAsync(cancellationToken))
.Where(r => 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<List<IdentityRole>> GetListInternalAsync( protected virtual async Task<List<IdentityRole>> GetListInternalAsync(
string sorting = null, string sorting = null,
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,

34
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 RemoveClaimFromAllUsersAsync(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,
@ -164,7 +178,7 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
DateTime? maxModifitionTime = null, DateTime? maxModifitionTime = null,
DateTime? minModifitionTime = null, DateTime? minModifitionTime = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var query = await GetFilteredQueryableAsync( var query = await GetFilteredQueryableAsync(
filter, filter,
roleId, roleId,
@ -184,7 +198,7 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
minModifitionTime, minModifitionTime,
cancellationToken cancellationToken
); );
return await query return await query
.OrderBy(sorting.IsNullOrWhiteSpace() ? nameof(IdentityUser.UserName) : sorting) .OrderBy(sorting.IsNullOrWhiteSpace() ? nameof(IdentityUser.UserName) : sorting)
.As<IMongoQueryable<IdentityUser>>() .As<IMongoQueryable<IdentityUser>>()
@ -365,17 +379,17 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var users = await GetListByIdsAsync(userIds, cancellationToken: cancellationToken); var users = await GetListByIdsAsync(userIds, cancellationToken: cancellationToken);
var userAndRoleIds = users.SelectMany(u => u.Roles) var userAndRoleIds = users.SelectMany(u => u.Roles)
.Select(userRole => new { userRole.UserId, userRole.RoleId }) .Select(userRole => new { userRole.UserId, userRole.RoleId })
.GroupBy(x => x.UserId).ToDictionary(x => x.Key, x => x.Select(r => r.RoleId).ToList()); .GroupBy(x => x.UserId).ToDictionary(x => x.Key, x => x.Select(r => r.RoleId).ToList());
var userAndOrganizationUnitIds = users.SelectMany(u => u.OrganizationUnits) var userAndOrganizationUnitIds = users.SelectMany(u => u.OrganizationUnits)
.Select(userOrganizationUnit => new { userOrganizationUnit.UserId, userOrganizationUnit.OrganizationUnitId }) .Select(userOrganizationUnit => new { userOrganizationUnit.UserId, userOrganizationUnit.OrganizationUnitId })
.GroupBy(x => x.UserId).ToDictionary(x => x.Key, x => x.Select(r => r.OrganizationUnitId).ToList()); .GroupBy(x => x.UserId).ToDictionary(x => x.Key, x => x.Select(r => r.OrganizationUnitId).ToList());
var organizationUnitIds = userAndOrganizationUnitIds.SelectMany(x => x.Value); var organizationUnitIds = userAndOrganizationUnitIds.SelectMany(x => x.Value);
var roleIds = userAndRoleIds.SelectMany(x => x.Value); var roleIds = userAndRoleIds.SelectMany(x => x.Value);
var organizationUnitAndRoleIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken)).Where(ou => organizationUnitIds.Contains(ou.Id)) var organizationUnitAndRoleIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken)).Where(ou => organizationUnitIds.Contains(ou.Id))
.Select(userOrganizationUnit => new .Select(userOrganizationUnit => new
{ {
@ -384,10 +398,10 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
}).ToListAsync(cancellationToken: cancellationToken); }).ToListAsync(cancellationToken: cancellationToken);
var allOrganizationUnitRoleIds = organizationUnitAndRoleIds.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToList(); var allOrganizationUnitRoleIds = organizationUnitAndRoleIds.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToList();
var allRoleIds = roleIds.Union(allOrganizationUnitRoleIds); var allRoleIds = roleIds.Union(allOrganizationUnitRoleIds);
var roles = await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => new{ r.Id, r.Name }).ToListAsync(cancellationToken); var roles = await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => new{ r.Id, r.Name }).ToListAsync(cancellationToken);
var userRoles = userAndRoleIds.ToDictionary(x => x.Key, x => roles.Where(r => x.Value.Contains(r.Id)).Select(r => r.Name).ToArray()); var userRoles = userAndRoleIds.ToDictionary(x => x.Key, x => roles.Where(r => x.Value.Contains(r.Id)).Select(r => r.Name).ToArray());
var result = userRoles.Select(x => new IdentityUserIdWithRoleNames { Id = x.Key, RoleNames = x.Value }).ToList(); var result = userRoles.Select(x => new IdentityUserIdWithRoleNames { Id = x.Key, RoleNames = x.Value }).ToList();
foreach (var userAndOrganizationUnitId in userAndOrganizationUnitIds) foreach (var userAndOrganizationUnitId in userAndOrganizationUnitIds)
@ -429,17 +443,17 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
{ {
var upperFilter = filter?.ToUpperInvariant(); var upperFilter = filter?.ToUpperInvariant();
var query = await GetMongoQueryableAsync(cancellationToken); var query = await GetMongoQueryableAsync(cancellationToken);
if (roleId.HasValue) if (roleId.HasValue)
{ {
var organizationUnitIds = (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken)) var organizationUnitIds = (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Roles.Any(r => r.RoleId == roleId.Value)) .Where(ou => ou.Roles.Any(r => r.RoleId == roleId.Value))
.Select(userOrganizationUnit => userOrganizationUnit.Id) .Select(userOrganizationUnit => userOrganizationUnit.Id)
.ToArray(); .ToArray();
query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId))); query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId)));
} }
return query return query
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( .WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(), !filter.IsNullOrWhiteSpace(),

59
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;
@ -15,12 +17,26 @@ public abstract class IdentityClaimTypeRepository_Tests<TStartupModule> : AbpIde
where TStartupModule : IAbpModule where TStartupModule : IAbpModule
{ {
protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } protected IIdentityClaimTypeRepository ClaimTypeRepository { get; }
protected IdentityClaimTypeManager IdentityClaimTypeManager { get; }
protected IGuidGenerator GuidGenerator { 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() public IdentityClaimTypeRepository_Tests()
{ {
ClaimTypeRepository = ServiceProvider.GetRequiredService<IIdentityClaimTypeRepository>(); ClaimTypeRepository = ServiceProvider.GetRequiredService<IIdentityClaimTypeRepository>();
IdentityClaimTypeManager = ServiceProvider.GetRequiredService<IdentityClaimTypeManager>();
GuidGenerator = ServiceProvider.GetRequiredService<IGuidGenerator>(); GuidGenerator = ServiceProvider.GetRequiredService<IGuidGenerator>();
UnitOfWorkManager = ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
IdentityUserManager = ServiceProvider.GetRequiredService<IdentityUserManager>();
UserRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
RoleRepository = ServiceProvider.GetRequiredService<IIdentityRoleRepository>();
IdentityRoleManager = ServiceProvider.GetRequiredService<IdentityRoleManager>();
IdentityTestData = ServiceProvider.GetRequiredService<IdentityTestData>();
} }
[Fact] [Fact]
@ -42,7 +58,7 @@ public abstract class IdentityClaimTypeRepository_Tests<TStartupModule> : AbpIde
{ {
(await ClaimTypeRepository.GetCountAsync("Age")).ShouldBe(1); (await ClaimTypeRepository.GetCountAsync("Age")).ShouldBe(1);
} }
[Fact] [Fact]
public async Task GetListAsyncByNames() public async Task GetListAsyncByNames()
{ {
@ -50,4 +66,45 @@ 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");
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 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();
}
}
} }

Loading…
Cancel
Save