Browse Source

Apply data filters while querying from roles.

pull/10614/head
Halil İbrahim Kalkan 4 years ago
parent
commit
ff83ff4ff9
  1. 10
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs
  2. 11
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  3. 18
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityUserRepository.cs

10
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs

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

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

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

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

@ -72,8 +72,9 @@ namespace Volo.Abp.Identity.MongoDB
var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
return await dbContext.Roles //TODO: Such usage suppress filters!
.AsQueryable()
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
return await queryable
.Where(r => roleIds.Contains(r.Id))
.Select(r => r.Name)
.ToListAsync(GetCancellationToken(cancellationToken));
@ -117,7 +118,9 @@ namespace Volo.Abp.Identity.MongoDB
{
cancellationToken = GetCancellationToken(cancellationToken);
var role = await (await GetDbContextAsync(cancellationToken)).Roles.AsQueryable() //TODO: Such usages breaks data filters
var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
var role = await queryable
.Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(cancellationToken);
@ -244,8 +247,7 @@ namespace Volo.Abp.Identity.MongoDB
{
var result = await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnitId))
.ToListAsync(GetCancellationToken(cancellationToken))
;
.ToListAsync(GetCancellationToken(cancellationToken));
return result;
}
@ -255,8 +257,7 @@ namespace Volo.Abp.Identity.MongoDB
{
var result = await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))
.ToListAsync(GetCancellationToken(cancellationToken))
;
.ToListAsync(GetCancellationToken(cancellationToken));
return result;
}
@ -269,8 +270,7 @@ namespace Volo.Abp.Identity.MongoDB
var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable()
.Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id)
.ToListAsync(cancellationToken)
;
.ToListAsync(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))

Loading…
Cancel
Save