Skip to content

Commit

Permalink
Merge pull request #17627 from abpframework/liangshiwei/export&import…
Browse files Browse the repository at this point in the history
…user

Add GetRoleNames by user ids method
  • Loading branch information
maliming authored Sep 14, 2023
2 parents 1919f5b + 7dc1100 commit 1eb01ee
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,8 @@ Task UpdateOrganizationAsync(
Guid? targetOrganizationId,
CancellationToken cancellationToken = default
);

Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ select ouRole.RoleId
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public virtual async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
.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,
Expand Down Expand Up @@ -360,4 +360,31 @@ public virtual async Task UpdateOrganizationAsync(Guid sourceOrganizationId, Gui

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ public async Task GetRoleNamesAsync()
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()
Expand Down

0 comments on commit 1eb01ee

Please sign in to comment.