| | | 1 | | using ArturRios.Data.Relational.Core.Configuration; |
| | | 2 | | using ArturRios.Heimdall.Data.EntityMaps; |
| | | 3 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 4 | | using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace ArturRios.Heimdall.Data.Configuration; |
| | | 9 | | |
| | | 10 | | public class AppDbContext( |
| | | 11 | | DbContextOptions options, |
| | | 12 | | ILoggerFactory loggerFactory, |
| | 6956 | 13 | | DbContextDiagnosticsOptions diagnostics) : BaseDbContext(options), IDataProtectionKeyContext |
| | | 14 | | { |
| | | 15 | | private const string Schema = "heimdall"; |
| | | 16 | | |
| | 9477 | 17 | | public DbSet<Person> Persons { get; init; } |
| | 7601 | 18 | | public DbSet<Scope> Scopes { get; init; } |
| | 7862 | 19 | | public DbSet<Role> Roles { get; init; } |
| | 7133 | 20 | | public DbSet<Application> Applications { get; init; } |
| | 7062 | 21 | | public DbSet<ApplicationCredential> ApplicationCredentials { get; init; } |
| | 7077 | 22 | | public DbSet<GoogleUser> GoogleUsers { get; init; } |
| | 7262 | 23 | | public DbSet<ScopeOwner> ScopeOwners { get; init; } |
| | 7152 | 24 | | public DbSet<ScopeUser> ScopeUsers { get; init; } |
| | 7037 | 25 | | public DbSet<ScopePermission> ScopePermissions { get; init; } |
| | 7044 | 26 | | public DbSet<PasswordResetToken> PasswordResetTokens { get; init; } |
| | 7031 | 27 | | public DbSet<EmailVerificationToken> EmailVerificationTokens { get; init; } |
| | 7027 | 28 | | public DbSet<PendingRegistration> PendingRegistrations { get; init; } |
| | 7202 | 29 | | public DbSet<TwoFactorAuth> TwoFactorAuths { get; init; } |
| | 7019 | 30 | | public DbSet<TwoFactorEmailCode> TwoFactorEmailCodes { get; init; } |
| | 7042 | 31 | | public DbSet<TwoFactorRecoveryCode> TwoFactorRecoveryCodes { get; init; } |
| | 6988 | 32 | | public DbSet<AuditLog> AuditLogs { get; init; } |
| | 6971 | 33 | | public DbSet<ErasedSubject> ErasedSubjects { get; init; } |
| | 6965 | 34 | | public DbSet<AttemptNoticeBudget> AttemptNoticeBudgets { get; init; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// ASP.NET Core's Data Protection key ring, kept in the database rather than on a local |
| | | 38 | | /// filesystem (<see cref="IDataProtectionKeyContext" />). |
| | | 39 | | /// </summary> |
| | | 40 | | /// <remarks> |
| | | 41 | | /// Not a domain table, and the only one here that no entity map configures — its shape |
| | | 42 | | /// belongs to Data Protection. It is in this context because the keys have to outlive the |
| | | 43 | | /// container and be reachable from every instance: the TOTP secrets of UC-36 are encrypted |
| | | 44 | | /// with them (NFR-16), and a key ring that is lost or not shared makes every one of those |
| | | 45 | | /// secrets undecryptable, which silently stops the authenticator-app factor from ever |
| | | 46 | | /// verifying again. The default is a directory on the local filesystem, which the image does |
| | | 47 | | /// not persist and two instances do not share. |
| | | 48 | | /// </remarks> |
| | 7861 | 49 | | public DbSet<DataProtectionKey> DataProtectionKeys { get; init; } |
| | | 50 | | |
| | | 51 | | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| | | 52 | | { |
| | 6395 | 53 | | optionsBuilder |
| | 6395 | 54 | | .UseLoggerFactory(loggerFactory) |
| | 6395 | 55 | | .UseSnakeCaseNamingConvention() |
| | 6395 | 56 | | .EnableDetailedErrors(diagnostics.DetailedErrors) |
| | 6395 | 57 | | .EnableSensitiveDataLogging(diagnostics.SensitiveDataLogging); |
| | 6395 | 58 | | } |
| | | 59 | | |
| | | 60 | | protected override void OnModelCreating(ModelBuilder modelBuilder) |
| | | 61 | | { |
| | 3 | 62 | | modelBuilder.HasDefaultSchema(Schema); |
| | | 63 | | |
| | 3 | 64 | | modelBuilder.Entity<Person>().Configure(); |
| | 3 | 65 | | modelBuilder.Entity<Scope>().Configure(); |
| | 3 | 66 | | modelBuilder.Entity<Role>().Configure(); |
| | 3 | 67 | | modelBuilder.Entity<Application>().Configure(); |
| | 3 | 68 | | modelBuilder.Entity<ApplicationCredential>().Configure(); |
| | 3 | 69 | | modelBuilder.Entity<GoogleUser>().Configure(); |
| | 3 | 70 | | modelBuilder.Entity<ScopePermission>().Configure(); |
| | 3 | 71 | | modelBuilder.Entity<ScopeOwner>().Configure(); |
| | 3 | 72 | | modelBuilder.Entity<ScopeUser>().Configure(); |
| | 3 | 73 | | modelBuilder.Entity<PasswordResetToken>().Configure(); |
| | 3 | 74 | | modelBuilder.Entity<EmailVerificationToken>().Configure(); |
| | 3 | 75 | | modelBuilder.Entity<PendingRegistration>().Configure(); |
| | 3 | 76 | | modelBuilder.Entity<TwoFactorAuth>().Configure(); |
| | 3 | 77 | | modelBuilder.Entity<TwoFactorEmailCode>().Configure(); |
| | 3 | 78 | | modelBuilder.Entity<TwoFactorRecoveryCode>().Configure(); |
| | 3 | 79 | | modelBuilder.Entity<AuditLog>().Configure(); |
| | 3 | 80 | | modelBuilder.Entity<ErasedSubject>().Configure(); |
| | 3 | 81 | | modelBuilder.Entity<AttemptNoticeBudget>().Configure(); |
| | 3 | 82 | | } |
| | | 83 | | } |