Browse Source

Apply data filters while querying from roles.

pull/10614/head
Halil İbrahim Kalkan 5 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) protected virtual TQueryable ApplyDataFilters<TQueryable>(TQueryable query)
where TQueryable : IQueryable<TEntity> 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); 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; var tenantId = CurrentTenant.Id;
query = (TQueryable)query.WhereIf(DataFilter.IsEnabled<IMultiTenant>(), e => ((IMultiTenant)e).TenantId == tenantId); 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); cancellationToken = GetCancellationToken(cancellationToken);
var dbContext = await GetDbContextAsync(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 dbContext.SessionHandle != null
? collection.AsQueryable(dbContext.SessionHandle) ? collection.AsQueryable(dbContext.SessionHandle)
: collection.AsQueryable() : 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(); var roleIds = organizationUnits.SelectMany(x => x.Roles.Select(r => r.RoleId)).ToArray();
return await dbContext.Roles //TODO: Such usage suppress filters! var queryable = await GetMongoQueryableAsync<IdentityRole>(cancellationToken);
.AsQueryable()
return await queryable
.Where(r => roleIds.Contains(r.Id)) .Where(r => roleIds.Contains(r.Id))
.Select(r => r.Name) .Select(r => r.Name)
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
@ -117,7 +118,9 @@ namespace Volo.Abp.Identity.MongoDB
{ {
cancellationToken = GetCancellationToken(cancellationToken); 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) .Where(x => x.NormalizedName == normalizedRoleName)
.OrderBy(x => x.Id) .OrderBy(x => x.Id)
.FirstOrDefaultAsync(cancellationToken); .FirstOrDefaultAsync(cancellationToken);
@ -244,8 +247,7 @@ namespace Volo.Abp.Identity.MongoDB
{ {
var result = await (await GetMongoQueryableAsync(cancellationToken)) var result = await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnitId)) .Where(u => u.OrganizationUnits.Any(uou => uou.OrganizationUnitId == organizationUnitId))
.ToListAsync(GetCancellationToken(cancellationToken)) .ToListAsync(GetCancellationToken(cancellationToken));
;
return result; return result;
} }
@ -255,8 +257,7 @@ namespace Volo.Abp.Identity.MongoDB
{ {
var result = await (await GetMongoQueryableAsync(cancellationToken)) var result = await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId))) .Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))
.ToListAsync(GetCancellationToken(cancellationToken)) .ToListAsync(GetCancellationToken(cancellationToken));
;
return result; return result;
} }
@ -269,8 +270,7 @@ namespace Volo.Abp.Identity.MongoDB
var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable() var organizationUnitIds = await (await GetDbContextAsync(cancellationToken)).OrganizationUnits.AsQueryable()
.Where(ou => ou.Code.StartsWith(code)) .Where(ou => ou.Code.StartsWith(code))
.Select(ou => ou.Id) .Select(ou => ou.Id)
.ToListAsync(cancellationToken) .ToListAsync(cancellationToken);
;
return await (await GetMongoQueryableAsync(cancellationToken)) return await (await GetMongoQueryableAsync(cancellationToken))
.Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId))) .Where(u => u.OrganizationUnits.Any(uou => organizationUnitIds.Contains(uou.OrganizationUnitId)))

Loading…
Cancel
Save