From 34d26493147cea303d878640b00ced30b1a001d9 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 3 Oct 2018 16:24:36 +0300 Subject: [PATCH] identity IdentityClaimTyp improvements --- .../Abp/Identity/IdentityClaimTypeConsts.cs | 2 +- .../Abp/Identity/IIdentityUserRepository.cs | 6 ++++ .../Volo/Abp/Identity/IdentityUserClaim.cs | 2 +- .../EfCoreIdentityUserRepository.cs | 35 +++++++++++++++++-- .../MongoDB/MongoIdentityUserRepository.cs | 25 +++++++++++-- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityClaimTypeConsts.cs b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityClaimTypeConsts.cs index 5581c53638..1b691f404c 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityClaimTypeConsts.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain.Shared/Volo/Abp/Identity/IdentityClaimTypeConsts.cs @@ -6,7 +6,7 @@ namespace Volo.Abp.Identity { public class IdentityClaimTypeConsts { - public const int MaxNameLength = 128; + public const int MaxNameLength = 256; public const int MaxRegexLength = 512; diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs index 98269da005..20eb1a749d 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs @@ -40,6 +40,8 @@ namespace Volo.Abp.Identity CancellationToken cancellationToken = default ); + Task UpdateClaimsAsync(Guid id, List claims); + Task> GetListByNormalizedRoleNameAsync( string normalizedRoleName, bool includeDetails = false, @@ -61,6 +63,10 @@ namespace Volo.Abp.Identity CancellationToken cancellationToken = default ); + Task> GetClaimsAsync( + Guid id, + CancellationToken cancellationToken = default); + Task GetCountAsync( string filter = null, CancellationToken cancellationToken = default diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserClaim.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserClaim.cs index d302beb7c2..d91db39b33 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserClaim.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserClaim.cs @@ -25,7 +25,7 @@ namespace Volo.Abp.Identity UserId = userId; } - protected internal IdentityUserClaim(Guid id, Guid userId, [NotNull] string claimType, string claimValue, Guid? tenantId) + public IdentityUserClaim(Guid id, Guid userId, [NotNull] string claimType, string claimValue, Guid? tenantId) : base(id, claimType, claimValue, tenantId) { UserId = userId; diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs index fed25edeb7..7cef5b5735 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs @@ -8,15 +8,18 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Guids; namespace Volo.Abp.Identity.EntityFrameworkCore { public class EfCoreIdentityUserRepository : EfCoreRepository, IIdentityUserRepository { - public EfCoreIdentityUserRepository(IDbContextProvider dbContextProvider) + private readonly IGuidGenerator _guidGenerator; + + public EfCoreIdentityUserRepository(IDbContextProvider dbContextProvider, IGuidGenerator guidGenerator) : base(dbContextProvider) { - + _guidGenerator = guidGenerator; } public virtual async Task FindByNormalizedUserNameAsync( @@ -131,6 +134,34 @@ namespace Volo.Abp.Identity.EntityFrameworkCore return await query.ToListAsync(GetCancellationToken(cancellationToken)); } + public virtual async Task> GetClaimsAsync( + Guid id, + CancellationToken cancellationToken = default) + { + var query = from userClaim in DbContext.Set() + where userClaim.UserId == id + select userClaim; + + return await query.ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task UpdateClaimsAsync(Guid id, List claims) + { + var dbSet = DbContext.Set(); + + var oldClaims = dbSet.Where(c => c.UserId == id).ToList(); + + foreach (var oldClaim in oldClaims) + { + dbSet.Remove(oldClaim); + } + + foreach (var claim in claims) + { + dbSet.Add(new IdentityUserClaim(_guidGenerator.Create(), id, claim.ClaimType, claim.ClaimValue, CurrentTenant.Id)); + } + } + public virtual async Task GetCountAsync( string filter = null, CancellationToken cancellationToken = default) diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs index c9187953da..9bbea54e72 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs @@ -8,16 +8,19 @@ using System.Threading.Tasks; using MongoDB.Driver; using MongoDB.Driver.Linq; using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.Guids; using Volo.Abp.MongoDB; namespace Volo.Abp.Identity.MongoDB { public class MongoIdentityUserRepository : MongoDbRepository, IIdentityUserRepository { - public MongoIdentityUserRepository(IMongoDbContextProvider dbContextProvider) + private readonly IGuidGenerator _guidGenerator; + + public MongoIdentityUserRepository(IMongoDbContextProvider dbContextProvider, IGuidGenerator guidGenerator) : base(dbContextProvider) { - + _guidGenerator = guidGenerator; } public async Task FindByNormalizedUserNameAsync( @@ -118,6 +121,24 @@ namespace Volo.Abp.Identity.MongoDB return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken)); } + public async Task> GetClaimsAsync(Guid id, CancellationToken cancellationToken = default) + { + var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)); + return user.Claims.ToList(); + } + + public async Task UpdateClaimsAsync(Guid id, List claims) + { + var user = await GetAsync(id); + + user.Claims.Clear(); + + foreach (var claim in claims) + { + user.Claims.Add(new IdentityUserClaim(_guidGenerator.Create(), id, claim.ClaimType, claim.ClaimValue, CurrentTenant.Id)); + } + } + public async Task GetCountAsync( string filter = null, CancellationToken cancellationToken = default)