Browse Source

Apply data filters

pull/10616/head
liangshiwei 5 years ago
parent
commit
9c0db697f6
  1. 6
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  2. 8
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs
  3. 2
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs
  4. 31
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs
  5. 54
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs

6
framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -523,14 +523,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return GetMongoQueryableAsync<TEntity>(cancellationToken); return GetMongoQueryableAsync<TEntity>(cancellationToken);
} }
protected virtual async Task<IMongoQueryable<TEnt>> GetMongoQueryableAsync<TEnt>(CancellationToken cancellationToken = default) protected virtual async Task<IMongoQueryable<TOtherEntity>> GetMongoQueryableAsync<TOtherEntity>(CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var dbContext = await GetDbContextAsync(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEnt>(); var collection = dbContext.Collection<TOtherEntity>();
return ApplyDataFilters<IMongoQueryable<TEnt>, TEnt>( return ApplyDataFilters<IMongoQueryable<TOtherEntity>, TOtherEntity>(
dbContext.SessionHandle != null dbContext.SessionHandle != null
? collection.AsQueryable(dbContext.SessionHandle) ? collection.AsQueryable(dbContext.SessionHandle)
: collection.AsQueryable() : collection.AsQueryable()

8
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs

@ -82,7 +82,7 @@ namespace Volo.CmsKit.MongoDB.Comments
orderby comment.CreationTime orderby comment.CreationTime
select user; select user;
var authors = await authorsQuery.ToListAsync(token); var authors = await ApplyDataFilters<IMongoQueryable<CmsUser>, CmsUser>(authorsQuery).ToListAsync(token);
return comments return comments
.Select( .Select(
@ -131,7 +131,7 @@ namespace Volo.CmsKit.MongoDB.Comments
orderby comment.CreationTime orderby comment.CreationTime
select user; select user;
var authors = await authorsQuery.ToListAsync(GetCancellationToken(cancellationToken)); var authors = await ApplyDataFilters<IMongoQueryable<CmsUser>, CmsUser>(authorsQuery).ToListAsync(GetCancellationToken(cancellationToken));
var comments = await (await GetMongoQueryableAsync(cancellationToken)) var comments = await (await GetMongoQueryableAsync(cancellationToken))
.Where(c => c.EntityId == entityId && c.EntityType == entityType) .Where(c => c.EntityId == entityId && c.EntityType == entityType)
@ -184,9 +184,7 @@ namespace Volo.CmsKit.MongoDB.Comments
if (!string.IsNullOrEmpty(authorUsername)) if (!string.IsNullOrEmpty(authorUsername))
{ {
var authorQueryable = (await GetDbContextAsync(cancellationToken)).Collection<CmsUser>().AsQueryable(); var author = await (await GetMongoQueryableAsync<CmsUser>(cancellationToken)).FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken);
var author = await authorQueryable.FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken);
var authorId = author?.Id ?? Guid.Empty; var authorId = author?.Id ?? Guid.Empty;

2
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs

@ -67,7 +67,7 @@ namespace Volo.CmsKit.MongoDB.Tags
Check.NotNullOrEmpty(entityType, nameof(entityType)); Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(entityId, nameof(entityId)); Check.NotNullOrEmpty(entityId, nameof(entityId));
var entityTagIds = await (await GetDbContextAsync(cancellationToken)).EntityTags.AsQueryable() var entityTagIds = await (await GetMongoQueryableAsync<EntityTag>(cancellationToken))
.Where(q => q.EntityId == entityId) .Where(q => q.EntityId == entityId)
.Select(q => q.TagId) .Select(q => q.TagId)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); .ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));

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

@ -41,16 +41,13 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId) .Select(r => r.OrganizationUnitId)
.ToArray(); .ToArray();
var dbContext = await GetDbContextAsync(cancellationToken); var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id)) .Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray(); .ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray(); var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
var allRoleIds = orgUnitRoleIds.Union(roleIds); var allRoleIds = orgUnitRoleIds.Union(roleIds);
return await dbContext.Roles.AsQueryable().Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken)); return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).Select(r => r.Name).ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync( public virtual async Task<List<string>> GetRoleNamesInOrganizationUnitAsync(
@ -63,12 +60,9 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId) .Select(r => r.OrganizationUnitId)
.ToArray(); .ToArray();
var dbContext = await GetDbContextAsync(cancellationToken); var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id)) .Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray(); .ToListAsync(cancellationToken: cancellationToken);
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
@ -183,16 +177,13 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId) .Select(r => r.OrganizationUnitId)
.ToArray(); .ToArray();
var dbContext = await GetDbContextAsync(cancellationToken); var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id)) .Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray(); .ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray(); var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
var allRoleIds = orgUnitRoleIds.Union(roleIds); var allRoleIds = orgUnitRoleIds.Union(roleIds);
return await dbContext.Roles.AsQueryable().Where(r => allRoleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken)); return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => allRoleIds.Contains(r.Id)).ToListAsync(GetCancellationToken(cancellationToken));
} }
public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync( public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(
@ -203,9 +194,7 @@ namespace Volo.Abp.Identity.MongoDB
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken)); var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken));
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId); var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);
var dbContext = await GetDbContextAsync(cancellationToken); return await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
return await dbContext.OrganizationUnits.AsQueryable()
.Where(ou => organizationUnitIds.Contains(ou.Id)) .Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
} }
@ -267,7 +256,7 @@ namespace Volo.Abp.Identity.MongoDB
{ {
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable() var organizationUnitIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Code.StartsWith(code)) .Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id) .Select(ou => ou.Id)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);

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

@ -89,10 +89,9 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>( return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken))
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)) .Where(r => roleIds.Contains(r.Id))
)
.OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting) .OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting)
.As<IMongoQueryable<IdentityRole>>() .As<IMongoQueryable<IdentityRole>>()
.PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount) .PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount)
@ -104,12 +103,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>( return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => roleIds.Contains(r.Id)).CountAsync(cancellationToken);
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))
)
.As<IMongoQueryable<IdentityRole>>()
.CountAsync(cancellationToken);
} }
public virtual async Task<List<IdentityRole>> GetUnaddedRolesAsync( public virtual async Task<List<IdentityRole>> GetUnaddedRolesAsync(
@ -122,8 +117,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(dbContext.Roles.AsQueryable()) return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken))
.Where(r => !roleIds.Contains(r.Id)) .Where(r => !roleIds.Contains(r.Id))
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) .WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter))
.OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting) .OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting)
@ -138,8 +133,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(dbContext.Roles.AsQueryable()) return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken))
.Where(r => !roleIds.Contains(r.Id)) .Where(r => !roleIds.Contains(r.Id))
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter)) .WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter))
.As<IMongoQueryable<IdentityRole>>() .As<IMongoQueryable<IdentityRole>>()
@ -183,8 +178,8 @@ namespace Volo.Abp.Identity.MongoDB
bool includeDetails = false, bool includeDetails = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(cancellationToken); return await
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( .WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(), !filter.IsNullOrWhiteSpace(),
@ -202,8 +197,7 @@ namespace Volo.Abp.Identity.MongoDB
public virtual async Task<int> GetUnaddedUsersCountAsync(OrganizationUnit organizationUnit, string filter = null, public virtual async Task<int> GetUnaddedUsersCountAsync(OrganizationUnit organizationUnit, string filter = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(cancellationToken); return await (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable())
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( .WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(), !filter.IsNullOrWhiteSpace(),
@ -224,8 +218,9 @@ namespace Volo.Abp.Identity.MongoDB
public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default) public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default)
{ {
var userQueryable = await GetMongoQueryableAsync<IdentityUser>(cancellationToken);
var dbContext = await GetDbContextAsync(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken);
var users = await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable()) var users = await userQueryable
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.As<IMongoQueryable<IdentityUser>>() .As<IMongoQueryable<IdentityUser>>()
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
@ -242,8 +237,7 @@ namespace Volo.Abp.Identity.MongoDB
string filter = null, string filter = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(cancellationToken); return (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
return ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable())
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id)) .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>( .WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(), !filter.IsNullOrWhiteSpace(),
@ -253,23 +247,5 @@ namespace Volo.Abp.Identity.MongoDB
(u.PhoneNumber != null && u.PhoneNumber.Contains(filter)) (u.PhoneNumber != null && u.PhoneNumber.Contains(filter))
); );
} }
//TODO: Should improve!
private TQueryable ApplyDataFilters<TQueryable,TEntity>(TQueryable query)
where TQueryable : IQueryable<TEntity>
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{
query = (TQueryable)query.WhereIf(DataFilter.IsEnabled<ISoftDelete>(), e => ((ISoftDelete)e).IsDeleted == false);
}
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity)))
{
var tenantId = CurrentTenant.Id;
query = (TQueryable)query.WhereIf(DataFilter.IsEnabled<IMultiTenant>(), e => ((IMultiTenant)e).TenantId == tenantId);
}
return query;
}
} }
} }

Loading…
Cancel
Save