Skip to content

Commit

Permalink
Merge pull request #18754 from abpframework/liangshiwei/identity
Browse files Browse the repository at this point in the history
Filter users if a role comes from an organization unit
  • Loading branch information
oykuermann committed Jan 16, 2024
2 parents 596676d + c415180 commit 945068a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,19 @@ public virtual async Task<List<Guid>> GetUserIdListByRoleIdAsync(Guid roleId, Ca
DateTime? minModifitionTime = null,
CancellationToken cancellationToken = default)
{

var upperFilter = filter?.ToUpperInvariant();
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
var query = (await GetDbSetAsync())
.IncludeDetails(includeDetails);

if (roleId.HasValue)
{
var dbContext = await GetDbContextAsync();
var organizationUnitIds = await dbContext.Set<OrganizationUnitRole>().Where(q => q.RoleId == roleId.Value).Select(q => q.OrganizationUnitId).ToArrayAsync(cancellationToken: cancellationToken);
query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId)));
}

return await query
.WhereIf(
!filter.IsNullOrWhiteSpace(),
u =>
Expand All @@ -209,7 +219,6 @@ public virtual async Task<List<Guid>> GetUserIdListByRoleIdAsync(Guid roleId, Ca
(u.Surname != null && u.Surname.Contains(filter)) ||
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
)
.WhereIf(roleId.HasValue, identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value))
.WhereIf(organizationUnitId.HasValue, identityUser => identityUser.OrganizationUnits.Any(x => x.OrganizationUnitId == organizationUnitId.Value))
.WhereIf(!string.IsNullOrWhiteSpace(userName), x => x.UserName.Contains(userName))
.WhereIf(!string.IsNullOrWhiteSpace(phoneNumber), x => x.PhoneNumber.Contains(phoneNumber))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,19 @@ public virtual async Task<List<Guid>> GetUserIdListByRoleIdAsync(Guid roleId, Ca
CancellationToken cancellationToken = default)
{
var upperFilter = filter?.ToUpperInvariant();
return await (await GetMongoQueryableAsync(cancellationToken))
var query = await GetMongoQueryableAsync(cancellationToken);

if (roleId.HasValue)
{
var organizationUnitIds = (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Roles.Any(r => r.RoleId == roleId.Value))
.Select(userOrganizationUnit => userOrganizationUnit.Id)
.ToArray();

query = query.Where(identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value) || identityUser.OrganizationUnits.Any(x => organizationUnitIds.Contains(x.OrganizationUnitId)));
}

return await query
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(),
u =>
Expand All @@ -176,7 +188,6 @@ public virtual async Task<List<Guid>> GetUserIdListByRoleIdAsync(Guid roleId, Ca
(u.Surname != null && u.Surname.Contains(filter)) ||
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
)
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(roleId.HasValue, identityUser => identityUser.Roles.Any(x => x.RoleId == roleId.Value))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(organizationUnitId.HasValue, identityUser => identityUser.OrganizationUnits.Any(x => x.OrganizationUnitId == organizationUnitId.Value))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(userName), x => x.UserName.Contains(userName))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(!string.IsNullOrWhiteSpace(phoneNumber), x => x.PhoneNumber.Contains(phoneNumber))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public async Task GetListAsync()
StringComparison.OrdinalIgnoreCase
).ShouldBeGreaterThan(0);
}

users = await UserRepository.GetListAsync(null, 5, 0, null, roleId: TestData.RoleManagerId);
users.ShouldContain(x => x.UserName == "john.nash");
users.ShouldContain(x => x.UserName == "neo");

users = await UserRepository.GetListAsync(null, 999, 0, "undefined-username");
users.Count.ShouldBe(0);
Expand Down

0 comments on commit 945068a

Please sign in to comment.