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 196c26da355..1a5281e44ce 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 @@ -144,7 +144,7 @@ Task UpdateOrganizationAsync( CancellationToken cancellationToken = default ); - Task>> GetRoleNamesAsync( + Task> GetRoleNamesAsync( IEnumerable userIds, CancellationToken cancellationToken = default); } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserIdWithRoleNames.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserIdWithRoleNames.cs new file mode 100644 index 00000000000..88d18ffbc49 --- /dev/null +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserIdWithRoleNames.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Identity; + +public class IdentityUserIdWithRoleNames +{ + public Guid Id { get; set; } + + public string[] RoleNames { get; set; } +} \ No newline at end of file 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 28a3277d3d7..65a6ebaa644 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 @@ -56,7 +56,7 @@ select ouRole.RoleId return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); } - public virtual async Task>> GetRoleNamesAsync( + public virtual async Task> GetRoleNamesAsync( IEnumerable userIds, CancellationToken cancellationToken = default) { @@ -72,9 +72,9 @@ where userIds.Contains(userRole.UserId) var result = await query .GroupBy(x => x.UserId) - .ToDictionaryAsync(x => x.Key, x => x.Select(r => r.Name).ToList(), cancellationToken: cancellationToken); + .ToDictionaryAsync(x => x.Key, x => x.Select(r => r.Name).ToArray(), cancellationToken: cancellationToken); - return result; + return result.Select(x => new IdentityUserIdWithRoleNames() { Id = x.Key, RoleNames = x.Value }).ToList(); } public virtual async Task> GetRoleNamesInOrganizationUnitAsync( 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 0ecb801f935..f5589d5dfa6 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 @@ -361,7 +361,7 @@ public virtual async Task UpdateOrganizationAsync(Guid sourceOrganizationId, Gui await UpdateManyAsync(users, cancellationToken: cancellationToken); } - public virtual async Task>> GetRoleNamesAsync( + public virtual async Task> GetRoleNamesAsync( IEnumerable userIds, CancellationToken cancellationToken = default) { @@ -383,6 +383,8 @@ public virtual async Task>> GetRoleNamesAsync( r.Name }).ToListAsync(cancellationToken); - return userAndRoleIds.ToDictionary(x => x.Key, x => roles.Where(r => x.Value.Contains(r.Id)).Select(r => r.Name).ToList()); + var result = userAndRoleIds.ToDictionary(x => x.Key, x => roles.Where(r => x.Value.Contains(r.Id)).Select(r => r.Name).ToArray()); + + return result.Select(x => new IdentityUserIdWithRoleNames() { Id = x.Key, RoleNames = x.Value }).ToList(); } } 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 091dae04495..2d2c354aaeb 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 @@ -64,14 +64,22 @@ public async Task GetRoleNames_By_UserIds_Async() TestData.UserDavidId }); - userRoleNames[TestData.UserBobId].Count.ShouldBe(1); - userRoleNames[TestData.UserBobId][0].ShouldBe("manager"); - userRoleNames[TestData.UserJohnId].Count.ShouldBe(2); - userRoleNames[TestData.UserJohnId].ShouldContain("moderator"); - userRoleNames[TestData.UserJohnId].ShouldContain("supporter"); - userRoleNames[TestData.UserNeoId].Count.ShouldBe(1); - userRoleNames[TestData.UserNeoId][0].ShouldBe("supporter"); - userRoleNames.ShouldNotContainKey(TestData.UserDavidId); + userRoleNames.Count.ShouldBe(3); + + var userBob = userRoleNames.First(x => x.Id == TestData.UserBobId); + userBob.RoleNames.Length.ShouldBe(1); + userBob.RoleNames[0].ShouldBe("manager"); + + var userJohn = userRoleNames.First(x => x.Id == TestData.UserJohnId); + userJohn.RoleNames.Length.ShouldBe(2); + userJohn.RoleNames.ShouldContain("moderator"); + userJohn.RoleNames.ShouldContain("supporter"); + + var userNeo = userRoleNames.First(x => x.Id == TestData.UserNeoId); + userNeo.RoleNames.Length.ShouldBe(1); + userNeo.RoleNames[0].ShouldBe("supporter"); + + userRoleNames.ShouldNotContain(x => x.Id == TestData.UserDavidId); } [Fact]