125 lines
4.2 KiB
C#
125 lines
4.2 KiB
C#
using PostFundManagement.Domain.Abstractions;
|
|
using PostFundManagement.Domain.Entities;
|
|
|
|
namespace PostFundManagement.Infrastructure.Database;
|
|
|
|
public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
|
|
ITenantContextService tenantService) : DbContext(options)
|
|
{
|
|
private bool BypassFilter => tenantService.IsAdmin;
|
|
private long CurrentTenantId => tenantService.TenantId;
|
|
|
|
public DbSet<TenantUser> TenantUsers => Set<TenantUser>();
|
|
|
|
public DbSet<TenantOrganisation> TenantOrganisations => Set<TenantOrganisation>();
|
|
|
|
public DbSet<Tenant> Tenants => Set<Tenant>();
|
|
|
|
public DbSet<Notification> Notifications => Set<Notification>();
|
|
|
|
public DbSet<Invite> Invites => Set<Invite>();
|
|
|
|
public DbSet<Activity> Activities => Set<Activity>();
|
|
|
|
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
|
|
|
|
public DbSet<SiteVisit> SiteVisits => Set<SiteVisit>();
|
|
|
|
public DbSet<ChangeRequest> ChangeRequests => Set<ChangeRequest>();
|
|
|
|
public DbSet<ComplianceItem> ComplianceItems => Set<ComplianceItem>();
|
|
|
|
public DbSet<Issue> Issues => Set<Issue>();
|
|
|
|
public DbSet<Outcome> Outcomes => Set<Outcome>();
|
|
|
|
public DbSet<Beneficiary> Beneficiaries => Set<Beneficiary>();
|
|
|
|
public DbSet<Evidence> Evidences => Set<Evidence>();
|
|
|
|
public DbSet<Risk> Risks => Set<Risk>();
|
|
|
|
public DbSet<Indicator> Indicators => Set<Indicator>();
|
|
|
|
public DbSet<Disbursement> Disbursements => Set<Disbursement>();
|
|
|
|
public DbSet<Milestone> Milestones => Set<Milestone>();
|
|
|
|
public DbSet<Budget> Budgets => Set<Budget>();
|
|
|
|
public DbSet<Contract> Contracts => Set<Contract>();
|
|
|
|
public DbSet<Award> Awards => Set<Award>();
|
|
|
|
public DbSet<Project> Projects => Set<Project>();
|
|
|
|
public DbSet<Organisation> Organisations => Set<Organisation>();
|
|
|
|
public DbSet<Rule> Rules => Set<Rule>();
|
|
|
|
public DbSet<Programme> Programmes => Set<Programme>();
|
|
|
|
public DbSet<Portfolio> Portfolios => Set<Portfolio>();
|
|
|
|
public DbSet<Instrument> Instruments => Set<Instrument>();
|
|
|
|
public DbSet<User> Users => Set<User>();
|
|
|
|
private void ApplyGlobalTenantFilter<TEntity>(ModelBuilder modelBuilder)
|
|
where TEntity : class, ITenantEntity =>
|
|
modelBuilder.Entity<TEntity>().HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
if (typeof(ITenantEntity).IsAssignableFrom(entityType.ClrType))
|
|
{
|
|
var method = typeof(ApplicationDbContext).GetMethod(nameof(ApplyGlobalTenantFilter),
|
|
BindingFlags.NonPublic | BindingFlags.Instance)?.MakeGenericMethod(entityType.ClrType);
|
|
|
|
method?.Invoke(this, [modelBuilder]);
|
|
}
|
|
}
|
|
|
|
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter ||
|
|
Set<TenantOrganisation>().Any(to => to.TenantId == CurrentTenantId && to.OrganisationId == o.Id));
|
|
|
|
modelBuilder.Entity<TenantOrganisation>()
|
|
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
|
|
|
|
modelBuilder.Entity<TenantUser>()
|
|
.HasQueryFilter(to => BypassFilter || to.TenantId == CurrentTenantId);
|
|
}
|
|
|
|
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
AlterQualifyingTenantEntity();
|
|
|
|
return base.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public override int SaveChanges()
|
|
{
|
|
AlterQualifyingTenantEntity();
|
|
|
|
return base.SaveChanges();
|
|
}
|
|
|
|
private void AlterQualifyingTenantEntity()
|
|
{
|
|
var entries = ChangeTracker.Entries<ITenantEntity>()
|
|
.Where(e => e.State == EntityState.Added && e.Entity.TenantId == 0)
|
|
.ToList();
|
|
|
|
if (entries.Count == 0) return;
|
|
|
|
if (CurrentTenantId <= 0 && !BypassFilter)
|
|
throw new InvalidOperationException("Cannot save multi-tenant entity: CurrentTenantId is not set in the active execution context.");
|
|
|
|
foreach (var entry in entries)
|
|
entry.Entity.TenantId = CurrentTenantId;
|
|
}
|
|
} |