From 431fe807eb392143789253b8d677087ffcb59c58 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Wed, 20 May 2020 11:40:26 +0300 Subject: [PATCH] Updated IOrganizationUnitRepository Updated GetRolesAsync with paging. added test --- .../Identity/IOrganizationUnitRepository.cs | 27 +++---- .../EfCoreOrganizationUnitRepository.cs | 52 ++++++++------ .../MongoOrganizationUnitRepository.cs | 47 +++++++------ .../OrganizationUnitRepository_Tests.cs | 70 +++++++++++-------- 4 files changed, 111 insertions(+), 85 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 248e663413..c4867420ed 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 @@ -19,12 +19,12 @@ namespace Volo.Abp.Identity Guid? parentId, bool includeDetails = false, CancellationToken cancellationToken = default - ); - - Task GetAsync( - string displayName, - bool includeDetails = true, - CancellationToken cancellationToken = default + ); + + Task GetAsync( + string displayName, + bool includeDetails = true, + CancellationToken cancellationToken = default ); Task> GetListAsync( @@ -33,16 +33,19 @@ namespace Volo.Abp.Identity int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default - ); - - Task> GetListAsync( - IEnumerable ids, - bool includeDetails = false, - CancellationToken cancellationToken = default + ); + + Task> GetListAsync( + IEnumerable ids, + bool includeDetails = false, + CancellationToken cancellationToken = default ); Task> GetRolesAsync( OrganizationUnit organizationUnit, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, bool includeDetails = false, 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 f00b69bb30..180d011810 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 @@ -55,10 +55,10 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) .PageBy(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); - } - public virtual async Task> GetListAsync( + } + public virtual async Task> GetListAsync( IEnumerable ids, - bool includeDetails = false, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await DbSet @@ -78,10 +78,13 @@ namespace Volo.Abp.Identity.EntityFrameworkCore ou => ou.DisplayName == displayName, GetCancellationToken(cancellationToken) ); - } - + } + public virtual async Task> GetRolesAsync( - OrganizationUnit organizationUnit, + OrganizationUnit organizationUnit, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default) { @@ -89,6 +92,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore 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); return await query.ToListAsync(GetCancellationToken(cancellationToken)); } @@ -108,35 +114,35 @@ namespace Volo.Abp.Identity.EntityFrameworkCore where userOu.OrganizationUnitId == organizationUnit.Id select user; - if (!filter.IsNullOrWhiteSpace()) - { - query = query.Where(u => - u.UserName.Contains(filter) || - u.Email.Contains(filter) || - (u.PhoneNumber != null && u.PhoneNumber.Contains(filter)) - ); + if (!filter.IsNullOrWhiteSpace()) + { + query = query.Where(u => + u.UserName.Contains(filter) || + u.Email.Contains(filter) || + (u.PhoneNumber != null && u.PhoneNumber.Contains(filter)) + ); } return await query.OrderBy(sorting ?? nameof(IdentityUser.UserName)) .PageBy(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public virtual async Task GetMembersCountAsync( - OrganizationUnit organizationUnit, - CancellationToken cancellationToken = default) - { + } + + public virtual async Task GetMembersCountAsync( + OrganizationUnit organizationUnit, + CancellationToken cancellationToken = default) + { var query = from userOu in DbContext.Set() join user in DbContext.Users on userOu.UserId equals user.Id where userOu.OrganizationUnitId == organizationUnit.Id - select user; - - return await query.CountAsync(GetCancellationToken(cancellationToken)); + select user; + + return await query.CountAsync(GetCancellationToken(cancellationToken)); } public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); - } + } } } 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 102096f79f..666af68e8b 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 @@ -38,11 +38,11 @@ namespace Volo.Abp.Identity.MongoDB return await GetMongoQueryable() .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public virtual async Task> GetListAsync( + } + + public virtual async Task> GetListAsync( IEnumerable ids, - bool includeDetails = false, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await GetMongoQueryable() @@ -78,19 +78,26 @@ namespace Volo.Abp.Identity.MongoDB public virtual async Task> GetRolesAsync( OrganizationUnit organizationUnit, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default) { var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(cancellationToken); - } - public virtual async Task> GetMembersAsync( - OrganizationUnit organizationUnit, - string sorting = null, - int maxResultCount = int.MaxValue, - int skipCount = 0, - string filter = null, - bool includeDetails = false, + return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) + .OrderBy(sorting ?? nameof(IdentityRole.Name)) + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(cancellationToken); + } + public virtual async Task> GetMembersAsync( + OrganizationUnit organizationUnit, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + string filter = null, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await DbContext.Users.AsQueryable() @@ -105,14 +112,14 @@ namespace Volo.Abp.Identity.MongoDB .As>() .PageBy>(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public virtual async Task GetMembersCountAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) - { + } + + public virtual async Task GetMembersCountAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) + { return await DbContext.Users.AsQueryable() .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) - .As>() - .CountAsync(GetCancellationToken(cancellationToken)); - } + .As>() + .CountAsync(GetCancellationToken(cancellationToken)); + } } } 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 1b8dac865a..a6cd719e1f 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 @@ -1,13 +1,13 @@ using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Shouldly; -using System; -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp.Guids; using Volo.Abp.Modularity; -using Volo.Abp.Uow; +using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.Identity @@ -18,7 +18,7 @@ namespace Volo.Abp.Identity private readonly IOrganizationUnitRepository _organizationUnitRepository; private readonly ILookupNormalizer _lookupNormalizer; private readonly IdentityTestData _testData; - private readonly IGuidGenerator _guidGenerator; + private readonly IGuidGenerator _guidGenerator; private readonly OrganizationUnitManager _organizationUnitManager; private readonly IIdentityRoleRepository _identityRoleRepository; private readonly IUnitOfWorkManager _unitOfWorkManager; @@ -32,8 +32,8 @@ namespace Volo.Abp.Identity _guidGenerator = GetRequiredService(); _organizationUnitManager = GetRequiredService(); _identityRoleRepository = GetRequiredService(); - _unitOfWorkManager = GetRequiredService(); - _identityUserRepository = GetRequiredService(); + _unitOfWorkManager = GetRequiredService(); + _identityUserRepository = GetRequiredService(); } [Fact] @@ -78,36 +78,46 @@ namespace Volo.Abp.Identity .FirstOrDefault(ou => ou.DisplayName == "OU111"); ou.Roles.ShouldNotBeNull(); ou.Roles.Any().ShouldBeTrue(); - } + } [Fact] public async Task GetOrganizationUnitRolesAsync() { - OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", includeDetails: true); - var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, true); + var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, includeDetails: true); ou111Roles.Count.ShouldBe(2); ou111Roles.ShouldContain(n => n.Name == "manager"); ou111Roles.ShouldContain(n => n.Name == "moderator"); } [Fact] - public async Task GetMembersInOrganizationUnitListAsync() - { - 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 }); - users.Count.ShouldBeGreaterThan(0); + public async Task GetOrganizationUnitRolesWithPagingAsync() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", includeDetails: true); + + var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, sorting: "name desc", maxResultCount: 1, includeDetails: true); + ou111Roles.Count.ShouldBe(1); + ou111Roles.ShouldContain(n => n.Name == "moderator"); + } + + [Fact] + public async Task GetMembersInOrganizationUnitListAsync() + { + 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 }); + users.Count.ShouldBeGreaterThan(0); } [Fact] - public async Task GetMembersInOrganizationUnitWithParamsAsync() - { - OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); - var users = await _organizationUnitRepository.GetMembersAsync(ou, "UserName DESC", 5, 0, "n"); - + public async Task GetMembersInOrganizationUnitWithParamsAsync() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var users = await _organizationUnitRepository.GetMembersAsync(ou, "UserName DESC", 5, 0, "n"); + users.Count.ShouldBeGreaterThan(1); - users.Count.ShouldBeLessThanOrEqualTo(5); - - //Filter check + users.Count.ShouldBeLessThanOrEqualTo(5); + + //Filter check users.ShouldAllBe(u => u.UserName.Contains("ne") || u.Email.Contains("n")); //Order check @@ -121,16 +131,16 @@ namespace Volo.Abp.Identity } users = await _organizationUnitRepository.GetMembersAsync(ou, null, 999, 0, "undefined-username"); - users.Count.ShouldBe(0); + users.Count.ShouldBe(0); } [Fact] - public async Task GetCountMembersCountInOrganizationUnit() - { - OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); - var users = await _organizationUnitRepository.GetMembersCountAsync(ou); - - users.ShouldBeGreaterThan(1); + public async Task GetCountMembersCountInOrganizationUnit() + { + OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true); + var users = await _organizationUnitRepository.GetMembersCountAsync(ou); + + users.ShouldBeGreaterThan(1); } } }