Skip to content

Commit

Permalink
Add IdentityUserIdWithRoleNames
Browse files Browse the repository at this point in the history
  • Loading branch information
realLiangshiwei committed Sep 14, 2023
1 parent 510335c commit f22e1c2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Task UpdateOrganizationAsync(
CancellationToken cancellationToken = default
);

Task<Dictionary<Guid, List<string>>> GetRoleNamesAsync(
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,7 +56,7 @@ select ouRole.RoleId
return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken));
}

public virtual async Task<Dictionary<Guid, List<string>>> GetRoleNamesAsync(
public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default)
{
Expand All @@ -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<List<string>> GetRoleNamesInOrganizationUnitAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public virtual async Task UpdateOrganizationAsync(Guid sourceOrganizationId, Gui
await UpdateManyAsync(users, cancellationToken: cancellationToken);
}

public virtual async Task<Dictionary<Guid, List<string>>> GetRoleNamesAsync(
public virtual async Task<List<IdentityUserIdWithRoleNames>> GetRoleNamesAsync(
IEnumerable<Guid> userIds,
CancellationToken cancellationToken = default)
{
Expand All @@ -383,6 +383,8 @@ public virtual async Task<Dictionary<Guid, List<string>>> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit f22e1c2

Please sign in to comment.