Browse Source

Merge pull request #22540 from abpframework/auto-merge/rel-9-1/3594

Merge branch rel-9.2 with rel-9.1
pull/22542/head
maliming 1 year ago
committed by GitHub
parent
commit
af22ec001f
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs
  2. 4
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs
  3. 16
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
  4. 46
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs
  5. 7
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  6. 9
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs
  7. 7
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

3
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs

@ -98,18 +98,21 @@ public interface IOrganizationUnitRepository : IBasicRepository<OrganizationUnit
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,
int skipCount = 0, int skipCount = 0,
string filter = null, string filter = null,
bool includeChildren = false,
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
Task<List<Guid>> GetMemberIdsAsync( Task<List<Guid>> GetMemberIdsAsync(
Guid id, Guid id,
bool includeChildren = false,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );
Task<int> GetMembersCountAsync( Task<int> GetMembersCountAsync(
OrganizationUnit organizationUnit, OrganizationUnit organizationUnit,
string filter = null, string filter = null,
bool includeChildren = false,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default
); );

4
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

@ -420,14 +420,14 @@ public class IdentityUserManager : UserManager<IdentityUser>, IDomainService
var sourceOrganization = await OrganizationUnitRepository.GetAsync(sourceOrganizationId, cancellationToken: CancellationToken); var sourceOrganization = await OrganizationUnitRepository.GetAsync(sourceOrganizationId, cancellationToken: CancellationToken);
Logger.LogDebug($"Remove dynamic claims cache for users of organization: {sourceOrganizationId}"); Logger.LogDebug($"Remove dynamic claims cache for users of organization: {sourceOrganizationId}");
var userIdList = await OrganizationUnitRepository.GetMemberIdsAsync(sourceOrganizationId, cancellationToken: CancellationToken); var userIdList = await OrganizationUnitRepository.GetMemberIdsAsync(sourceOrganizationId, includeChildren: true, cancellationToken: CancellationToken);
await DynamicClaimCache.RemoveManyAsync(userIdList.Select(userId => AbpDynamicClaimCacheItem.CalculateCacheKey(userId, sourceOrganization.TenantId)), token: CancellationToken); await DynamicClaimCache.RemoveManyAsync(userIdList.Select(userId => AbpDynamicClaimCacheItem.CalculateCacheKey(userId, sourceOrganization.TenantId)), token: CancellationToken);
var targetOrganization = targetOrganizationId.HasValue ? await OrganizationUnitRepository.GetAsync(targetOrganizationId.Value, cancellationToken: CancellationToken) : null; var targetOrganization = targetOrganizationId.HasValue ? await OrganizationUnitRepository.GetAsync(targetOrganizationId.Value, cancellationToken: CancellationToken) : null;
if (targetOrganization != null) if (targetOrganization != null)
{ {
Logger.LogDebug($"Remove dynamic claims cache for users of organization: {targetOrganizationId}"); Logger.LogDebug($"Remove dynamic claims cache for users of organization: {targetOrganizationId}");
userIdList = await OrganizationUnitRepository.GetMemberIdsAsync(targetOrganizationId.Value, cancellationToken: CancellationToken); userIdList = await OrganizationUnitRepository.GetMemberIdsAsync(targetOrganizationId.Value, includeChildren: true, cancellationToken: CancellationToken);
await DynamicClaimCache.RemoveManyAsync(userIdList.Select(userId => AbpDynamicClaimCacheItem.CalculateCacheKey(userId, targetOrganization.TenantId)), token: CancellationToken); await DynamicClaimCache.RemoveManyAsync(userIdList.Select(userId => AbpDynamicClaimCacheItem.CalculateCacheKey(userId, targetOrganization.TenantId)), token: CancellationToken);
} }

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

@ -7,6 +7,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
@ -424,9 +425,20 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
{ {
if (targetOrganizationId != null) if (targetOrganizationId != null)
{ {
var sourceOrganization = await (await GetDbContextAsync()).Set<OrganizationUnit>().FirstOrDefaultAsync(x => x.Id == sourceOrganizationId, cancellationToken: cancellationToken);
if (sourceOrganization == null)
{
throw new EntityNotFoundException(typeof(OrganizationUnit), sourceOrganizationId);
}
var allSourceOrganizationIds = await (await GetDbContextAsync()).Set<OrganizationUnit>()
.Where(x => x.Code.StartsWith(sourceOrganization.Code))
.Select(x => x.Id).ToArrayAsync(cancellationToken: cancellationToken);
var users = await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => x.OrganizationUnitId == targetOrganizationId).Select(x => x.UserId).ToArrayAsync(cancellationToken: cancellationToken); var users = await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => x.OrganizationUnitId == targetOrganizationId).Select(x => x.UserId).ToArrayAsync(cancellationToken: cancellationToken);
await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => x.OrganizationUnitId == sourceOrganizationId && !users.Contains(x.UserId)).ExecuteUpdateAsync(t => t.SetProperty(e => e.OrganizationUnitId, targetOrganizationId), GetCancellationToken(cancellationToken));
await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => x.OrganizationUnitId == sourceOrganizationId).ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => allSourceOrganizationIds.Contains(x.OrganizationUnitId) && !users.Contains(x.UserId)).ExecuteUpdateAsync(t => t.SetProperty(e => e.OrganizationUnitId, targetOrganizationId), GetCancellationToken(cancellationToken));
await (await GetDbContextAsync()).Set<IdentityUserOrganizationUnit>().Where(x => allSourceOrganizationIds.Contains(x.OrganizationUnitId)).ExecuteDeleteAsync(GetCancellationToken(cancellationToken));
} }
else else
{ {

46
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs

@ -208,32 +208,31 @@ public class EfCoreOrganizationUnitRepository
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,
int skipCount = 0, int skipCount = 0,
string filter = null, string filter = null,
bool includeChildren = false,
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter); var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, includeChildren);
return await query.IncludeDetails(includeDetails).OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityUser.UserName) : sorting) return await query.IncludeDetails(includeDetails).OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityUser.UserName) : sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task<List<Guid>> GetMemberIdsAsync(Guid id, CancellationToken cancellationToken = default) public virtual async Task<List<Guid>> GetMemberIdsAsync(Guid id, bool includeChildren = false, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var organizationUnit = await GetAsync(id, cancellationToken: cancellationToken);
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, null, includeChildren);
return await (from userOu in dbContext.Set<IdentityUserOrganizationUnit>() return await query.Select(x => x.Id).ToListAsync(cancellationToken);
join user in dbContext.Users on userOu.UserId equals user.Id
where userOu.OrganizationUnitId == id
select user.Id).ToListAsync(cancellationToken);
} }
public virtual async Task<int> GetMembersCountAsync( public virtual async Task<int> GetMembersCountAsync(
OrganizationUnit organizationUnit, OrganizationUnit organizationUnit,
string filter = null, string filter = null,
bool includeChildren = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter); var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, includeChildren);
return await query.CountAsync(GetCancellationToken(cancellationToken)); return await query.CountAsync(GetCancellationToken(cancellationToken));
} }
@ -324,14 +323,33 @@ public class EfCoreOrganizationUnitRepository
dbContext.Set<IdentityUserOrganizationUnit>().RemoveRange(ouMembersQuery); dbContext.Set<IdentityUserOrganizationUnit>().RemoveRange(ouMembersQuery);
} }
protected virtual async Task<IQueryable<IdentityUser>> CreateGetMembersFilteredQueryAsync(OrganizationUnit organizationUnit, string filter = null) protected virtual async Task<IQueryable<IdentityUser>> CreateGetMembersFilteredQueryAsync(
OrganizationUnit organizationUnit,
string filter = null,
bool includeChildren = false)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var query = from userOu in dbContext.Set<IdentityUserOrganizationUnit>() IQueryable<IdentityUser> query;
join user in dbContext.Users on userOu.UserId equals user.Id if (includeChildren)
where userOu.OrganizationUnitId == organizationUnit.Id {
select user; var childrenIds = await (await GetDbSetAsync())
.Where(ou => ou.Code.StartsWith(organizationUnit.Code))
.Select(x => x.Id)
.ToListAsync();
query = from userOu in dbContext.Set<IdentityUserOrganizationUnit>()
join user in dbContext.Users on userOu.UserId equals user.Id
where childrenIds.Contains(userOu.OrganizationUnitId)
select user;
}
else
{
query = from userOu in dbContext.Set<IdentityUserOrganizationUnit>()
join user in dbContext.Users on userOu.UserId equals user.Id
where userOu.OrganizationUnitId == organizationUnit.Id
select user;
}
if (!filter.IsNullOrWhiteSpace()) if (!filter.IsNullOrWhiteSpace())
{ {

7
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDB.Driver.Linq; using MongoDB.Driver.Linq;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
@ -367,7 +368,11 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
foreach (var user in users) foreach (var user in users)
{ {
user.RemoveOrganizationUnit(sourceOrganizationId); foreach (var organizationId in allSourceOrganizationIds)
{
user.RemoveOrganizationUnit(organizationId);
}
if (targetOrganizationId.HasValue) if (targetOrganizationId.HasValue)
{ {
user.AddOrganizationUnit(targetOrganizationId.Value); user.AddOrganizationUnit(targetOrganizationId.Value);

9
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs

@ -184,18 +184,19 @@ public class MongoOrganizationUnitRepository
int maxResultCount = int.MaxValue, int maxResultCount = int.MaxValue,
int skipCount = 0, int skipCount = 0,
string filter = null, string filter = null,
bool includeChildren = false,
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, cancellationToken); var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, includeChildren, cancellationToken);
return await query return await query
.OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityUser.UserName) : sorting) .OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityUser.UserName) : sorting)
.PageBy(skipCount, maxResultCount) .PageBy(skipCount, maxResultCount)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
} }
public virtual async Task<List<Guid>> GetMemberIdsAsync(Guid id, CancellationToken cancellationToken = default) public virtual async Task<List<Guid>> GetMemberIdsAsync(Guid id, bool includeChildren = false, CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetQueryableAsync<IdentityUser>(cancellationToken)) return await (await GetQueryableAsync<IdentityUser>(cancellationToken))
@ -206,10 +207,11 @@ public class MongoOrganizationUnitRepository
public virtual async Task<int> GetMembersCountAsync( public virtual async Task<int> GetMembersCountAsync(
OrganizationUnit organizationUnit, OrganizationUnit organizationUnit,
string filter = null, string filter = null,
bool includeChildren = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, cancellationToken); var query = await CreateGetMembersFilteredQueryAsync(organizationUnit, filter, includeChildren, cancellationToken);
return await query.CountAsync(cancellationToken); return await query.CountAsync(cancellationToken);
} }
@ -277,6 +279,7 @@ public class MongoOrganizationUnitRepository
protected virtual async Task<IQueryable<IdentityUser>> CreateGetMembersFilteredQueryAsync( protected virtual async Task<IQueryable<IdentityUser>> CreateGetMembersFilteredQueryAsync(
OrganizationUnit organizationUnit, OrganizationUnit organizationUnit,
string filter = null, string filter = null,
bool includeChildren = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return (await GetQueryableAsync<IdentityUser>(cancellationToken)) return (await GetQueryableAsync<IdentityUser>(cancellationToken))

7
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

@ -267,6 +267,10 @@ public abstract class OrganizationUnitRepository_Tests<TStartupModule> : AbpIden
var usersCount = await _organizationUnitRepository.GetMembersCountAsync(ou); var usersCount = await _organizationUnitRepository.GetMembersCountAsync(ou);
usersCount.ShouldBeGreaterThan(1); usersCount.ShouldBeGreaterThan(1);
usersCount = await _organizationUnitRepository.GetMembersCountAsync(ou, includeChildren: true);
usersCount.ShouldBeGreaterThanOrEqualTo(2);
} }
[Fact] [Fact]
@ -301,6 +305,9 @@ public abstract class OrganizationUnitRepository_Tests<TStartupModule> : AbpIden
await _organizationUnitRepository.RemoveAllMembersAsync(ou); await _organizationUnitRepository.RemoveAllMembersAsync(ou);
var newCount = await _organizationUnitRepository.GetMembersCountAsync(ou); var newCount = await _organizationUnitRepository.GetMembersCountAsync(ou);
newCount.ShouldBe(0); newCount.ShouldBe(0);
newCount = await _organizationUnitRepository.GetMembersCountAsync(ou, includeChildren: true);
newCount.ShouldBe(0);
} }
[Fact] [Fact]

Loading…
Cancel
Save