Browse Source

Updated IOrganizationUnitRepository

Updated GetRolesAsync with paging. added test
pull/3732/head
Galip Tolga Erdem 7 years ago
parent
commit
431fe807eb
  1. 27
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs
  2. 52
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs
  3. 47
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs
  4. 70
      modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/OrganizationUnitRepository_Tests.cs

27
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs

@ -19,12 +19,12 @@ namespace Volo.Abp.Identity
Guid? parentId,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task<OrganizationUnit> GetAsync(
string displayName,
bool includeDetails = true,
CancellationToken cancellationToken = default
);
Task<OrganizationUnit> GetAsync(
string displayName,
bool includeDetails = true,
CancellationToken cancellationToken = default
);
Task<List<OrganizationUnit>> GetListAsync(
@ -33,16 +33,19 @@ namespace Volo.Abp.Identity
int skipCount = 0,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task<List<OrganizationUnit>> GetListAsync(
IEnumerable<Guid> ids,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task<List<OrganizationUnit>> GetListAsync(
IEnumerable<Guid> ids,
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task<List<IdentityRole>> GetRolesAsync(
OrganizationUnit organizationUnit,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
bool includeDetails = false,
CancellationToken cancellationToken = default
);

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

@ -55,10 +55,10 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
.OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName))
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<OrganizationUnit>> GetListAsync(
}
public virtual async Task<List<OrganizationUnit>> GetListAsync(
IEnumerable<Guid> ids,
bool includeDetails = false,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await DbSet
@ -78,10 +78,13 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
ou => ou.DisplayName == displayName,
GetCancellationToken(cancellationToken)
);
}
}
public virtual async Task<List<IdentityRole>> GetRolesAsync(
OrganizationUnit organizationUnit,
OrganizationUnit organizationUnit,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
@ -89,6 +92,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
join role in DbContext.Roles.IncludeDetails(includeDetails) on organizationRole.RoleId equals role.Id
where organizationRole.OrganizationUnitId == organizationUnit.Id
select role;
query = query
.OrderBy(sorting ?? nameof(IdentityRole.Name))
.PageBy(skipCount, maxResultCount);
return await query.ToListAsync(GetCancellationToken(cancellationToken));
}
@ -108,35 +114,35 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
where userOu.OrganizationUnitId == organizationUnit.Id
select user;
if (!filter.IsNullOrWhiteSpace())
{
query = query.Where(u =>
u.UserName.Contains(filter) ||
u.Email.Contains(filter) ||
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
);
if (!filter.IsNullOrWhiteSpace())
{
query = query.Where(u =>
u.UserName.Contains(filter) ||
u.Email.Contains(filter) ||
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
);
}
return await query.OrderBy(sorting ?? nameof(IdentityUser.UserName))
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<int> GetMembersCountAsync(
OrganizationUnit organizationUnit,
CancellationToken cancellationToken = default)
{
}
public virtual async Task<int> GetMembersCountAsync(
OrganizationUnit organizationUnit,
CancellationToken cancellationToken = default)
{
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
join user in DbContext.Users on userOu.UserId equals user.Id
where userOu.OrganizationUnitId == organizationUnit.Id
select user;
return await query.CountAsync(GetCancellationToken(cancellationToken));
select user;
return await query.CountAsync(GetCancellationToken(cancellationToken));
}
public override IQueryable<OrganizationUnit> WithDetails()
{
return GetQueryable().IncludeDetails();
}
}
}
}

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

@ -38,11 +38,11 @@ namespace Volo.Abp.Identity.MongoDB
return await GetMongoQueryable()
.Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<OrganizationUnit>> GetListAsync(
}
public virtual async Task<List<OrganizationUnit>> GetListAsync(
IEnumerable<Guid> ids,
bool includeDetails = false,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
@ -78,19 +78,26 @@ namespace Volo.Abp.Identity.MongoDB
public virtual async Task<List<IdentityRole>> GetRolesAsync(
OrganizationUnit organizationUnit,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(cancellationToken);
}
public virtual async Task<List<IdentityUser>> GetMembersAsync(
OrganizationUnit organizationUnit,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
bool includeDetails = false,
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))
.OrderBy(sorting ?? nameof(IdentityRole.Name))
.As<IMongoQueryable<IdentityRole>>()
.PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount)
.ToListAsync(cancellationToken);
}
public virtual async Task<List<IdentityUser>> GetMembersAsync(
OrganizationUnit organizationUnit,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await DbContext.Users.AsQueryable()
@ -105,14 +112,14 @@ namespace Volo.Abp.Identity.MongoDB
.As<IMongoQueryable<IdentityUser>>()
.PageBy<IdentityUser, IMongoQueryable<IdentityUser>>(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<int> GetMembersCountAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default)
{
}
public virtual async Task<int> GetMembersCountAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default)
{
return await DbContext.Users.AsQueryable()
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.As<IMongoQueryable<IdentityUser>>()
.CountAsync(GetCancellationToken(cancellationToken));
}
.As<IMongoQueryable<IdentityUser>>()
.CountAsync(GetCancellationToken(cancellationToken));
}
}
}

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

@ -1,13 +1,13 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Guids;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.Identity
@ -18,7 +18,7 @@ namespace Volo.Abp.Identity
private readonly IOrganizationUnitRepository _organizationUnitRepository;
private readonly ILookupNormalizer _lookupNormalizer;
private readonly IdentityTestData _testData;
private readonly IGuidGenerator _guidGenerator;
private readonly IGuidGenerator _guidGenerator;
private readonly OrganizationUnitManager _organizationUnitManager;
private readonly IIdentityRoleRepository _identityRoleRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
@ -32,8 +32,8 @@ namespace Volo.Abp.Identity
_guidGenerator = GetRequiredService<IGuidGenerator>();
_organizationUnitManager = GetRequiredService<OrganizationUnitManager>();
_identityRoleRepository = GetRequiredService<IIdentityRoleRepository>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_identityUserRepository = GetRequiredService<IIdentityUserRepository>();
}
[Fact]
@ -78,36 +78,46 @@ namespace Volo.Abp.Identity
.FirstOrDefault(ou => ou.DisplayName == "OU111");
ou.Roles.ShouldNotBeNull();
ou.Roles.Any().ShouldBeTrue();
}
}
[Fact]
public async Task GetOrganizationUnitRolesAsync()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", includeDetails: true);
var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, true);
var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, includeDetails: true);
ou111Roles.Count.ShouldBe(2);
ou111Roles.ShouldContain(n => n.Name == "manager");
ou111Roles.ShouldContain(n => n.Name == "moderator");
}
[Fact]
public async Task GetMembersInOrganizationUnitListAsync()
{
OrganizationUnit ou1 = await _organizationUnitRepository.GetAsync("OU111", true);
OrganizationUnit ou2 = await _organizationUnitRepository.GetAsync("OU112", true);
var users = await _identityUserRepository.GetUsersInOrganizationsListAsync(new List<Guid> { ou1.Id, ou2.Id });
users.Count.ShouldBeGreaterThan(0);
public async Task GetOrganizationUnitRolesWithPagingAsync()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", includeDetails: true);
var ou111Roles = await _organizationUnitRepository.GetRolesAsync(ou, sorting: "name desc", maxResultCount: 1, includeDetails: true);
ou111Roles.Count.ShouldBe(1);
ou111Roles.ShouldContain(n => n.Name == "moderator");
}
[Fact]
public async Task GetMembersInOrganizationUnitListAsync()
{
OrganizationUnit ou1 = await _organizationUnitRepository.GetAsync("OU111", true);
OrganizationUnit ou2 = await _organizationUnitRepository.GetAsync("OU112", true);
var users = await _identityUserRepository.GetUsersInOrganizationsListAsync(new List<Guid> { ou1.Id, ou2.Id });
users.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task GetMembersInOrganizationUnitWithParamsAsync()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
var users = await _organizationUnitRepository.GetMembersAsync(ou, "UserName DESC", 5, 0, "n");
public async Task GetMembersInOrganizationUnitWithParamsAsync()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
var users = await _organizationUnitRepository.GetMembersAsync(ou, "UserName DESC", 5, 0, "n");
users.Count.ShouldBeGreaterThan(1);
users.Count.ShouldBeLessThanOrEqualTo(5);
//Filter check
users.Count.ShouldBeLessThanOrEqualTo(5);
//Filter check
users.ShouldAllBe(u => u.UserName.Contains("ne") || u.Email.Contains("n"));
//Order check
@ -121,16 +131,16 @@ namespace Volo.Abp.Identity
}
users = await _organizationUnitRepository.GetMembersAsync(ou, null, 999, 0, "undefined-username");
users.Count.ShouldBe(0);
users.Count.ShouldBe(0);
}
[Fact]
public async Task GetCountMembersCountInOrganizationUnit()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
var users = await _organizationUnitRepository.GetMembersCountAsync(ou);
users.ShouldBeGreaterThan(1);
public async Task GetCountMembersCountInOrganizationUnit()
{
OrganizationUnit ou = await _organizationUnitRepository.GetAsync("OU111", true);
var users = await _organizationUnitRepository.GetMembersCountAsync(ou);
users.ShouldBeGreaterThan(1);
}
}
}

Loading…
Cancel
Save