Browse Source
Merge pull request #17377 from abpframework/liangshiwei/usercount
Add GetCountAsync method
pull/17378/head
maliming
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with
66 additions and
1 deletions
-
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs
-
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/RoleWithUserCount.cs
-
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
-
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
-
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/AbpIdentityTestDataBuilder.cs
-
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityTestData.cs
-
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs
|
|
|
@ -119,6 +119,11 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid> |
|
|
|
CancellationToken cancellationToken = default |
|
|
|
); |
|
|
|
|
|
|
|
Task<List<RoleWithUserCount>> GetCountAsync( |
|
|
|
Guid[] roleIds, |
|
|
|
CancellationToken cancellationToken = default |
|
|
|
); |
|
|
|
|
|
|
|
Task<IdentityUser> FindByTenantIdAndUserNameAsync( |
|
|
|
[NotNull] string userName, |
|
|
|
Guid? tenantId, |
|
|
|
|
|
|
|
@ -0,0 +1,10 @@ |
|
|
|
using System; |
|
|
|
|
|
|
|
namespace Volo.Abp.Identity; |
|
|
|
|
|
|
|
public class RoleWithUserCount |
|
|
|
{ |
|
|
|
public Guid RoleId { get; set; } |
|
|
|
|
|
|
|
public long UserCount { get; set; } |
|
|
|
} |
|
|
|
@ -263,6 +263,25 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext, |
|
|
|
.LongCountAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<RoleWithUserCount>> 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<RoleWithUserCount>(); |
|
|
|
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<List<OrganizationUnit>> GetOrganizationUnitsAsync( |
|
|
|
Guid id, |
|
|
|
bool includeDetails = false, |
|
|
|
|
|
|
|
@ -269,6 +269,25 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb |
|
|
|
.LongCountAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<RoleWithUserCount>> 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<RoleWithUserCount>(); |
|
|
|
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<List<IdentityUser>> GetUsersInOrganizationUnitAsync( |
|
|
|
Guid organizationUnitId, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
|
|
|
|
@ -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"); |
|
|
|
|
|
|
|
@ -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(); |
|
|
|
|
|
|
|
@ -135,6 +135,16 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : 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() |
|
|
|
|