mongodb repo for organization units

pull/2563/head
Mehmet Perk 6 years ago
parent bdffd6d29a
commit 4e7e04a7fe

@ -22,6 +22,10 @@ namespace Volo.Abp.Identity
CancellationToken cancellationToken = default
);
Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
Guid id,
CancellationToken cancellationToken = default);
Task<IdentityUser> FindByLoginAsync(
[NotNull] string loginProvider,
[NotNull] string providerKey,

@ -366,7 +366,11 @@ namespace Volo.Abp.Identity
Check.NotNull(user, nameof(user));
return await _userRepository.GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken);
var userRoles = await _userRepository.GetRoleNamesAsync(user.Id, cancellationToken: cancellationToken);
var userOrganizationUnitRoles = await _userRepository.GetRoleNamesInOrganizationUnitAsync(user.Id, cancellationToken: cancellationToken);
return userRoles.Union(userOrganizationUnitRoles).ToList();
}
/// <summary>

@ -8,18 +8,18 @@ namespace Volo.Abp.Identity.Organizations
{
public interface IOrganizationUnitRepository : IBasicRepository<OrganizationUnit, Guid>
{
Task<List<OrganizationUnit>> GetChildrenAsync(Guid? parentId);
Task<List<OrganizationUnit>> GetChildrenAsync(Guid? parentId, CancellationToken cancellationToken = default);
Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId);
Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId, CancellationToken cancellationToken = default);
Task<List<OrganizationUnit>> GetListAsync(IEnumerable<Guid> ids);
Task<List<OrganizationUnit>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default);
Task<List<OrganizationUnit>> GetListAsync(bool includeDetails = true);
Task<List<OrganizationUnit>> GetListAsync(bool includeDetails = true, CancellationToken cancellationToken = default);
Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default);
Task AddRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId);
Task AddRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default);
Task RemoveRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId);
Task RemoveRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default);
}
}

@ -151,8 +151,8 @@ namespace Volo.Abp.Identity.Organizations
public virtual async Task AddRoleToOrganizationUnitAsync(Guid roleId, Guid ouId)
{
await AddRoleToOrganizationUnitAsync(
await _identityRoleRepository.GetAsync(roleId),
await _organizationUnitRepository.GetAsync(ouId)
await _identityRoleRepository.GetAsync(roleId).ConfigureAwait(false),
await _organizationUnitRepository.GetAsync(ouId, true).ConfigureAwait(false)
);
}
@ -162,7 +162,7 @@ namespace Volo.Abp.Identity.Organizations
if (currentRoles.Any(r => r.Id == role.Id))
{
return;
return ;
}
ou.AddRole(role.Id);
await _organizationUnitRepository.AddRole(ou, role, ou.TenantId);

@ -44,6 +44,19 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
return await query.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
Guid id,
CancellationToken cancellationToken = default)
{
var query = from userOu in DbContext.Set<IdentityUserOrganizationUnit>()
join roleOu in DbContext.Set<OrganizationUnitRole>() on userOu.OrganizationUnitId equals roleOu.OrganizationUnitId
join userOuRoles in DbContext.Roles on roleOu.RoleId equals userOuRoles.Id
where userOu.UserId == id
select userOuRoles.Name;
return await query.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<IdentityUser> FindByLoginAsync(
string loginProvider,
string providerKey,

@ -18,28 +18,28 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
{
}
public async Task<List<OrganizationUnit>> GetChildrenAsync(Guid? parentId)
public async Task<List<OrganizationUnit>> GetChildrenAsync(Guid? parentId, CancellationToken cancellationToken = default)
{
return await DbSet.Where(x => x.ParentId == parentId)
.ToListAsync();
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId)
public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId, CancellationToken cancellationToken = default)
{
return await DbSet.Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value)
.ToListAsync();
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetListAsync(IEnumerable<Guid> ids)
public async Task<List<OrganizationUnit>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
return await DbSet.Where(t => ids.Contains(t.Id)).ToListAsync();
return await DbSet.Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetListAsync(bool includeDetails = true)
public override async Task<List<OrganizationUnit>> GetListAsync(bool includeDetails = true, CancellationToken cancellationToken = default)
{
return await DbSet
.IncludeDetails(includeDetails)
.ToListAsync();
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
@ -52,14 +52,14 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
).ConfigureAwait(false);
}
public async Task AddRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId)
public Task AddRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default)
{
var our = new OrganizationUnitRole(tenantId, role.Id, ou.Id);
DbContext.Set<OrganizationUnitRole>().Add(our);
await DbContext.SaveChangesAsync();
return Task.FromResult(0);
}
public async Task RemoveRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId)
public async Task RemoveRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default)
{
var context = DbContext.Set<OrganizationUnitRole>();
var our = await context.FirstOrDefaultAsync(our =>
@ -68,12 +68,12 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
our.TenantId == tenantId
);
DbContext.Set<OrganizationUnitRole>().Remove(our);
await DbContext.SaveChangesAsync();
}
public override IQueryable<OrganizationUnit> WithDetails()
{
return GetQueryable().IncludeDetails();
}
}
}

@ -1,5 +1,6 @@
using MongoDB.Driver;
using Volo.Abp.Data;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Identity.MongoDB
@ -13,6 +14,8 @@ namespace Volo.Abp.Identity.MongoDB
public IMongoCollection<IdentityClaimType> ClaimTypes => Collection<IdentityClaimType>();
public IMongoCollection<OrganizationUnit> OrganizationUnits => Collection<OrganizationUnit>();
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);

@ -1,4 +1,5 @@
using System;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Identity.MongoDB
@ -31,6 +32,11 @@ namespace Volo.Abp.Identity.MongoDB
{
b.CollectionName = options.CollectionPrefix + "ClaimTypes";
});
builder.Entity<OrganizationUnit>(b =>
{
b.CollectionName = options.CollectionPrefix + "OrganizationUnit";
});
}
}
}

@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.Modularity;
using Volo.Abp.Users.MongoDB;
@ -17,6 +18,7 @@ namespace Volo.Abp.Identity.MongoDB
options.AddRepository<IdentityUser, MongoIdentityUserRepository>();
options.AddRepository<IdentityRole, MongoIdentityRoleRepository>();
options.AddRepository<IdentityClaimType, MongoIdentityRoleRepository>();
options.AddRepository<OrganizationUnit, MongoIdentityRoleRepository>();
});
}
}

@ -1,5 +1,6 @@
using MongoDB.Driver;
using Volo.Abp.Data;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Identity.MongoDB
@ -12,5 +13,7 @@ namespace Volo.Abp.Identity.MongoDB
IMongoCollection<IdentityRole> Roles { get; }
IMongoCollection<IdentityClaimType> ClaimTypes { get; }
IMongoCollection<OrganizationUnit> OrganizationUnits { get; }
}
}

@ -9,6 +9,7 @@ using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.Guids;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Identity.MongoDB
@ -17,14 +18,14 @@ namespace Volo.Abp.Identity.MongoDB
{
private readonly IGuidGenerator _guidGenerator;
public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider, IGuidGenerator guidGenerator)
public MongoIdentityUserRepository(IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider, IGuidGenerator guidGenerator)
: base(dbContextProvider)
{
_guidGenerator = guidGenerator;
}
public async Task<IdentityUser> FindByNormalizedUserNameAsync(
string normalizedUserName,
string normalizedUserName,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
@ -36,7 +37,7 @@ namespace Volo.Abp.Identity.MongoDB
}
public async Task<List<string>> GetRoleNamesAsync(
Guid id,
Guid id,
CancellationToken cancellationToken = default)
{
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)).ConfigureAwait(false);
@ -44,9 +45,20 @@ namespace Volo.Abp.Identity.MongoDB
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
Guid id,
CancellationToken cancellationToken = default)
{
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)).ConfigureAwait(false);
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);
var organizationUnits = DbContext.OrganizationUnits.AsQueryable().Where(ou => organizationUnitIds.Contains(ou.Id));
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.Id));
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<IdentityUser> FindByLoginAsync(
string loginProvider,
string providerKey,
string loginProvider,
string providerKey,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
@ -74,7 +86,7 @@ namespace Volo.Abp.Identity.MongoDB
}
public async Task<List<IdentityUser>> GetListByNormalizedRoleNameAsync(
string normalizedRoleName,
string normalizedRoleName,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
@ -92,9 +104,9 @@ namespace Volo.Abp.Identity.MongoDB
public async Task<List<IdentityUser>> GetListAsync(
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
@ -121,6 +133,16 @@ namespace Volo.Abp.Identity.MongoDB
return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(
Guid id,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
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);
}
public async Task<long> GetCountAsync(
string filter = null,
CancellationToken cancellationToken = default)

@ -0,0 +1,66 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.Guids;
using Volo.Abp.Identity.Organizations;
using Volo.Abp.MongoDB;
namespace Volo.Abp.Identity.MongoDB
{
public class MongoOrganizationUnitRepository : MongoDbRepository<IAbpIdentityMongoDbContext, OrganizationUnit, Guid>, IOrganizationUnitRepository
{
private readonly IGuidGenerator _guidGenerator;
public MongoOrganizationUnitRepository(
IMongoDbContextProvider<IAbpIdentityMongoDbContext> dbContextProvider,
IGuidGenerator guidGenerator)
: base(dbContextProvider)
{
_guidGenerator = guidGenerator;
}
public async Task<List<OrganizationUnit>> GetChildrenAsync(Guid? parentId, CancellationToken cancellationToken = default)
{
return await DbContext.OrganizationUnits.AsQueryable().Where(ou => ou.ParentId == parentId)
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetAllChildrenWithParentCodeAsync(string code, Guid? parentId, CancellationToken cancellationToken = default)
{
return await DbContext.OrganizationUnits.AsQueryable()
.Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value)
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<List<OrganizationUnit>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
return await DbContext.OrganizationUnits.AsQueryable()
.Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public async Task<OrganizationUnit> GetOrganizationUnit(string displayName, bool includeDetails = false, CancellationToken cancellationToken = default)
{
return await DbContext.OrganizationUnits.AsQueryable()
.FirstOrDefaultAsync(
ou => ou.DisplayName == displayName,
GetCancellationToken(cancellationToken)
).ConfigureAwait(false);
}
public Task AddRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task RemoveRole(OrganizationUnit ou, IdentityRole role, Guid? tenantId, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}

@ -89,9 +89,9 @@ namespace Volo.Abp.Identity
{
var ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", true).ConfigureAwait(false);
var adminRole = await _identityRoleRepository.FindByNormalizedNameAsync(_lookupNormalizer.NormalizeName("admin")).ConfigureAwait(false);
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole.Id, ou.Id);
await _organizationUnitManager.AddRoleToOrganizationUnitAsync(adminRole, ou);
//TODO: This method has a bug: includeDetails not work
//TODO: This method has a bug: includeDetails not work or add role not work
ou = await _organizationUnitRepository.GetOrganizationUnit("OU1", includeDetails: true).ConfigureAwait(false);
ou.Roles.FirstOrDefault().Id.ShouldBe(adminRole.Id);
}

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Volo.Abp.Identity.MongoDB
{
public class OrganizationUnitRepository_Tests : OrganizationUnitRepository_Tests<AbpIdentityMongoDbTestModule>
{
}
}
Loading…
Cancel
Save