208 lines
6.7 KiB
C#
208 lines
6.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using W542.GandalfReborn.Data.Entities.App;
|
|
using W542.GandalfReborn.Data.Entities.Base;
|
|
using W542.GandalfReborn.Data.Entities.Security;
|
|
using W542.GandalfReborn.Data.Entities.Subject;
|
|
using W542.GandalfReborn.Data.Entities.Subject.SignIn;
|
|
using W542.GandalfReborn.Data.Entities.Tenant;
|
|
|
|
namespace W542.GandalfReborn.Data.Database;
|
|
|
|
public abstract class CoreContext<T>(DbContextOptions<T> options) : DbContext(options) where T : DbContext
|
|
{
|
|
public DbSet<TenantEntity> Tenants { get; set; }
|
|
public DbSet<SubjectEntity> Subjects { get; set; }
|
|
public DbSet<AppRelationEntity> Apps { get; set; }
|
|
public DbSet<TenantSubjectRelationEntity> TenantSubjectRelations { get; set; }
|
|
public DbSet<AppSubjectRelationEntity> AppSubjectRelations { get; set; }
|
|
public DbSet<AuthorityEntity> AuthorityEntities { get; set; }
|
|
public DbSet<TokenMetadataEntity> TokenMetadata { get; set; }
|
|
public DbSet<AuthCodeEntity> AuthCodes { get; set; }
|
|
|
|
public DbSet<AppRelationVersionEntity> AppsVersions { get; set; }
|
|
public DbSet<TenantVersionEntity> TenantVersions { get; set; }
|
|
public DbSet<AppSubjectRelationVersionEntity> AppSubjectRelationVersions { get; set; }
|
|
public DbSet<TenantSubjectRelationVersionEntity> TenantSubjectRelationVersions { get; set; }
|
|
public DbSet<AppSubjectRelationInternalAuthorityRelationVersionEntity> AppSubjectRelationInternalAuthorityRelationVersions { get; set; }
|
|
public DbSet<TenantSubjectRelationInternalAuthorityRelationVersionEntity> TenantSubjectRelationInternalAuthorityRelationVersions { get; set; }
|
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
{
|
|
#region Tenant
|
|
|
|
var tenantBuilder = builder.Entity<TenantEntity>();
|
|
|
|
tenantBuilder
|
|
.HasMany(x => x.Subjects)
|
|
.WithMany(x => x.Tenants)
|
|
.UsingEntity<TenantSubjectRelationEntity>(
|
|
r => r
|
|
.HasOne(x => x.Subject)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.SubjectId),
|
|
l => l
|
|
.HasOne(x => x.Tenant)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.TenantId),
|
|
j => j
|
|
.HasKey(x => new { x.SubjectId, x.TenantId })
|
|
);
|
|
|
|
tenantBuilder
|
|
.HasOne(x => x.Owner)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.OwnerId)
|
|
.IsRequired();
|
|
|
|
#endregion
|
|
|
|
#region TenantSubjectRelation
|
|
|
|
var tenantSubjectRelationBuilder = builder.Entity<TenantSubjectRelationEntity>();
|
|
|
|
tenantSubjectRelationBuilder
|
|
.HasMany(x => x.InternalAuthorities)
|
|
.WithMany(x => x.TenantSubjectRelations)
|
|
.UsingEntity<TenantSubjectRelationInternalAuthorityRelationEntity>(
|
|
r => r
|
|
.HasOne(x => x.InternalAuthority)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.InternalAuthorityId),
|
|
l => l
|
|
.HasOne(x => x.TenantSubjectRelation)
|
|
.WithMany()
|
|
.HasForeignKey(x => new { x.SubjectId, x.TenantId }),
|
|
j => j
|
|
.HasKey(x => new { x.SubjectId, x.TenantId, x.InternalAuthorityId })
|
|
)
|
|
.HasKey(x => new { x.SubjectId, x.TenantId });
|
|
|
|
#endregion
|
|
|
|
#region Subject
|
|
|
|
var subjectBuilder = builder.Entity<SubjectEntity>();
|
|
|
|
subjectBuilder
|
|
.HasMany(x => x.SignInMethods)
|
|
.WithOne(x => x.Subject)
|
|
.HasForeignKey(x => x.SubjectId)
|
|
.IsRequired();
|
|
|
|
subjectBuilder
|
|
.HasIndex(x => x.Name)
|
|
.IsUnique();
|
|
|
|
subjectBuilder
|
|
.HasData(new SubjectEntity
|
|
{
|
|
Id = 1,
|
|
Visibility = EntityVisibility.Active,
|
|
Name = "chris"
|
|
});
|
|
|
|
#endregion
|
|
|
|
#region SighnIns
|
|
|
|
var signInBuilder = builder.Entity<SignInEntity>();
|
|
|
|
signInBuilder
|
|
.HasIndex(x => x.Email)
|
|
.IsUnique();
|
|
|
|
#endregion
|
|
|
|
#region App
|
|
|
|
var appBuilder = builder.Entity<AppRelationEntity>();
|
|
|
|
appBuilder
|
|
.HasMany(x => x.Subjects)
|
|
.WithMany(x => x.Apps)
|
|
.UsingEntity<AppSubjectRelationEntity>(
|
|
r => r
|
|
.HasOne(x => x.Subject)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.SubjectId),
|
|
l => l
|
|
.HasOne(x => x.App)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.AppId),
|
|
j => j
|
|
.HasKey(x => new { x.SubjectId, x.AppId })
|
|
);
|
|
|
|
appBuilder
|
|
.HasOne(x => x.Tenant)
|
|
.WithMany(x => x.Apps)
|
|
.HasForeignKey(x => x.TenantId);
|
|
|
|
#endregion
|
|
|
|
#region AppSubjectRelation
|
|
|
|
var appSubjectRelationBuilder = builder.Entity<AppSubjectRelationEntity>();
|
|
|
|
appSubjectRelationBuilder
|
|
.HasMany(x => x.InternalAuthorities)
|
|
.WithMany(x => x.AppSubjectRelations)
|
|
.UsingEntity<AppSubjectRelationInternalAuthorityRelationEntity>(
|
|
r => r
|
|
.HasOne(x => x.InternalAuthority)
|
|
.WithMany()
|
|
.HasForeignKey(x => x.InternalAuthorityId),
|
|
l => l
|
|
.HasOne(x => x.AppSubjectRelation)
|
|
.WithMany()
|
|
.HasForeignKey(x => new { x.SubjectId, x.AppId }),
|
|
j => j
|
|
.HasKey(x => new { x.SubjectId, x.AppId, x.InternalAuthorityId })
|
|
)
|
|
.HasKey(x => new { x.SubjectId, x.AppId });
|
|
|
|
#endregion
|
|
|
|
#region Authority
|
|
|
|
var authorityBuilder = builder.Entity<AuthorityEntity>();
|
|
|
|
authorityBuilder
|
|
.HasData(
|
|
new AuthorityEntity
|
|
{
|
|
Id = 1,
|
|
Type = AuthorityType.Tenant,
|
|
Name = "Tenant_Read",
|
|
Description = "Allows users to read tenants"
|
|
}, new AuthorityEntity
|
|
{
|
|
Id = 2,
|
|
Type = AuthorityType.App,
|
|
Name = "App_Read",
|
|
Description = "Allows users to read apps"
|
|
}
|
|
);
|
|
|
|
authorityBuilder
|
|
.HasKey(x => x.Id);
|
|
|
|
authorityBuilder
|
|
.HasIndex(x => x.Name)
|
|
.IsUnique();
|
|
|
|
#endregion
|
|
|
|
#region Tokens
|
|
|
|
builder.Entity<TokenMetadataEntity>();
|
|
|
|
#endregion
|
|
|
|
#region AuthCodes
|
|
|
|
builder.Entity<AuthCodeEntity>();
|
|
|
|
#endregion
|
|
}
|
|
} |