Browse Source

Apply data filters

pull/10616/head
liangshiwei 5 years ago
parent
commit
9c0db697f6
  1. 8
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  2. 58
      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. 35
      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

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

@ -522,15 +522,15 @@ 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()

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

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

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));

35
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,16 +60,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 roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray(); 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 return await queryable
.Where(r => roleIds.Contains(r.Id)) .Where(r => roleIds.Contains(r.Id))
@ -119,7 +113,7 @@ namespace Volo.Abp.Identity.MongoDB
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken); var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var role = await queryable var role = await queryable
.Where(x => x.NormalizedName == normalizedRoleName) .Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id) .OrderBy(x => x.Id)
@ -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