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 bf39874e00..0a69e90eae 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 @@ -28,9 +28,11 @@ namespace Volo.Abp.Identity ); Task> GetListAsync( + Guid? parentId, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, + string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default ); @@ -112,5 +114,10 @@ namespace Volo.Abp.Identity OrganizationUnit organizationUnit, CancellationToken cancellationToken = default ); + + Task GetLongCountAsync( + Guid? parentId, + string filter = null, + CancellationToken cancellationToken = default); } } 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 59fbd9518e..5b5ad8a6cf 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 @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.IO; using System.Linq.Dynamic.Core; using System.Linq; using System.Threading; @@ -44,14 +45,20 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task> GetListAsync( + Guid? parentId, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, + string filter = null, bool includeDetails = true, CancellationToken cancellationToken = default) { return await DbSet .IncludeDetails(includeDetails) + .Where(ou=>ou.ParentId==parentId) + .WhereIf(!filter.IsNullOrWhiteSpace(), + ou => ou.DisplayName.Contains(filter) || + ou.Code.Contains(filter)) .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) .PageBy(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); @@ -90,9 +97,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore CancellationToken cancellationToken = default) { var query = from organizationRole in DbContext.Set() - join role in DbContext.Roles.IncludeDetails(includeDetails) on organizationRole.RoleId equals role.Id - where organizationRole.OrganizationUnitId == organizationUnit.Id - select role; + join role in DbContext.Roles.IncludeDetails(includeDetails) on organizationRole.RoleId equals role.Id + where organizationRole.OrganizationUnitId == organizationUnit.Id + select role; query = query .OrderBy(sorting ?? nameof(IdentityRole.Name)) .PageBy(skipCount, maxResultCount); @@ -105,9 +112,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore 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 - select role; + join role in DbContext.Roles on organizationRole.RoleId equals role.Id + where organizationRole.OrganizationUnitId == organizationUnit.Id + select role; return await query.CountAsync(GetCancellationToken(cancellationToken)); } @@ -157,8 +164,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore var query = CreateGetMembersFilteredQuery(organizationUnit, filter); return await query.IncludeDetails(includeDetails).OrderBy(sorting ?? nameof(IdentityUser.UserName)) - .PageBy(skipCount, maxResultCount) - .ToListAsync(GetCancellationToken(cancellationToken)); + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task GetMembersCountAsync( @@ -245,7 +252,19 @@ namespace Volo.Abp.Identity.EntityFrameworkCore DbContext.Set().RemoveRange(ouMembersQuery); } - protected virtual IQueryable CreateGetMembersFilteredQuery(OrganizationUnit organizationUnit, string filter = null) + public virtual async Task GetLongCountAsync(Guid? parentId, string filter = null, + CancellationToken cancellationToken = default) + { + return await DbSet + .Where(ou=>ou.ParentId==parentId) + .WhereIf(!filter.IsNullOrWhiteSpace(), ou => + ou.DisplayName.Contains(filter) || + ou.Code.Contains(filter)) + .LongCountAsync(GetCancellationToken(cancellationToken)); + } + + protected virtual IQueryable CreateGetMembersFilteredQuery(OrganizationUnit organizationUnit, + string filter = null) { var query = from userOu in DbContext.Set() join user in DbContext.Users on userOu.UserId equals user.Id @@ -264,4 +283,4 @@ namespace Volo.Abp.Identity.EntityFrameworkCore return query; } } -} +} \ No newline at end of file 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 10ae4cd038..f6ae630eda 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 @@ -16,7 +16,7 @@ namespace Volo.Abp.Identity.MongoDB { public class MongoOrganizationUnitRepository : MongoDbRepository, - IOrganizationUnitRepository + IOrganizationUnitRepository { public MongoOrganizationUnitRepository( IMongoDbContextProvider dbContextProvider) @@ -41,8 +41,8 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { return await GetMongoQueryable() - .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) - .ToListAsync(GetCancellationToken(cancellationToken)); + .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) + .ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetListAsync( @@ -51,22 +51,28 @@ namespace Volo.Abp.Identity.MongoDB CancellationToken cancellationToken = default) { return await GetMongoQueryable() - .Where(t => ids.Contains(t.Id)) - .ToListAsync(GetCancellationToken(cancellationToken)); + .Where(t => ids.Contains(t.Id)) + .ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task> GetListAsync( + Guid? parentId, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, + string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default) { return await GetMongoQueryable() - .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) - .As>() - .PageBy>(skipCount, maxResultCount) - .ToListAsync(GetCancellationToken(cancellationToken)); + .Where(ou=>ou.ParentId==parentId) + .WhereIf(!filter.IsNullOrWhiteSpace(), + ou => ou.DisplayName.Contains(filter) || + ou.Code.Contains(filter)) + .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task GetAsync( @@ -166,7 +172,6 @@ namespace Volo.Abp.Identity.MongoDB return await query.CountAsync(GetCancellationToken(cancellationToken)); } - public async Task> GetUnaddedUsersAsync( OrganizationUnit organizationUnit, string sorting = null, @@ -213,7 +218,8 @@ namespace Volo.Abp.Identity.MongoDB return Task.FromResult(0); } - public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + 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)) @@ -227,7 +233,19 @@ namespace Volo.Abp.Identity.MongoDB } } - protected virtual IMongoQueryable CreateGetMembersFilteredQuery(OrganizationUnit organizationUnit, string filter = null) + public virtual async Task GetLongCountAsync(Guid? parentId, string filter = null, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .Where(ou=>ou.ParentId==parentId) + .WhereIf>(!filter.IsNullOrWhiteSpace(), ou => + ou.DisplayName.Contains(filter) || + ou.Code.Contains(filter)) + .LongCountAsync(GetCancellationToken(cancellationToken)); + } + + protected virtual IMongoQueryable CreateGetMembersFilteredQuery(OrganizationUnit organizationUnit, + string filter = null) { return DbContext.Users.AsQueryable() .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) @@ -240,4 +258,4 @@ namespace Volo.Abp.Identity.MongoDB ); } } -} +} \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs index 5d64bb6c78..b32c830002 100644 --- a/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityRoleAppService_Tests.cs @@ -11,11 +11,14 @@ namespace Volo.Abp.Identity { private readonly IIdentityRoleAppService _roleAppService; private readonly IIdentityRoleRepository _roleRepository; - + private readonly IOrganizationUnitRepository _organizationUnitRepository; + private readonly OrganizationUnitManager _organization; public IdentityRoleAppService_Tests() { _roleAppService = GetRequiredService(); _roleRepository = GetRequiredService(); + _organization = GetRequiredService(); + _organizationUnitRepository=GetRequiredService(); } [Fact] @@ -81,6 +84,31 @@ namespace Volo.Abp.Identity role.Name.ShouldBe(input.Name); } + [Fact] + public async Task CreateWithDetailsAsync() + { + //Arrange + var input = new IdentityRoleCreateDto + { + Name = Guid.NewGuid().ToString("N").Left(8) + }; + + var orgInput=new OrganizationUnit( + _organization.GuidGenerator.Create(), + Guid.NewGuid().ToString("N").Left(8) + ); + + //Act + var result = await _roleAppService.CreateAsync(input); + + await _organization.CreateAsync(orgInput); + + var role = await _roleRepository.GetAsync(result.Id); + await _organization.AddRoleToOrganizationUnitAsync(role,orgInput); + //Assert + orgInput.Roles.Count.ShouldBeGreaterThan(0); + } + [Fact] public async Task UpdateAsync() { 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 a671ba3284..d63daf1646 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 @@ -59,6 +59,22 @@ namespace Volo.Abp.Identity var ous = await _organizationUnitRepository.GetListAsync(ouIds); ous.Count.ShouldBe(2); ous.ShouldContain(ou => ou.Id == ouIds.First()); + + var ou11 = await _organizationUnitRepository.GetAsync("OU11"); + ou11.ShouldNotBeNull(); + var ou11Children = await _organizationUnitRepository.GetListAsync(ou11.Id, includeDetails: true); + ou11Children.Count.ShouldBe(2); + } + + [Fact] + public async Task GetLongCountAsync() + { + (await _organizationUnitRepository.GetLongCountAsync(_guidGenerator.Create(), filter: "11")).ShouldBe(0); + var countRoot = await _organizationUnitRepository.GetLongCountAsync(null, filter: "1"); + countRoot.ShouldBe(1); + var ou11 = await _organizationUnitRepository.GetAsync("OU11"); + ou11.ShouldNotBeNull(); + (await _organizationUnitRepository.GetLongCountAsync(ou11.Id, "2")).ShouldBe(1); } [Fact] @@ -192,7 +208,7 @@ namespace Volo.Abp.Identity { OrganizationUnit ou1 = await _organizationUnitRepository.GetAsync("OU111", true); OrganizationUnit ou2 = await _organizationUnitRepository.GetAsync("OU112", true); - var users = await _identityUserRepository.GetUsersInOrganizationsListAsync(new List {ou1.Id, ou2.Id}); + var users = await _identityUserRepository.GetUsersInOrganizationsListAsync(new List { ou1.Id, ou2.Id }); users.Count.ShouldBeGreaterThan(0); }