Merge pull request #10616 from abpframework/liangshiwei/mongo

Apply data filters
pull/9600/head^2
maliming 4 years ago committed by GitHub
commit c67760322d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -68,16 +68,16 @@ namespace Volo.Abp.Domain.Repositories
{
return ApplyDataFilters<TQueryable, TEntity>(query);
}
protected virtual TQueryable ApplyDataFilters<TQueryable, TEnt>(TQueryable query)
where TQueryable : IQueryable<TEnt>
protected virtual TQueryable ApplyDataFilters<TQueryable, TOtherEntity>(TQueryable query)
where TQueryable : IQueryable<TOtherEntity>
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEnt)))
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TOtherEntity)))
{
query = (TQueryable)query.WhereIf(DataFilter.IsEnabled<ISoftDelete>(), e => ((ISoftDelete)e).IsDeleted == false);
}
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEnt)))
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TOtherEntity)))
{
var tenantId = CurrentTenant.Id;
query = (TQueryable)query.WhereIf(DataFilter.IsEnabled<IMultiTenant>(), e => ((IMultiTenant)e).TenantId == tenantId);

@ -522,15 +522,15 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
{
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);
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
? collection.AsQueryable(dbContext.SessionHandle)
: collection.AsQueryable()

@ -28,26 +28,24 @@ namespace Volo.CmsKit.MongoDB.Blogs
Check.NotNullOrEmpty(slug, nameof(slug));
var token = GetCancellationToken(cancellationToken);
var blogPost = await GetAsync(x =>
x.BlogId == blogId &&
x.Slug.ToLower() == slug,
cancellationToken: token);
var dbContext = await GetDbContextAsync(token);
blogPost.Author = await dbContext.Collection<CmsUser>().AsQueryable().FirstOrDefaultAsync(x => x.Id == blogPost.AuthorId, token);
blogPost.Author = await (await GetMongoQueryableAsync<CmsUser>(token)).FirstOrDefaultAsync(x => x.Id == blogPost.AuthorId, token);
return blogPost;
}
public virtual async Task<int> GetCountAsync(
string filter = null,
Guid? blogId = null,
string filter = null,
Guid? blogId = null,
CancellationToken cancellationToken = default)
{
var token = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(token))
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(!string.IsNullOrWhiteSpace(filter), x => x.Title.Contains(filter) || x.Slug.Contains(filter))
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(blogId.HasValue, x => x.BlogId == blogId)
@ -65,7 +63,7 @@ namespace Volo.CmsKit.MongoDB.Blogs
var token = GetCancellationToken(cancellationToken);
var dbContext = await GetDbContextAsync(token);
var blogPostQueryable = await GetQueryableAsync();
var usersQueryable = dbContext.Collection<CmsUser>().AsQueryable();
var queryable = blogPostQueryable
@ -102,4 +100,4 @@ namespace Volo.CmsKit.MongoDB.Blogs
return await queryable.AnyAsync(x => x.BlogId == blogId && x.Slug.ToLower() == slug, token);
}
}
}
}

@ -47,26 +47,26 @@ namespace Volo.CmsKit.MongoDB.Comments
}
public virtual async Task<List<CommentWithAuthorQueryResultItem>> GetListAsync(
string filter = null,
string filter = null,
string entityType = null,
Guid? repliedCommentId = null,
string authorUsername = null,
DateTime? creationStartDate = null,
DateTime? creationEndDate = null,
DateTime? creationStartDate = null,
DateTime? creationEndDate = null,
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
int maxResultCount = int.MaxValue,
int skipCount = 0,
CancellationToken cancellationToken = default
)
{
var token = GetCancellationToken(cancellationToken);
var query = await GetListQueryAsync(
filter,
filter,
entityType,
repliedCommentId,
authorUsername,
creationStartDate,
creationEndDate,
repliedCommentId,
authorUsername,
creationStartDate,
creationEndDate,
token);
var comments = await query.OrderBy(sorting.IsNullOrEmpty() ? "creationTime desc" : sorting)
@ -75,15 +75,15 @@ namespace Volo.CmsKit.MongoDB.Comments
.ToListAsync(token);
var commentIds = comments.Select(x => x.Id).ToList();
var authorsQuery = from comment in (await GetMongoQueryableAsync(token))
join user in (await GetDbContextAsync(token)).CmsUsers on comment.CreatorId equals user.Id
where commentIds.Contains(comment.Id)
orderby comment.CreationTime
select user;
var authors = await authorsQuery.ToListAsync(token);
var authors = await ApplyDataFilters<IMongoQueryable<CmsUser>, CmsUser>(authorsQuery).ToListAsync(token);
return comments
.Select(
comment =>
@ -95,22 +95,22 @@ namespace Volo.CmsKit.MongoDB.Comments
}
public virtual async Task<long> GetCountAsync(
string text = null,
string text = null,
string entityType = null,
Guid? repliedCommentId = null,
Guid? repliedCommentId = null,
string authorUsername = null,
DateTime? creationStartDate = null,
DateTime? creationEndDate = null,
DateTime? creationEndDate = null,
CancellationToken cancellationToken = default
)
{
var query = await GetListQueryAsync(
text,
text,
entityType,
repliedCommentId,
authorUsername,
creationStartDate,
creationEndDate,
repliedCommentId,
authorUsername,
creationStartDate,
creationEndDate,
cancellationToken);
return await query.As<IMongoQueryable<Comment>>()
@ -131,7 +131,7 @@ namespace Volo.CmsKit.MongoDB.Comments
orderby comment.CreationTime
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))
.Where(c => c.EntityId == entityId && c.EntityType == entityType)
@ -169,11 +169,11 @@ namespace Volo.CmsKit.MongoDB.Comments
);
}
}
protected virtual async Task<IQueryable<Comment>> GetListQueryAsync(
string filter = null,
string filter = null,
string entityType = null,
Guid? repliedCommentId = null,
Guid? repliedCommentId = null,
string authorUsername = null,
DateTime? creationStartDate = null,
DateTime? creationEndDate = null,
@ -181,18 +181,16 @@ namespace Volo.CmsKit.MongoDB.Comments
)
{
var queryable = await GetMongoQueryableAsync(cancellationToken);
if (!string.IsNullOrEmpty(authorUsername))
{
var authorQueryable = (await GetDbContextAsync(cancellationToken)).Collection<CmsUser>().AsQueryable();
var author = await authorQueryable.FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken);
var author = await (await GetMongoQueryableAsync<CmsUser>(cancellationToken)).FirstOrDefaultAsync(x => x.UserName == authorUsername, cancellationToken: cancellationToken);
var authorId = author?.Id ?? Guid.Empty;
queryable = queryable.Where(x => x.CreatorId == authorId);
}
return queryable.WhereIf(!filter.IsNullOrWhiteSpace(), c => c.Text.Contains(filter))
.WhereIf(!entityType.IsNullOrWhiteSpace(), c => c.EntityType == entityType)
.WhereIf(repliedCommentId.HasValue, c => c.RepliedCommentId == repliedCommentId)

@ -67,7 +67,7 @@ namespace Volo.CmsKit.MongoDB.Tags
Check.NotNullOrEmpty(entityType, nameof(entityType));
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)
.Select(q => q.TagId)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));

@ -41,16 +41,13 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId)
.ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray();
.ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
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(
@ -63,16 +60,13 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId)
.ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray();
.ToListAsync(cancellationToken: cancellationToken);
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
return await queryable
.Where(r => roleIds.Contains(r.Id))
@ -119,7 +113,7 @@ namespace Volo.Abp.Identity.MongoDB
cancellationToken = GetCancellationToken(cancellationToken);
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var role = await queryable
.Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id)
@ -183,16 +177,13 @@ namespace Volo.Abp.Identity.MongoDB
.Select(r => r.OrganizationUnitId)
.ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
var organizationUnits = dbContext.OrganizationUnits
.AsQueryable()
var organizationUnits = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToArray();
.ToListAsync(cancellationToken: cancellationToken);
var orgUnitRoleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
var roleIds = user.Roles.Select(r => r.RoleId).ToArray();
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(
@ -203,9 +194,7 @@ namespace Volo.Abp.Identity.MongoDB
var user = await GetAsync(id, cancellationToken: GetCancellationToken(cancellationToken));
var organizationUnitIds = user.OrganizationUnits.Select(r => r.OrganizationUnitId);
var dbContext = await GetDbContextAsync(cancellationToken);
return await dbContext.OrganizationUnits.AsQueryable()
return await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => organizationUnitIds.Contains(ou.Id))
.ToListAsync(GetCancellationToken(cancellationToken));
}
@ -267,7 +256,7 @@ namespace Volo.Abp.Identity.MongoDB
{
cancellationToken = GetCancellationToken(cancellationToken);
var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable()
var organizationUnitIds = await (await GetMongoQueryableAsync<OrganizationUnit>(cancellationToken))
.Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id)
.ToListAsync(cancellationToken);

@ -89,10 +89,9 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default)
{
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))
)
return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken))
.Where(r => roleIds.Contains(r.Id))
.OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting)
.As<IMongoQueryable<IdentityRole>>()
.PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount)
@ -104,12 +103,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default)
{
var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray();
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityRole>, IdentityRole>(
dbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id))
)
.As<IMongoQueryable<IdentityRole>>()
.CountAsync(cancellationToken);
return await (await GetMongoQueryableAsync<IdentityRole>(cancellationToken)).Where(r => roleIds.Contains(r.Id)).CountAsync(cancellationToken);
}
public virtual async Task<List<IdentityRole>> GetUnaddedRolesAsync(
@ -122,8 +117,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default)
{
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))
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter))
.OrderBy(sorting.IsNullOrEmpty() ? nameof(IdentityRole.Name) : sorting)
@ -138,8 +133,8 @@ namespace Volo.Abp.Identity.MongoDB
CancellationToken cancellationToken = default)
{
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))
.WhereIf(!filter.IsNullOrWhiteSpace(), r => r.Name.Contains(filter))
.As<IMongoQueryable<IdentityRole>>()
@ -183,8 +178,8 @@ namespace Volo.Abp.Identity.MongoDB
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable())
return await
(await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(),
@ -202,8 +197,7 @@ namespace Volo.Abp.Identity.MongoDB
public virtual async Task<int> GetUnaddedUsersCountAsync(OrganizationUnit organizationUnit, string filter = null,
CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync(cancellationToken);
return await ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable())
return await (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
.Where(u => !u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(),
@ -224,8 +218,9 @@ namespace Volo.Abp.Identity.MongoDB
public virtual async Task RemoveAllMembersAsync(OrganizationUnit organizationUnit, CancellationToken cancellationToken = default)
{
var userQueryable = await GetMongoQueryableAsync<IdentityUser>(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))
.As<IMongoQueryable<IdentityUser>>()
.ToListAsync(GetCancellationToken(cancellationToken));
@ -242,8 +237,7 @@ namespace Volo.Abp.Identity.MongoDB
string filter = null,
CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync(cancellationToken);
return ApplyDataFilters<IMongoQueryable<IdentityUser>, IdentityUser>(dbContext.Users.AsQueryable())
return (await GetMongoQueryableAsync<IdentityUser>(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnit.Id))
.WhereIf<IdentityUser, IMongoQueryable<IdentityUser>>(
!filter.IsNullOrWhiteSpace(),
@ -253,23 +247,5 @@ namespace Volo.Abp.Identity.MongoDB
(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