Browse Source

Merge pull request #17627 from abpframework/liangshiwei/export&importuser

Add GetRoleNames by user ids method
pull/17635/head
maliming 3 years ago
committed by GitHub
parent
commit
1eb01ee6ab
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs
  2. 11
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserIdWithRoleNames.cs
  3. 20
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
  4. 29
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  5. 28
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs

4
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs

@ -143,4 +143,8 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
Guid? targetOrganizationId,
CancellationToken cancellationToken = default
);
Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default);
}

11
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; }
}

20
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs

@ -56,6 +56,26 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync();
return await (from userRole in dbContext.Set<IdentityUserRole>()
join role in dbContext.Roles on userRole.RoleId equals role.Id
where userIds.Contains(userRole.UserId)
group new
{
userRole.UserId,
role.Name
} by userRole.UserId
into gp
select new IdentityUserIdWithRoleNames
{
Id = gp.Key, RoleNames = gp.Select(x => x.Name).ToArray()
}).ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
Guid id,
CancellationToken cancellationToken = default)

29
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs

@ -131,7 +131,7 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
.Where(u => u.Roles.Any(r => r.RoleId == role.Id))
.ToListAsync(cancellationToken);
}
public virtual async Task<List<IdentityUser>> GetListAsync(
string sorting = null,
int maxResultCount = int.MaxValue,
@ -360,4 +360,31 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}
public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
var userAndRoleIds = (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
.Where(u => userIds.Contains(u.Id))
.SelectMany(u => u.Roles)
.Select(userRole => new
{
userRole.UserId,
userRole.RoleId
}).GroupBy(x => x.UserId).ToDictionary(x => x.Key, x => x.Select(r => r.RoleId).ToList());
var roleIds = userAndRoleIds.SelectMany(x => x.Value);
var roles = await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => roleIds.Contains(r.Id)).Select(r => new
{
r.Id,
r.Name
}).ToListAsync(cancellationToken);
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();
}
}

28
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs

@ -53,6 +53,34 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
roles.ShouldContain("supporter");
roles.ShouldContain("manager");
}
[Fact]
public async Task GetRoleNames_By_UserIds_Async()
{
var userRoleNames = await UserRepository.GetRoleNamesAsync(new [] {
TestData.UserBobId,
TestData.UserJohnId,
TestData.UserNeoId,
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]
public async Task FindByLoginAsync()

Loading…
Cancel
Save