Browse Source

get users by organization unit

pull/2563/head
Mehmet Perk 7 years ago
parent
commit
4db9a9a42e
  1. 10
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityUserRepository.cs
  2. 37
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs
  3. 2
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs
  4. 26
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs
  5. 2
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs
  6. 38
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  7. 2
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs
  8. 6
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs
  9. 32
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs
  10. 26
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityUserRepository_Tests.cs
  11. 2
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/LazyLoading_Tests.cs
  12. 4
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

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

@ -71,6 +71,16 @@ namespace Volo.Abp.Identity
bool includeDetails = false,
CancellationToken cancellationToken = default);
Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(
Guid organizationUnitId,
CancellationToken cancellationToken = default
);
Task<List<IdentityUser>> GetUsersInOrganizationUnitWithChildrenAsync(
string code,
CancellationToken cancellationToken = default
);
Task<long> GetCountAsync(
string filter = null,
CancellationToken cancellationToken = default

37
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs

@ -93,8 +93,8 @@ namespace Volo.Abp.Identity
public virtual async Task<bool> IsInOrganizationUnitAsync(Guid userId, Guid ouId)
{
return await IsInOrganizationUnitAsync(
await GetByIdAsync(userId),
await _organizationUnitRepository.GetAsync(ouId)
await GetByIdAsync(userId).ConfigureAwait(false),
await _organizationUnitRepository.GetAsync(ouId).ConfigureAwait(false)
);
}
@ -106,8 +106,8 @@ namespace Volo.Abp.Identity
public virtual async Task AddToOrganizationUnitAsync(Guid userId, Guid ouId)
{
await AddToOrganizationUnitAsync(
await _identityUserRepository.GetAsync(userId, true),
await _organizationUnitRepository.GetAsync(ouId)
await _identityUserRepository.GetAsync(userId, true).ConfigureAwait(false),
await _organizationUnitRepository.GetAsync(ouId).ConfigureAwait(false)
);
}
@ -130,8 +130,8 @@ namespace Volo.Abp.Identity
public virtual async Task RemoveFromOrganizationUnitAsync(Guid userId, Guid ouId)
{
await RemoveFromOrganizationUnitAsync(
await _identityUserRepository.GetAsync(userId, true),
await _organizationUnitRepository.GetAsync(ouId)
await _identityUserRepository.GetAsync(userId, true).ConfigureAwait(false),
await _organizationUnitRepository.GetAsync(ouId).ConfigureAwait(false)
);
}
@ -145,7 +145,7 @@ namespace Volo.Abp.Identity
public virtual async Task SetOrganizationUnitsAsync(Guid userId, params Guid[] organizationUnitIds)
{
await SetOrganizationUnitsAsync(
await _identityUserRepository.GetAsync(userId, true),
await _identityUserRepository.GetAsync(userId, true).ConfigureAwait(false),
organizationUnitIds
);
}
@ -164,7 +164,7 @@ namespace Volo.Abp.Identity
{
if (!organizationUnitIds.Contains(currentOu.Id))
{
await RemoveFromOrganizationUnitAsync(user.Id, currentOu.Id);
await RemoveFromOrganizationUnitAsync(user.Id, currentOu.Id).ConfigureAwait(false);
}
}
@ -175,7 +175,7 @@ namespace Volo.Abp.Identity
{
await AddToOrganizationUnitAsync(
user,
await _organizationUnitRepository.GetAsync(organizationUnitId)
await _organizationUnitRepository.GetAsync(organizationUnitId).ConfigureAwait(false)
);
}
}
@ -197,7 +197,24 @@ namespace Volo.Abp.Identity
var ouOfUser = user.OrganizationUnits;
return await _organizationUnitRepository.GetListAsync(ouOfUser.Select(t => t.OrganizationUnitId));
return await _organizationUnitRepository.GetListAsync(ouOfUser.Select(t => t.OrganizationUnitId)).ConfigureAwait(false);
}
[UnitOfWork]
public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(OrganizationUnit organizationUnit, bool includeChildren = false)
{
if (includeChildren)
{
return await _identityUserRepository
.GetUsersInOrganizationUnitWithChildrenAsync(organizationUnit.Code)
.ConfigureAwait(false);
}
else
{
return await _identityUserRepository
.GetUsersInOrganizationUnitAsync(organizationUnit.Id)
.ConfigureAwait(false);
}
}
}
}

2
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/Organizations/IOrganizationUnitRepository.cs

@ -16,7 +16,7 @@ namespace Volo.Abp.Identity.Organizations
Task<List<OrganizationUnit>> GetListAsync(bool includeDetails = true, CancellationToken cancellationToken = default);
Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default);
Task<OrganizationUnit> GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default);
}
}

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

@ -170,6 +170,32 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
return await query.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(
Guid organizationUnitId,
CancellationToken cancellationToken = default
)
{
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
join user in DbSet on userOu.UserId equals user.Id
where userOu.OrganizationUnitId == organizationUnitId
select user;
return await query.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitWithChildrenAsync(
string code,
CancellationToken cancellationToken = default
)
{
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
join user in DbSet on userOu.UserId equals user.Id
join ou in DbContext.Set<OrganizationUnit>() on userOu.OrganizationUnitId equals ou.Id
where ou.Code.StartsWith(code)
select user;
return await query.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public override IQueryable<IdentityUser> WithDetails()
{
return GetQueryable().IncludeDetails();

2
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs

@ -42,7 +42,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
public async Task<OrganizationUnit> GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await DbSet
.IncludeDetails(includeDetails)

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

@ -140,7 +140,10 @@ namespace Volo.Abp.Identity.MongoDB
{
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)).ConfigureAwait(false);
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);
return await DbContext.OrganizationUnits.AsQueryable().Where(ou => organizationUnitIds.Contains(ou.Id)).ToListAsync().ConfigureAwait(false);
return await DbContext.OrganizationUnits.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
public async Task<long> GetCountAsync(
@ -156,5 +159,38 @@ namespace Volo.Abp.Identity.MongoDB
)
.LongCountAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(
Guid organizationUnitId,
CancellationToken cancellationToken = default)
{
var organizationUnitUserIds = await DbContext.Users.AsQueryable()
.SelectMany(u => u.OrganizationUnits)
.Where(ou => ou.OrganizationUnitId == organizationUnitId)
.Select(ouu => ouu.Id)
.ToListAsync(GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
return await GetMongoQueryable()
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitUserIds.Contains(uou.Id)))
.ToListAsync(GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
public async Task<List<IdentityUser>> GetUsersInOrganizationUnitWithChildrenAsync(
string code,
CancellationToken cancellationToken = default)
{
var organizationUnitIds = await DbContext.OrganizationUnits.AsQueryable()
.Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id)
.ToListAsync(GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
return await GetMongoQueryable()
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))
.ToListAsync(GetCancellationToken(cancellationToken))
.ConfigureAwait(false);
}
}
}

2
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs

@ -44,7 +44,7 @@ namespace Volo.Abp.Identity.MongoDB
.Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
public async Task<OrganizationUnit> GetOrganizationUnitAsync(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await DbContext.OrganizationUnits.AsQueryable()
.FirstOrDefaultAsync(

6
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_Tests.cs

@ -97,7 +97,7 @@ namespace Volo.Abp.Identity
_lookupNormalizer.NormalizeName("david")).ConfigureAwait(false);
user.ShouldNotBeNull();
var ou = await _organizationUnitRepository.GetOrganizationUnit(
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync(
_lookupNormalizer.NormalizeName("OU11")).ConfigureAwait(false);
ou.ShouldNotBeNull();
@ -120,7 +120,7 @@ namespace Volo.Abp.Identity
{
using (var uow = _unitOfWorkManager.Begin())
{
var ou = await _organizationUnitRepository.GetOrganizationUnit(
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync(
_lookupNormalizer.NormalizeName("OU111")).ConfigureAwait(false);
ou.ShouldNotBeNull();
@ -128,7 +128,7 @@ namespace Volo.Abp.Identity
_lookupNormalizer.NormalizeName("john.nash")).ConfigureAwait(false);
user.ShouldNotBeNull();
var ouNew = await _organizationUnitRepository.GetOrganizationUnit(
var ouNew = await _organizationUnitRepository.GetOrganizationUnitAsync(
_lookupNormalizer.NormalizeName("OU2")).ConfigureAwait(false);
ouNew.ShouldNotBeNull();

32
modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/OrganizationUnitManager_Tests.cs

@ -31,55 +31,55 @@ namespace Volo.Abp.Identity
{
await _organizationUnitManager.CreateAsync(new OrganizationUnit(null, "Root 1"));
var root1 = await _organizationUnitRepository.GetOrganizationUnit("Root 1").ConfigureAwait(false);
var root1 = await _organizationUnitRepository.GetOrganizationUnitAsync("Root 1").ConfigureAwait(false);
root1.ShouldNotBeNull();
}
[Fact]
public async Task UpdateAsync()
{
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false);
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU111").ConfigureAwait(false);
ou.Code = OrganizationUnit.CreateCode(123);
await _organizationUnitManager.UpdateAsync(ou);
var ouAfterChange = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false);
var ouAfterChange = await _organizationUnitRepository.GetOrganizationUnitAsync("OU111").ConfigureAwait(false);
ouAfterChange.Code.ShouldContain("123");
}
[Fact]
public async Task DeleteAsync()
{
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false);
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU11").ConfigureAwait(false);
await _organizationUnitManager.DeleteAsync(ou.Id);
(await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false)).ShouldBeNull();
(await _organizationUnitRepository.GetOrganizationUnitAsync("OU11").ConfigureAwait(false)).ShouldBeNull();
}
[Fact]
public async Task MoveAsync()
{
var ou1 = await _organizationUnitRepository.GetOrganizationUnit("OU1").ConfigureAwait(false);
var ou2 = await _organizationUnitRepository.GetOrganizationUnit("OU2").ConfigureAwait(false);
var ou1 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1").ConfigureAwait(false);
var ou2 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU2").ConfigureAwait(false);
await _organizationUnitManager.MoveAsync(ou1.Id, ou2.Id);
ou1 = await _organizationUnitRepository.GetOrganizationUnit("OU1").ConfigureAwait(false);
ou1 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1").ConfigureAwait(false);
ou1.ParentId.ShouldBe(ou2.Id);
ou1.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2));
var ou11 = await _organizationUnitRepository.GetOrganizationUnit("OU11").ConfigureAwait(false);
var ou11 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU11").ConfigureAwait(false);
ou11.ParentId.ShouldBe(ou1.Id);
ou11.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1));
var ou111 = await _organizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false);
var ou111 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU111").ConfigureAwait(false);
ou111.ParentId.ShouldBe(ou11.Id);
ou111.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1, 1));
var ou112 = await _organizationUnitRepository.GetOrganizationUnit("OU112").ConfigureAwait(false);
var ou112 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU112").ConfigureAwait(false);
ou112.ParentId.ShouldBe(ou11.Id);
ou112.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 1, 2));
var ou12 = await _organizationUnitRepository.GetOrganizationUnit("OU12").ConfigureAwait(false);
var ou12 = await _organizationUnitRepository.GetOrganizationUnitAsync("OU12").ConfigureAwait(false);
ou12.ParentId.ShouldBe(ou1.Id);
ou12.Code.ShouldBe(OrganizationUnit.CreateCode(2, 2, 2));
}
@ -87,24 +87,24 @@ namespace Volo.Abp.Identity
[Fact]
public async Task AddRoleToOrganizationUnitAsync()
{
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", true).ConfigureAwait(false);
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1", true).ConfigureAwait(false);
var adminRole = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")).ConfigureAwait(false);
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole, ou);
//TODO: This method has a bug: add role not work
ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", includeDetails: true).ConfigureAwait(false);
ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1", includeDetails: true).ConfigureAwait(false);
ou.Roles.FirstOrDefault().Id.ShouldBe(adminRole.Id);
}
[Fact]
public async Task RemoveRoleFromOrganizationUnitAsync()
{
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", true).ConfigureAwait(false);
var ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1", true).ConfigureAwait(false);
var adminRole = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")).ConfigureAwait(false);
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole.Id, ou.Id);
await _organizationUnitManager.RemoveRoleFromOrganizationUnitAsync(adminRole.Id, ou.Id);
ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", includeDetails: true).ConfigureAwait(false);
ou = await _organizationUnitRepository.GetOrganizationUnitAsync("OU1", includeDetails: true).ConfigureAwait(false);
ou.Roles.FirstOrDefault(r => r.RoleId == adminRole.Id).ShouldBeNull();
}
}

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

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.Modularity;
using Xunit;
@ -15,11 +16,13 @@ namespace Volo.Abp.Identity
{
protected IIdentityUserRepository UserRepository { get; }
protected ILookupNormalizer LookupNormalizer { get; }
protected IOrganizationUnitRepository OrganizationUnitRepository { get; }
protected IdentityUserRepository_Tests()
{
UserRepository = ServiceProvider.GetRequiredService<IIdentityUserRepository>();
LookupNormalizer = ServiceProvider.GetRequiredService<ILookupNormalizer>();
OrganizationUnitRepository = ServiceProvider.GetRequiredService<IOrganizationUnitRepository>();
}
[Fact]
@ -128,6 +131,22 @@ namespace Volo.Abp.Identity
(await UserRepository.GetCountAsync("undefined-username").ConfigureAwait(false)).ShouldBe(0);
}
[Fact]
public async Task GetUsersInOrganizationUnitAsync()
{
var users = await UserRepository.GetUsersInOrganizationUnitAsync((await GetOU("OU111").ConfigureAwait(false)).Id).ConfigureAwait(false);
users.ShouldNotBeNull();
users.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task GetUsersInOrganizationUnitWithChildrenAsync()
{
var users = await UserRepository.GetUsersInOrganizationUnitWithChildrenAsync((await GetOU("OU111").ConfigureAwait(false)).Code).ConfigureAwait(false);
users.ShouldNotBeNull();
users.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task Should_Eager_Load_User_Collections()
{
@ -148,5 +167,12 @@ namespace Volo.Abp.Identity
john.OrganizationUnits.ShouldNotBeNull();
john.OrganizationUnits.Any().ShouldBeTrue();
}
private async Task<OrganizationUnit> GetOU(string diplayName)
{
var organizationUnit = await OrganizationUnitRepository.GetOrganizationUnitAsync(diplayName).ConfigureAwait(false);
organizationUnit.ShouldNotBeNull();
return organizationUnit;
}
}
}

2
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/LazyLoading_Tests.cs

@ -70,7 +70,7 @@ namespace Volo.Abp.Identity
{
using (var uow = GetRequiredService<IUnitOfWorkManager>().Begin())
{
var ou = await OrganizationUnitRepository.GetOrganizationUnit(LookupNormalizer.NormalizeName("OU111"), includeDetails: false).ConfigureAwait(false);
var ou = await OrganizationUnitRepository.GetOrganizationUnitAsync(LookupNormalizer.NormalizeName("OU111"), includeDetails: false).ConfigureAwait(false);
ou.Roles.ShouldNotBeNull(); //?
ou.Roles.Any().ShouldBeTrue();

4
modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

@ -52,9 +52,9 @@ namespace Volo.Abp.Identity
}
[Fact]
public async Task GetOrganizationUnit()
public async Task GetOrganizationUnitAsync()
{
var organizationUnit = await OrganizationUnitRepository.GetOrganizationUnit("OU111").ConfigureAwait(false);
var organizationUnit = await OrganizationUnitRepository.GetOrganizationUnitAsync("OU111").ConfigureAwait(false);
organizationUnit.ShouldNotBeNull();
}

Loading…
Cancel
Save