From 2c1d6bfbf5d4923b2bd16004634dd4789dbef2a6 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Fri, 29 May 2020 23:54:07 +0300 Subject: [PATCH] added RemoveAllRoles and RemoveAllMembers to organizationUnitRepository --- .../Identity/IOrganizationUnitRepository.cs | 11 ++++++ .../Volo/Abp/Identity/IdentityUserManager.cs | 1 - .../EfCoreIdentityUserRepository.cs | 29 +++++++--------- .../EfCoreOrganizationUnitRepository.cs | 34 ++++++++++++++++++- .../MongoOrganizationUnitRepository.cs | 34 +++++++++++++++++-- .../OrganizationUnitRepository_Tests.cs | 24 +++++++++++++ 6 files changed, 113 insertions(+), 20 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs index def41cdc8a..bf698ca54f 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs @@ -64,9 +64,20 @@ namespace Volo.Abp.Identity bool includeDetails = false, CancellationToken cancellationToken = default ); + Task GetMembersCountAsync( OrganizationUnit organizationUnit, CancellationToken cancellationToken = default ); + + Task RemoveAllRolesAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default + ); + + Task RemoveAllMembersAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default + ); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index f90694349a..d22eef5af6 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -96,7 +96,6 @@ namespace Volo.Abp.Identity return IdentityResult.Success; } - public virtual async Task IsInOrganizationUnitAsync(Guid userId, Guid ouId) { var user = await IdentityUserRepository.GetAsync(userId, cancellationToken: CancellationToken); 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 c1bee0449a..f2e82cfb77 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 @@ -152,15 +152,16 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userOrg.UserId == id select ou; - var orgUserRoleQuery = DbContext.Set().Where(q => userOrganizationsQuery.Select(t => t.Id).Contains(q.OrganizationUnitId)) + var orgUserRoleQuery = DbContext.Set() + .Where(q => userOrganizationsQuery + .Select(t => t.Id) + .Contains(q.OrganizationUnitId)) .Select(t => t.RoleId); var orgRoles = DbContext.Roles.Where(q => orgUserRoleQuery.Contains(q.Id)); var resultQuery = query.Union(orgRoles); return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); - - //return await query.ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task GetCountAsync( @@ -201,22 +202,18 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userOu.OrganizationUnitId == organizationUnitId select user; return await query.ToListAsync(GetCancellationToken(cancellationToken)); - } - - public async Task> GetUsersInOrganizationsListAsync( - List organizationUnitIds, - CancellationToken cancellationToken = default - ) - { - //var userIds = DbContext.Set() - // .Where(q => organizationUnitIds.Contains(q.OrganizationUnitId)) - // .Select(u => u.UserId); - //var query = DbContext.Users.Where(u => userIds.Contains(u.Id)); + } + + public async Task> GetUsersInOrganizationsListAsync( + List organizationUnitIds, + CancellationToken cancellationToken = default + ) + { var query = from userOu in DbContext.Set() join user in DbSet on userOu.UserId equals user.Id where organizationUnitIds.Contains(userOu.OrganizationUnitId) select user; - return await query.ToListAsync(GetCancellationToken(cancellationToken)); + return await query.ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetUsersInOrganizationUnitWithChildrenAsync( @@ -235,6 +232,6 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); - } + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs index 350f64ffe7..812c655140 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs @@ -102,7 +102,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore public virtual async Task GetRolesCountAsync( OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) - { + { var query = from organizationRole in DbContext.Set() join role in DbContext.Roles on organizationRole.RoleId equals role.Id where organizationRole.OrganizationUnitId == organizationUnit.Id @@ -156,5 +156,37 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { return GetQueryable().IncludeDetails(); } + + public virtual Task RemoveAllRolesAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default) + { + var ouRolesQuery = DbContext.Set() + .Where(q => q.OrganizationUnitId == organizationUnit.Id); + + DbContext.Set().RemoveRange(ouRolesQuery); + return Task.FromResult(0); + //Can be long running process that could be made available for cancellation perhaps + //return Task.Run(() => + // DbContext.Set().RemoveRange(ouRolesQuery), + // GetCancellationToken(cancellationToken) + //); + } + + public virtual Task RemoveAllMembersAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default) + { + var ouMembersQuery = DbContext.Set() + .Where(q => q.OrganizationUnitId == organizationUnit.Id); + + DbContext.Set().RemoveRange(ouMembersQuery); + return Task.FromResult(0); + //Can be long running process that could be made available for cancellation perhaps + //return Task.Run(() => + // DbContext.Set().RemoveRange(ouMembersQuery), + // GetCancellationToken(cancellationToken) + //); + } } } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index ae735300fd..4c63ce703d 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -1,17 +1,22 @@ -using MongoDB.Driver; +using MongoDB.Bson; +using MongoDB.Driver; using MongoDB.Driver.Linq; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; +using Volo.Abp.Uow; namespace Volo.Abp.Identity.MongoDB { - public class MongoOrganizationUnitRepository : MongoDbRepository, IOrganizationUnitRepository + public class MongoOrganizationUnitRepository + : MongoDbRepository, + IOrganizationUnitRepository { public MongoOrganizationUnitRepository( IMongoDbContextProvider dbContextProvider) @@ -134,5 +139,30 @@ namespace Volo.Abp.Identity.MongoDB .As>() .CountAsync(GetCancellationToken(cancellationToken)); } + + public virtual Task RemoveAllRolesAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + { + organizationUnit.Roles.Clear(); + return Task.FromResult(0); + } + + public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + { + var users = await DbContext.Users.AsQueryable() + .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) + .As>() + .ToListAsync(GetCancellationToken(cancellationToken)) + ; + + //var filter = Builders.Filter.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)); + + //var update = Builders.Update.PullFilter(y => y.OrganizationUnits, iou=> iou); + //var result = await DbContext.Users.UpdateManyAsync(filter, update); + + for (int i = 0; i < users.Count; i++) + { + users[i].RemoveOrganizationUnit(organizationUnit.Id); + } + } } } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs index 6f6835396d..bbb7d649c9 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs @@ -151,5 +151,29 @@ namespace Volo.Abp.Identity rolesCount.ShouldBeGreaterThan(1); } + + [Fact] + public async Task RemoveAllMembersOfOrganizationUnit() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var membersCount = await _organizationUnitRepository.GetMembersCountAsync(ou); + membersCount.ShouldBeGreaterThan(1); + + await _organizationUnitRepository.RemoveAllMembersAsync(ou); + var newCount = await _organizationUnitRepository.GetMembersCountAsync(ou); + newCount.ShouldBe(0); + } + + [Fact] + public async Task RemoveAllRolesOfOrganizationUnit() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var rolesCount = await _organizationUnitRepository.GetRolesCountAsync(ou); + rolesCount.ShouldBeGreaterThan(1); + + await _organizationUnitRepository.RemoveAllRolesAsync(ou); + var newCount = await _organizationUnitRepository.GetRolesCountAsync(ou); + newCount.ShouldBe(0); + } } }