Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update email claim datamodel (#2159)
Leendert de Borstff862beadfcb
diff --git a/apps/server/AliasVault.Admin/Main/Models/UserEmailClaimWithCount.cs b/apps/server/AliasVault.Admin/Main/Models/EmailClaimWithCount.cssimilarity index 93%rename from apps/server/AliasVault.Admin/Main/Models/UserEmailClaimWithCount.csrename to apps/server/AliasVault.Admin/Main/Models/EmailClaimWithCount.csindex be775bc63..6cb129f6a 100644--- a/apps/server/AliasVault.Admin/Main/Models/UserEmailClaimWithCount.cs+++ b/apps/server/AliasVault.Admin/Main/Models/EmailClaimWithCount.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="UserEmailClaimWithCount.cs" company="aliasvault">+// <copyright file="EmailClaimWithCount.cs" company="aliasvault">// Copyright (c) aliasvault. All rights reserved.// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.// </copyright>@@ -10,7 +10,7 @@ namespace AliasVault.Admin.Main.Models;/// <summary>/// User email claim view model with count./// </summary>-public class UserEmailClaimWithCount+public class EmailClaimWithCount{/// <summary>/// Gets or sets the id.diff --git a/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs b/apps/server/Databases/AliasServerDb/AliasServerDbContext.csindex 2a74a8d80..1c23f20c5 100644--- a/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs+++ b/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs@@ -115,14 +115,24 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyConpublic DbSet<EmailAttachment> EmailAttachments { get; set; }/// <summary>- /// Gets or sets the UserEmailClaims DbSet.+ /// Gets or sets the EmailClaims DbSet./// </summary>- public DbSet<UserEmailClaim> UserEmailClaims { get; set; }+ public DbSet<EmailClaim> EmailClaims { get; set; }/// <summary>- /// Gets or sets the UserEncryptionKeys DbSet.+ /// Gets or sets the Groups DbSet./// </summary>- public DbSet<UserEncryptionKey> UserEncryptionKeys { get; set; }+ public DbSet<Group> Groups { get; set; }++ /// <summary>+ /// Gets or sets the GroupMembers DbSet.+ /// </summary>+ public DbSet<GroupMember> GroupMembers { get; set; }++ /// <summary>+ /// Gets or sets the EncryptionKeys DbSet.+ /// </summary>+ public DbSet<EncryptionKey> EncryptionKeys { get; set; }/// <summary>/// Gets or sets the Logs DbSet.@@ -288,18 +298,48 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyConbuilder.HasIndex(e => e.Application);});- // Configure VaultManifest - one row per logical manifest (current revision), keyed by ManifestId.+ /*+ * Configure the user's personal group reference.+ */+ modelBuilder.Entity<AliasVaultUser>(builder =>+ {+ builder.HasOne(e => e.PersonalGroup)+ .WithOne()+ .HasForeignKey<AliasVaultUser>(e => e.PersonalGroupId)+ .OnDelete(DeleteBehavior.Restrict);++ builder.HasIndex(e => e.PersonalGroupId).IsUnique().HasDatabaseName("UX_AliasVaultUsers_PersonalGroupId");+ });++ // Configure GroupMember, who may be granted access to the group's shared folders.+ modelBuilder.Entity<GroupMember>(builder =>+ {+ builder.HasOne(e => e.Group)+ .WithMany(g => g.Members)+ .HasForeignKey(e => e.GroupId)+ .OnDelete(DeleteBehavior.Cascade);++ // Deleting a user removes their memberships everywhere, of their own group and other possibly joined groups.+ builder.HasOne(e => e.User)+ .WithMany()+ .HasForeignKey(e => e.UserId)+ .OnDelete(DeleteBehavior.Cascade);++ builder.HasIndex(e => e.UserId);+ });++ // Configure VaultManifest: one row per logical manifest (current revision), keyed by ManifestId.modelBuilder.Entity<VaultManifest>(builder =>{builder.HasKey(e => e.ManifestId);- builder.HasOne(l => l.User)- .WithMany(c => c.VaultManifests)- .HasForeignKey(l => l.OwnerUserId)++ builder.HasOne(e => e.OwnerGroup)+ .WithMany()+ .HasForeignKey(e => e.OwnerGroupId).OnDelete(DeleteBehavior.Cascade);- // Every user has exactly one root manifest (the residual container for everything not carved out- // into another manifest).- builder.HasIndex(e => e.OwnerUserId).HasFilter("\"IsRoot\"").IsUnique().HasDatabaseName("UX_VaultManifests_OwnerUserId_Root");+ builder.HasIndex(e => e.OwnerGroupId).HasFilter("\"IsRoot\"").IsUnique().HasDatabaseName("UX_VaultManifests_OwnerGroupId_Root");+ builder.HasIndex(e => e.OwnerGroupId);});// Configure VaultManifestsHistory - superseded revisions, composite key (ManifestId, RevisionNumber).@@ -311,9 +351,6 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyCon.WithMany().HasForeignKey(e => e.ManifestId).OnDelete(DeleteBehavior.Cascade);-- // Denormalized owner id for direct per-user queries (admin statistics, retention).- builder.HasIndex(e => e.OwnerUserId);});// Configure VaultKey: a wrapped-VEK grant for (holder, manifest, unlock method).@@ -329,30 +366,45 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyConbuilder.Property(e => e.Metadata).HasColumnType("jsonb");});- // Configure UserEmailClaim - AliasVaultUser relationship- // Note: when a user is deleted the email claims user FK's should be set to NULL- // so the claims themselves are preserved to prevent re-use of the email address.- modelBuilder.Entity<UserEmailClaim>()- .HasOne(e => e.User)- .WithMany(u => u.EmailClaims)- .HasForeignKey(e => e.UserId)+ // Configure EmailClaim - Group relationship. SetNull: when the owning group is deleted (its+ // owner deleted their account) the claim becomes an ownerless tombstone, preserved to prevent+ // re-use of the email address; delivery rejects mail for it.+ modelBuilder.Entity<EmailClaim>()+ .HasOne(e => e.OwnerGroup)+ .WithMany()+ .HasForeignKey(e => e.OwnerGroupId).OnDelete(DeleteBehavior.SetNull);- // Configure Email - UserEncryptionKey relationship+ /*+ * Configure EmailClaim - VaultManifest relationship. SetNull for the same reason the user FK is:+ * the claim outlives the folder so the address cannot be re-registered by someone else. A claim left+ * with a null manifest is an orphaned shared alias, which reconciliation then disables.+ */+ modelBuilder.Entity<EmailClaim>(builder =>+ {+ builder.HasOne(e => e.VaultManifest)+ .WithMany()+ .HasForeignKey(e => e.VaultManifestId)+ .OnDelete(DeleteBehavior.SetNull);++ builder.HasIndex(e => e.VaultManifestId);+ });++ // Configure Email - EncryptionKey relationshipmodelBuilder.Entity<Email>().HasOne(l => l.EncryptionKey).WithMany(c => c.Emails)- .HasForeignKey(l => l.UserEncryptionKeyId)+ .HasForeignKey(l => l.EncryptionKeyId).OnDelete(DeleteBehavior.Cascade);- // Configure UserEncryptionKey - AliasVaultUser relationship- modelBuilder.Entity<UserEncryptionKey>()+ // Configure EncryptionKey - AliasVaultUser relationship+ modelBuilder.Entity<EncryptionKey>().HasOne(l => l.User).WithMany(c => c.EncryptionKeys).HasForeignKey(l => l.UserId).OnDelete(DeleteBehavior.Cascade);- modelBuilder.Entity<UserEncryptionKey>(builder =>+ modelBuilder.Entity<EncryptionKey>(builder =>{// A folder-scoped key is removed when its manifest is removed; a personal key (null manifest) is unaffected.builder.HasOne(k => k.VaultManifest)@@ -364,10 +416,10 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyConbuilder.HasIndex(k => new { k.UserId, k.VaultManifestId, k.IsPrimary });});- // Configure UserEmailClaim - UserEncryptionKey relationship. Restrict rather than cascade: an+ // Configure EmailClaim - EncryptionKey relationship. Restrict rather than cascade: an// email claim outlives its key on purpose (claims are retained to prevent address re-use), so a// key must be re-pointed or nulled before it can be removed.- modelBuilder.Entity<UserEmailClaim>()+ modelBuilder.Entity<EmailClaim>().HasOne(c => c.EncryptionKey).WithMany().HasForeignKey(c => c.EncryptionKeyId)diff --git a/apps/server/Databases/AliasServerDb/AliasVaultUser.cs b/apps/server/Databases/AliasServerDb/AliasVaultUser.csindex 8023b05a2..18bd327e9 100644--- a/apps/server/Databases/AliasServerDb/AliasVaultUser.cs+++ b/apps/server/Databases/AliasServerDb/AliasVaultUser.cs@@ -95,7 +95,7 @@ public class AliasVaultUser : IdentityUser/// <summary>/// Gets or sets the collection of EncryptionKeys./// </summary>- public virtual ICollection<UserEncryptionKey> EncryptionKeys { get; set; } = [];+ public virtual ICollection<EncryptionKey> EncryptionKeys { get; set; } = [];/// <summary>/// Gets or sets the collection of vault unlock keys (KEK/VEK model). Empty for users still on the legacydiff --git a/apps/server/Databases/AliasServerDb/Email.cs b/apps/server/Databases/AliasServerDb/Email.csindex a635954aa..7dadeb183 100644--- a/apps/server/Databases/AliasServerDb/Email.cs+++ b/apps/server/Databases/AliasServerDb/Email.cs@@ -30,14 +30,14 @@ public class Email/// Gets or sets encryption key foreign key./// </summary>[StringLength(255)]- public Guid UserEncryptionKeyId { get; set; }+ public Guid EncryptionKeyId { get; set; }/// <summary>- /// Gets or sets foreign key to the UserEncryptionKey object which contains the public key used for encrypting+ /// Gets or sets foreign key to the EncryptionKey object which contains the public key used for encrypting/// the symmetric encryption key./// </summary>- [ForeignKey("UserEncryptionKeyId")]- public virtual UserEncryptionKey EncryptionKey { get; set; } = null!;+ [ForeignKey("EncryptionKeyId")]+ public virtual EncryptionKey EncryptionKey { get; set; } = null!;/// <summary>/// Gets or sets the encrypted symmetric key which was used to encrypt the email message.diff --git a/apps/server/Databases/AliasServerDb/UserEmailClaim.cs b/apps/server/Databases/AliasServerDb/EmailClaim.cssimilarity index 56%rename from apps/server/Databases/AliasServerDb/UserEmailClaim.csrename to apps/server/Databases/AliasServerDb/EmailClaim.csindex ba36213ab..24a043693 100644--- a/apps/server/Databases/AliasServerDb/UserEmailClaim.cs+++ b/apps/server/Databases/AliasServerDb/EmailClaim.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="UserEmailClaim.cs" company="aliasvault">+// <copyright file="EmailClaim.cs" company="aliasvault">// Copyright (c) aliasvault. All rights reserved.// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.// </copyright>@@ -12,12 +12,12 @@ using System.ComponentModel.DataAnnotations.Schema;using Microsoft.EntityFrameworkCore;/// <summary>-/// UserEmailClaim object. This object is used to reserve an email address for a user.+/// EmailClaim object. This object is used to reserve an email address for a user./// </summary>[Index(nameof(Address), IsUnique = true)]-[Index(nameof(UserId), nameof(Disabled))]-[Index(nameof(UserId), nameof(CreatedAt))]-public class UserEmailClaim+[Index(nameof(OwnerGroupId), nameof(Disabled))]+[Index(nameof(OwnerGroupId), nameof(CreatedAt))]+public class EmailClaim{/// <summary>/// Gets or sets the ID.@@ -26,18 +26,37 @@ public class UserEmailClaimpublic Guid Id { get; set; }/// <summary>- /// Gets or sets user ID foreign key. This can be null if the email claim was associated with a user that has since been deleted.- /// Email claims are meant to be preserved even if the user is deleted to prevent re-use of the email address.+ /// Gets or sets the shared folder this alias lives in, or null for a personal alias.+ /// <para>+ /// This is what makes a shared alias belong to the <em>folder</em> rather than to whichever member happened+ /// to create it. Without it, revoking that member disables an alias sitting in someone else's folder (their+ /// next push no longer lists it), and their account deletion strands the address permanently. Read access+ /// and routing reconciliation both key off this column.+ /// </para>/// </summary>- [StringLength(255)]- public string? UserId { get; set; }+ public Guid? VaultManifestId { get; set; }++ /// <summary>+ /// Gets or sets the navigation property to the shared folder this alias lives in.+ /// </summary>+ [ForeignKey("VaultManifestId")]+ public virtual VaultManifest? VaultManifest { get; set; }++ /// <summary>+ /// Gets or sets the group this alias is owned by and billed to — like every manifest, an alias is owned by+ /// a group, never by a user. For a personal alias that is the claimer's Personal group; for an alias in a+ /// shared folder it is the group that owns the folder, so a family's aliases draw on the family owner's+ /// allowance no matter which member created them, and stay correct when the folder is transferred.+ /// Null when the owning group has been deleted (the user deleted their account): the claim is then a+ /// tombstone kept solely to prevent re-use of the address, and delivery rejects its mail.+ /// </summary>+ public Guid? OwnerGroupId { get; set; }/// <summary>- /// Gets or sets foreign key to the AliasVaultUser object. This can be null if the email claim was associated with a user that has since been deleted.- /// Email claims are meant to be preserved even if the user is deleted to prevent re-use of the email address.+ /// Gets or sets the navigation property to the owning group./// </summary>- [ForeignKey("UserId")]- public virtual AliasVaultUser? User { get; set; }+ [ForeignKey("OwnerGroupId")]+ public virtual Group? OwnerGroup { get; set; }/// <summary>/// Gets or sets the encryption key incoming mail for this alias is encrypted with. For most cases this will be null,@@ -50,7 +69,7 @@ public class UserEmailClaim/// Gets or sets the navigation property to the encryption key mail for this alias is encrypted with./// </summary>[ForeignKey("EncryptionKeyId")]- public virtual UserEncryptionKey? EncryptionKey { get; set; }+ public virtual EncryptionKey? EncryptionKey { get; set; }/// <summary>/// Gets or sets the full email address.diff --git a/apps/server/Databases/AliasServerDb/UserEncryptionKey.cs b/apps/server/Databases/AliasServerDb/EncryptionKey.cssimilarity index 89%rename from apps/server/Databases/AliasServerDb/UserEncryptionKey.csrename to apps/server/Databases/AliasServerDb/EncryptionKey.csindex 41a1d2478..d64bca1c5 100644--- a/apps/server/Databases/AliasServerDb/UserEncryptionKey.cs+++ b/apps/server/Databases/AliasServerDb/EncryptionKey.cs@@ -1,5 +1,5 @@//------------------------------------------------------------------------// <copyright file="UserEncryptionKey.cs" company="aliasvault">+// <copyright file="EncryptionKey.cs" company="aliasvault">// Copyright (c) aliasvault. All rights reserved.// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.// </copyright>@@ -10,13 +10,13 @@ using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;/// <summary>-/// UserEncryptionKey object. This object is used for storing user public keys for encryption.+/// EncryptionKey object. This object is used for storing user public keys for encryption./// <para>/// A row can be either personal (VaultManifestId is null) or folder-scoped (VaultManifestId is set)./// Personal private keys live in the user's EncryptionKeys data bucket, folder-scoped private keys live in the shared folder's manifest./// </para>/// </summary>-public class UserEncryptionKey+public class EncryptionKey{/// <summary>/// Gets or sets the ID.@@ -28,13 +28,13 @@ public class UserEncryptionKey/// Gets or sets user ID foreign key./// </summary>[StringLength(255)]- public string UserId { get; set; } = null!;+ public string? UserId { get; set; }/// <summary>/// Gets or sets foreign key to the AliasVaultUser object./// </summary>[ForeignKey("UserId")]- public virtual AliasVaultUser User { get; set; } = null!;+ public virtual AliasVaultUser? User { get; set; }/// <summary>/// Gets or sets the shared-folder manifest this key belongs to, or null when it is the user's own personal key.