mirror of https://github.com/abpframework/abp.git
21 changed files with 287 additions and 1 deletions
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityClaimTypeConsts |
|||
{ |
|||
public const int MaxNameLength = 128; |
|||
|
|||
public const int MaxRegexLength = 512; |
|||
|
|||
public const int MaxRegexDescriptionLength = 128; |
|||
|
|||
public const int MaxDescriptionLength = 256; |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public enum IdentityClaimValueType |
|||
{ |
|||
String, |
|||
Int, |
|||
Boolean, |
|||
DateTime |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public interface IIdentityClaimTypeRepository : IBasicRepository<IdentityClaimType, Guid> |
|||
{ |
|||
Task<bool> DoesNameExist(string name, Guid? claimTypeId = null); |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.Abp.Guids; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdenityClaimTypeManager : IDomainService |
|||
{ |
|||
private readonly IIdentityClaimTypeRepository _identityClaimTypeRepository; |
|||
private readonly IGuidGenerator _guidGenerator; |
|||
|
|||
public IdenityClaimTypeManager(IIdentityClaimTypeRepository identityClaimTypeRepository, IGuidGenerator guidGenerator) |
|||
{ |
|||
_identityClaimTypeRepository = identityClaimTypeRepository; |
|||
_guidGenerator = guidGenerator; |
|||
} |
|||
|
|||
public async Task<IdentityClaimType> GetAsync(Guid id) |
|||
{ |
|||
return await _identityClaimTypeRepository.GetAsync(id); |
|||
} |
|||
|
|||
public async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType) |
|||
{ |
|||
if (await _identityClaimTypeRepository.DoesNameExist(claimType.Name)) |
|||
{ |
|||
throw new AbpException($"Name Exist: {claimType.Name}"); |
|||
} |
|||
|
|||
return await _identityClaimTypeRepository.InsertAsync(claimType); |
|||
} |
|||
|
|||
public async Task<IdentityClaimType> UpdateAsync(IdentityClaimType claimType) |
|||
{ |
|||
if (await _identityClaimTypeRepository.DoesNameExist(claimType.Name, claimType.Id)) |
|||
{ |
|||
throw new AbpException($"Name Exist: {claimType.Name}"); |
|||
} |
|||
|
|||
return await _identityClaimTypeRepository.UpdateAsync(claimType); |
|||
} |
|||
|
|||
public async Task DeleteAsync(Guid id) |
|||
{ |
|||
await _identityClaimTypeRepository.DeleteAsync(id); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class IdentityClaimType : Entity<Guid> |
|||
{ |
|||
public virtual string Name { get; protected set; } |
|||
|
|||
public virtual bool Required { get; protected set; } |
|||
|
|||
public virtual bool IsStatic { get; protected set; } |
|||
|
|||
public virtual string Regex { get; protected set; } |
|||
|
|||
public virtual string RegexDescription { get; protected set; } |
|||
|
|||
public virtual string Description { get; protected set; } |
|||
|
|||
public virtual IdentityClaimValueType ValueType { get; protected set; } |
|||
|
|||
protected IdentityClaimType() |
|||
{ |
|||
} |
|||
|
|||
public IdentityClaimType(Guid id, [NotNull] string name, bool required, bool isStatic, [CanBeNull]string regex, [CanBeNull]string regexDescription, [CanBeNull] string description, IdentityClaimValueType valueType = IdentityClaimValueType.String) |
|||
{ |
|||
Check.NotNull(name, nameof(name)); |
|||
|
|||
Name = name; |
|||
Required = required; |
|||
IsStatic = isStatic; |
|||
Regex = regex; |
|||
RegexDescription = regexDescription; |
|||
Description = description; |
|||
ValueType = valueType; |
|||
} |
|||
|
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Internal; |
|||
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.Identity.EntityFrameworkCore |
|||
{ |
|||
public class EfCoreIdentityClaimTypeRepository : EfCoreRepository<IIdentityDbContext, IdentityClaimType, Guid>, IIdentityClaimTypeRepository |
|||
{ |
|||
public EfCoreIdentityClaimTypeRepository(IDbContextProvider<IIdentityDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
} |
|||
|
|||
public async Task<bool> DoesNameExist(string name, Guid? claimTypeId = null) |
|||
{ |
|||
return await DbSet.WhereIf(claimTypeId != null, ct => ct.Id == claimTypeId).CountAsync(ct => ct.Name == name) > 0; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using MongoDB.Driver.Linq; |
|||
using Volo.Abp.Domain.Repositories.MongoDB; |
|||
using Volo.Abp.MongoDB; |
|||
|
|||
namespace Volo.Abp.Identity.MongoDB |
|||
{ |
|||
public class MongoIdentityClaimTypeRepository : MongoDbRepository<IAbpIdentityMongoDbContext, IdentityClaimType, Guid>, IIdentityClaimTypeRepository |
|||
{ |
|||
public MongoIdentityClaimTypeRepository(IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
} |
|||
|
|||
public async Task<bool> DoesNameExist(string name, Guid? claimTypeId = null) |
|||
{ |
|||
return GetMongoQueryable().WhereIf(claimTypeId != null, ct => ct.Id == claimTypeId).Count(ct => ct.Name == name) > 0; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Identity.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.Identity.MongoDB |
|||
{ |
|||
public class IdentityClaimTypeRepository_Tests : IdentityClaimTypeRepository_Tests<AbpIdentityEntityFrameworkCoreTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.Identity.MongoDB |
|||
{ |
|||
public class IdentityClaimTypeRepository_Tests : IdentityClaimTypeRepository_Tests<AbpIdentityMongoDbTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public abstract class IdentityClaimTypeRepository_Tests<TStartupModule> : AbpIdentityTestBase<TStartupModule> |
|||
where TStartupModule : IAbpModule |
|||
{ |
|||
protected IIdentityClaimTypeRepository ClaimTypeRepository { get; } |
|||
protected IGuidGenerator GuidGenerator { get; } |
|||
|
|||
public IdentityClaimTypeRepository_Tests() |
|||
{ |
|||
ClaimTypeRepository = ServiceProvider.GetRequiredService<IIdentityClaimTypeRepository>(); |
|||
GuidGenerator = ServiceProvider.GetRequiredService<IGuidGenerator>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Check_Name_If_It_Is_Uniquee() |
|||
{ |
|||
var claim = (await ClaimTypeRepository.GetListAsync()).FirstOrDefault(); |
|||
|
|||
var result1 = await ClaimTypeRepository.DoesNameExist(claim.Name); |
|||
|
|||
result1.ShouldBe(true); |
|||
|
|||
var result2 = await ClaimTypeRepository.DoesNameExist(Guid.NewGuid().ToString()); |
|||
|
|||
result2.ShouldBe(false); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue