From cd587699b93609e78371ea0b54df3314d2406b64 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Wed, 16 Aug 2023 14:16:31 +0800 Subject: [PATCH] Add GetCountAsync method --- .../Abp/Identity/IIdentityUserRepository.cs | 5 +++++ .../Volo/Abp/Identity/RoleWithUserCount.cs | 10 ++++++++++ .../EfCoreIdentityUserRepository.cs | 19 +++++++++++++++++++ .../MongoDB/MongoIdentityUserRepository.cs | 19 +++++++++++++++++++ .../Identity/AbpIdentityTestDataBuilder.cs | 2 +- .../Volo/Abp/Identity/IdentityTestData.cs | 2 ++ .../Identity/IdentityUserRepository_Tests.cs | 10 ++++++++++ 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RoleWithUserCount.cs 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 29d6af4e3f..7aff7c5584 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 @@ -119,6 +119,11 @@ public interface IIdentityUserRepository : IBasicRepository CancellationToken cancellationToken = default ); + Task> GetCountAsync( + Guid[] roleIds, + CancellationToken cancellationToken = default + ); + Task FindByTenantIdAndUserNameAsync( [NotNull] string userName, Guid? tenantId, diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RoleWithUserCount.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RoleWithUserCount.cs new file mode 100644 index 0000000000..4030ce3fad --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RoleWithUserCount.cs @@ -0,0 +1,10 @@ +using System; + +namespace Volo.Abp.Identity; + +public class RoleWithUserCount +{ + public Guid RoleId { get; set; } + + public long UserCount { get; set; } +} 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 8e6e9cba6c..9aa6b133c5 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 @@ -263,6 +263,25 @@ public class EfCoreIdentityUserRepository : EfCoreRepository> GetCountAsync(Guid[] roleIds, CancellationToken cancellationToken = default) + { + var users = await (await GetDbSetAsync()) + .AsNoTracking() + .Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId))).IncludeDetails().ToListAsync(GetCancellationToken(cancellationToken)); + + var result = new List(); + foreach (var roleId in roleIds) + { + result.Add(new RoleWithUserCount + { + RoleId = roleId, + UserCount = users.Count(t => t.Roles.Any(role => role.RoleId == roleId)) + }); + } + + return result; + } + public virtual async Task> GetOrganizationUnitsAsync( Guid id, bool includeDetails = false, 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 b37112c317..b4a8d935fb 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 @@ -269,6 +269,25 @@ public class MongoIdentityUserRepository : MongoDbRepository> GetCountAsync(Guid[] roleIds, CancellationToken cancellationToken = default) + { + var users = await (await GetMongoQueryableAsync(cancellationToken)) + .Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId))) + .ToListAsync(GetCancellationToken(cancellationToken)); + + var result = new List(); + foreach (var roleId in roleIds) + { + result.Add(new RoleWithUserCount + { + RoleId = roleId, + UserCount = users.Count(t => t.Roles.Any(role => role.RoleId == roleId)) + }); + } + + return result; + } + public virtual async Task> GetUsersInOrganizationUnitAsync( Guid organizationUnitId, CancellationToken cancellationToken = default) diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs index dbd2e89fbf..7b5ffd259b 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs @@ -80,7 +80,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency _moderatorRole.AddClaim(_guidGenerator, new Claim("test-claim", "test-value")); await _roleRepository.InsertAsync(_moderatorRole); - _supporterRole = new IdentityRole(_guidGenerator.Create(), "supporter"); + _supporterRole = new IdentityRole(_testData.RoleSupporterId, "supporter"); await _roleRepository.InsertAsync(_supporterRole); _managerRole = new IdentityRole(_guidGenerator.Create(), "manager"); diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityTestData.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityTestData.cs index 24a435aca1..4862450e35 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityTestData.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityTestData.cs @@ -6,6 +6,8 @@ namespace Volo.Abp.Identity; public class IdentityTestData : ISingletonDependency { public Guid RoleModeratorId { get; } = Guid.NewGuid(); + + public Guid RoleSupporterId { get; } = Guid.NewGuid(); public Guid UserJohnId { get; } = Guid.NewGuid(); public Guid UserDavidId { get; } = Guid.NewGuid(); diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs index cb08fad60d..453917b08f 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs @@ -135,6 +135,16 @@ public abstract class IdentityUserRepository_Tests : AbpIdentity (await UserRepository.GetCountAsync("n")).ShouldBeGreaterThan(1); (await UserRepository.GetCountAsync("undefined-username")).ShouldBe(0); } + + [Fact] + public async Task GetCountAsync_With_RoleIds() + { + var roleWithUserCounts = await UserRepository.GetCountAsync(roleIds: new []{ TestData.RoleModeratorId, TestData.RoleSupporterId }); + + roleWithUserCounts.Count.ShouldBe(2); + roleWithUserCounts.ShouldContain(e => e.RoleId == TestData.RoleModeratorId && e.UserCount == 1); + roleWithUserCounts.ShouldContain(e => e.RoleId == TestData.RoleSupporterId && e.UserCount == 2); + } [Fact] public async Task GetUsersInOrganizationUnitAsync()