Hardened API and catered for Partner role query filter bypass

This commit is contained in:
2026-10-04 08:18:43 +02:00
parent 8d069c2da0
commit 8100518549
62 changed files with 232 additions and 193 deletions
@@ -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)