Browse Source

Refactored identity module

pull/1070/head
Halil ibrahim Kalkan 8 years ago
parent
commit
d0dcccc95b
  1. 13
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs
  2. 14
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdenityClaimTypeManager.cs
  3. 15
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs
  4. 35
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs
  5. 4
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs
  6. 33
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceDataSeeder.cs

13
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<IdentityClaimType, Guid>
{
Task<bool> DoesNameExist(string name, Guid? claimTypeId = null);
/// <summary>
/// Checks if there is a <see cref="IdentityClaimType"/> entity with given name.
/// </summary>
/// <param name="name">Name to check</param>
/// <param name="ignoredId">
/// An Id value to ignore on checking.
/// If there is an entity with given <paramref name="ignoredId"/> it's ignored.
/// </param>
Task<bool> AnyAsync(string name, Guid? ignoredId = null);
Task<List<IdentityClaimType>> GetListAsync(string sorting, int maxResultCount, int skipCount);
Task<int> GetTotalCount();
}
}

14
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<IdentityClaimType> 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<IdentityClaimType> 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}");
}

15
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<IIdentityDbContext, IdentityClaimType, Guid>, IIdentityClaimTypeRepository
{
public EfCoreIdentityClaimTypeRepository(IDbContextProvider<IIdentityDbContext> dbContextProvider) : base(dbContextProvider)
public EfCoreIdentityClaimTypeRepository(IDbContextProvider<IIdentityDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
public async Task<bool> DoesNameExist(string name, Guid? claimTypeId = null)
public async Task<bool> 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<List<IdentityClaimType>> GetListAsync(string sorting, int maxResultCount, int skipCount)
@ -28,10 +32,5 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
return identityClaimTypes;
}
public async Task<int> GetTotalCount()
{
return await DbSet.CountAsync();
}
}
}

35
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<bool> DoesNameExist(string name, Guid? claimTypeId = null)
public async Task<bool> 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<List<IdentityClaimType>> GetListAsync(string sorting, int maxResultCount, int skipCount)
{
var identityClaimTypes = GetMongoQueryable().OrderBy(sorting ?? "name desc")
.PageBy(skipCount, maxResultCount)
.ToList();
return identityClaimTypes;
}
public async Task<int> GetTotalCount()
{
return await GetMongoQueryable().CountAsync();
return await GetMongoQueryable()
.OrderBy(sorting ?? nameof(IdentityClaimType.Name))
.As<IMongoQueryable<IdentityClaimType>>()
.PageBy<IdentityClaimType, IMongoQueryable<IdentityClaimType>>(skipCount, maxResultCount)
.ToListAsync();
}
}
}

4
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);
}

33
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
)
);
}
}
}

Loading…
Cancel
Save