Stable multitenancy
This commit is contained in:
@@ -65,35 +65,60 @@ public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext>
|
||||
|
||||
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);
|
||||
|
||||
modelBuilder.Entity<Portfolio>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
||||
|
||||
modelBuilder.Entity<Programme>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
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);
|
||||
|
||||
modelBuilder.Entity<Project>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<Award>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<Notification>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<TenantOrganisation>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<TenantUser>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
|
||||
modelBuilder.Entity<AuditLog>()
|
||||
.HasQueryFilter(e => BypassFilter || e.TenantId == CurrentTenantId);
|
||||
method?.Invoke(this, [modelBuilder]);
|
||||
}
|
||||
}
|
||||
|
||||
modelBuilder.Entity<Organisation>().HasQueryFilter(o => BypassFilter ||
|
||||
Set<TenantOrganisation>().Any(to => to.TenantId == CurrentTenantId && to.OrganisationId == o.Id));
|
||||
|
||||
modelBuilder.Entity<User>().HasQueryFilter(o => BypassFilter ||
|
||||
Set<TenantUser>().Any(to => to.TenantId == CurrentTenantId && to.UserId == o.Id));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
+2272
File diff suppressed because it is too large
Load Diff
+5767
File diff suppressed because it is too large
Load Diff
+560
-512
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user