Hardened API and catered for Partner role query filter bypass
This commit is contained in:
@@ -6,7 +6,7 @@ namespace PostFundManagement.Infrastructure.Database;
|
||||
public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
|
||||
ITenantContextService tenantService) : DbContext(options)
|
||||
{
|
||||
private bool BypassFilter => tenantService.IsAdmin;
|
||||
private bool BypassFilter => tenantService.IsAdmin || tenantService.ByPassTenantQueryFilter;
|
||||
private long CurrentTenantId => tenantService.TenantId;
|
||||
|
||||
public DbSet<TenantUser> TenantUsers => Set<TenantUser>();
|
||||
@@ -67,7 +67,7 @@ public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext>
|
||||
|
||||
private void ApplyGlobalTenantFilter<TEntity>(ModelBuilder modelBuilder)
|
||||
where TEntity : class, ITenantEntity =>
|
||||
modelBuilder.Entity<TEntity>().HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
modelBuilder.Entity<TEntity>().HasQueryFilter(e => BypassFilter == false && e.TenantId == CurrentTenantId);
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -84,14 +84,14 @@ public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext>
|
||||
}
|
||||
}
|
||||
|
||||
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter ||
|
||||
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter == false &&
|
||||
Set<TenantOrganisation>().Any(to => to.TenantId == CurrentTenantId && to.OrganisationId == o.Id));
|
||||
|
||||
modelBuilder.Entity<TenantOrganisation>()
|
||||
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
|
||||
.HasQueryFilter(to => BypassFilter == false && to.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<TenantUser>()
|
||||
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
|
||||
.HasQueryFilter(to => BypassFilter == false && to.TenantId == CurrentTenantId);
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -10,5 +10,6 @@ public sealed partial class ApplicationDbContextFactory
|
||||
public long TenantId => 0L;
|
||||
public bool IsAdmin => true;
|
||||
public bool HasTenantFilter => false;
|
||||
public bool ByPassTenantQueryFilter { get; set; } = true;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ public sealed class TenantContextService : ITenantContextService
|
||||
|
||||
public long TenantId { get; }
|
||||
|
||||
public bool ByPassTenantQueryFilter { get; set; } = false;
|
||||
|
||||
public bool IsAdmin { get; }
|
||||
|
||||
public bool HasTenantFilter { get; }
|
||||
|
||||
Reference in New Issue
Block a user