diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs index e55bdd966b..c2d5b4fd5c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -8,10 +7,16 @@ namespace Volo.Abp.Identity { public interface IIdentityClaimTypeRepository : IBasicRepository { - Task DoesNameExist(string name, Guid? claimTypeId = null); + /// + /// Checks if there is a entity with given name. + /// + /// Name to check + /// + /// An Id value to ignore on checking. + /// If there is an entity with given it's ignored. + /// + Task AnyAsync(string name, Guid? ignoredId = null); Task> GetListAsync(string sorting, int maxResultCount, int skipCount); - - Task GetTotalCount(); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdenityClaimTypeManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdenityClaimTypeManager.cs index eac38b9f24..69e5428e66 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdenityClaimTypeManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdenityClaimTypeManager.cs @@ -1,26 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using Volo.Abp.Domain.Services; -using Volo.Abp.Guids; namespace Volo.Abp.Identity { public class IdenityClaimTypeManager : DomainService { private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository; - private readonly IGuidGenerator _guidGenerator; - public IdenityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository, IGuidGenerator guidGenerator) + public IdenityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository) { _identityClaimTypeRepository = identityClaimTypeRepository; - _guidGenerator = guidGenerator; } public virtual async Task CreateAsync(IdentityClaimType claimType) { - if (await _identityClaimTypeRepository.DoesNameExist(claimType.Name)) + if (await _identityClaimTypeRepository.AnyAsync(claimType.Name)) { throw new AbpException($"Name Exist: {claimType.Name}"); } @@ -30,7 +24,7 @@ namespace Volo.Abp.Identity public virtual async Task UpdateAsync(IdentityClaimType claimType) { - if (await _identityClaimTypeRepository.DoesNameExist(claimType.Name, claimType.Id)) + if (await _identityClaimTypeRepository.AnyAsync(claimType.Name, claimType.Id)) { throw new AbpException($"Name Exist: {claimType.Name}"); } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs index f50ed08d35..50a4578cae 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs @@ -11,13 +11,17 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { public class EfCoreIdentityClaimTypeRepository : EfCoreRepository, IIdentityClaimTypeRepository { - public EfCoreIdentityClaimTypeRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) + public EfCoreIdentityClaimTypeRepository(IDbContextProvider dbContextProvider) + : base(dbContextProvider) { + } - public async Task DoesNameExist(string name, Guid? claimTypeId = null) + public async Task AnyAsync(string name, Guid? ignoredId = null) { - return await DbSet.WhereIf(claimTypeId != null, ct => ct.Id != claimTypeId).CountAsync(ct => ct.Name == name) > 0; + return await DbSet + .WhereIf(ignoredId != null, ct => ct.Id != ignoredId) + .CountAsync(ct => ct.Name == name) > 0; } public async Task> GetListAsync(string sorting, int maxResultCount, int skipCount) @@ -28,10 +32,5 @@ namespace Volo.Abp.Identity.EntityFrameworkCore return identityClaimTypes; } - - public async Task GetTotalCount() - { - return await DbSet.CountAsync(); - } } } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs index 654848bb1b..c2bbd56a6d 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Linq.Dynamic.Core; using System.Threading.Tasks; +using MongoDB.Driver; using MongoDB.Driver.Linq; +using System.Linq; +using System.Linq.Dynamic.Core; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; @@ -15,23 +16,29 @@ namespace Volo.Abp.Identity.MongoDB { } - public async Task DoesNameExist(string name, Guid? claimTypeId = null) + public async Task AnyAsync(string name, Guid? ignoredId = null) { - return GetMongoQueryable().WhereIf(claimTypeId != null, ct => ct.Id != claimTypeId).Count(ct => ct.Name == name) > 0; + if (ignoredId == null) + { + return await GetMongoQueryable() + .Where(ct => ct.Name == name) + .AnyAsync(); + } + else + { + return await GetMongoQueryable() + .Where(ct => ct.Id != ignoredId && ct.Name == name) + .AnyAsync(); + } } public async Task> GetListAsync(string sorting, int maxResultCount, int skipCount) { - var identityClaimTypes = GetMongoQueryable().OrderBy(sorting ?? "name desc") - .PageBy(skipCount, maxResultCount) - .ToList(); - - return identityClaimTypes; - } - - public async Task GetTotalCount() - { - return await GetMongoQueryable().CountAsync(); + return await GetMongoQueryable() + .OrderBy(sorting ?? nameof(IdentityClaimType.Name)) + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(); } } } 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 56de1d5d5f..3250c0b04a 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 @@ -28,11 +28,11 @@ namespace Volo.Abp.Identity { var claim = (await ClaimTypeRepository.GetListAsync()).FirstOrDefault(); - var result1 = await ClaimTypeRepository.DoesNameExist(claim.Name); + var result1 = await ClaimTypeRepository.AnyAsync(claim.Name); result1.ShouldBe(true); - var result2 = await ClaimTypeRepository.DoesNameExist(Guid.NewGuid().ToString()); + var result2 = await ClaimTypeRepository.AnyAsync(Guid.NewGuid().ToString()); result2.ShouldBe(false); } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceDataSeeder.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceDataSeeder.cs index af62b8ed15..a5c1e23861 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceDataSeeder.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceDataSeeder.cs @@ -1,20 +1,24 @@ using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; +using Volo.Abp.Identity; namespace Volo.Abp.IdentityServer.IdentityResources { public class IdentityResourceDataSeeder : IIdentityResourceDataSeeder, ITransientDependency { + protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } protected IIdentityResourceRepository IdentityResourceRepository { get; } protected IGuidGenerator GuidGenerator { get; } public IdentityResourceDataSeeder( IIdentityResourceRepository identityResourceRepository, - IGuidGenerator guidGenerator) + IGuidGenerator guidGenerator, + IIdentityClaimTypeRepository claimTypeRepository) { IdentityResourceRepository = identityResourceRepository; GuidGenerator = guidGenerator; + ClaimTypeRepository = claimTypeRepository; } public virtual async Task CreateStandardResourcesAsync() @@ -30,11 +34,16 @@ namespace Volo.Abp.IdentityServer.IdentityResources foreach (var resource in resources) { - await AddIfNotExistsAsync(resource); + foreach (var claimType in resource.UserClaims) + { + await AddClaimTypeIfNotExistsAsync(claimType); + } + + await AddIdentityResourceIfNotExistsAsync(resource); } } - - protected virtual async Task AddIfNotExistsAsync(IdentityServer4.Models.IdentityResource resource) + + protected virtual async Task AddIdentityResourceIfNotExistsAsync(IdentityServer4.Models.IdentityResource resource) { if (await IdentityResourceRepository.FindByNameAsync(resource.Name) != null) { @@ -48,5 +57,21 @@ namespace Volo.Abp.IdentityServer.IdentityResources ) ); } + + protected virtual async Task AddClaimTypeIfNotExistsAsync(string claimType) + { + if (await ClaimTypeRepository.AnyAsync(claimType)) + { + return; + } + + await ClaimTypeRepository.InsertAsync( + new IdentityClaimType( + GuidGenerator.Create(), + claimType, + isStatic: true + ) + ); + } } }