Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/backend/Data/RegistryDbContext.cs
blob: a7dfa34e5aa1930d2cddf794b406b54c924484c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.0

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using XSharp.Web.Api.Auth;

namespace XSharp.Web.Api.Data;

internal sealed class RegistryDbContext(DbContextOptions<RegistryDbContext> options)
    : IdentityDbContext<ApplicationUser, IdentityRole<Guid>, Guid>(options)
{
    public DbSet<AuthChallenge> AuthChallenges => Set<AuthChallenge>();
    public DbSet<RegistryToken> RegistryTokens => Set<RegistryToken>();

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.HasDefaultSchema("registry");

        builder.Entity<ApplicationUser>(entity =>
        {
            entity.Property(user => user.CreatedAt).IsRequired();
            entity.Property(user => user.PublisherName).HasMaxLength(PublisherNamePolicy.MaximumLength);
            entity.Property(user => user.NormalizedPublisherName).HasMaxLength(PublisherNamePolicy.MaximumLength);
            entity.HasIndex(user => user.NormalizedEmail).IsUnique();
            entity.HasIndex(user => user.NormalizedPublisherName)
                .IsUnique()
                .HasDatabaseName("PublisherNameIndex");
        });
        builder.Entity<AuthChallenge>(entity =>
        {
            entity.HasKey(challenge => challenge.Id);
            entity.Property(challenge => challenge.Purpose).HasMaxLength(32);
            entity.Property(challenge => challenge.CodeDigest).HasMaxLength(32);
            entity.HasIndex(challenge => new { challenge.UserId, challenge.Purpose, challenge.ConsumedAt });
            entity.HasOne<ApplicationUser>()
                .WithMany()
                .HasForeignKey(challenge => challenge.UserId)
                .OnDelete(DeleteBehavior.Cascade);
        });
        builder.Entity<RegistryToken>(entity =>
        {
            entity.HasKey(token => token.Id);
            entity.Property(token => token.Name).HasMaxLength(80);
            entity.Property(token => token.Digest).HasMaxLength(32);
            entity.Property(token => token.Prefix).HasMaxLength(16);
            entity.Property(token => token.Scopes).HasColumnType("text[]");
            entity.HasIndex(token => token.Digest).IsUnique();
            entity.HasIndex(token => new { token.UserId, token.RevokedAt });
            entity.HasOne<ApplicationUser>()
                .WithMany()
                .HasForeignKey(token => token.UserId)
                .OnDelete(DeleteBehavior.Cascade);
        });
    }
}