diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.cs index 213e8a43..fe175e4a 100644 --- a/src/AliasVault.Api/Controllers/VaultController.cs +++ b/src/AliasVault.Api/Controllers/VaultController.cs @@ -240,7 +240,7 @@ public async Task Update([FromBody] Shared.Models.WebApi.Vault.Va // Update user email claims if email addresses have been supplied. if (model.EmailAddressList.Count > 0) { - await UpdateUserEmailClaims(context, user.Id, model.EmailAddressList); + await UpdateUserEmailClaims(context, user, model.EmailAddressList); } // Sync user public key if supplied. @@ -371,14 +371,14 @@ private async Task ApplyVaultRetention(AliasServerDbContext context, string user /// Updates the user's email claims based on the provided email address list. /// /// The database context. - /// The ID of the user. + /// The user object. /// The list of new email addresses to claim. /// A task representing the asynchronous operation. - private async Task UpdateUserEmailClaims(AliasServerDbContext context, string userId, List newEmailAddresses) + private async Task UpdateUserEmailClaims(AliasServerDbContext context, AliasVaultUser user, List newEmailAddresses) { // Get all existing user email claims. var existingEmailClaims = await context.UserEmailClaims - .Where(x => x.UserId == userId) + .Where(x => x.UserId == user.Id) .Select(x => x.Address) .ToListAsync(); @@ -398,10 +398,10 @@ private async Task UpdateUserEmailClaims(AliasServerDbContext context, string us var existingClaim = await context.UserEmailClaims .FirstOrDefaultAsync(x => x.Address == sanitizedEmail); - if (existingClaim != null && existingClaim.UserId != userId) + if (existingClaim != null && existingClaim.UserId != user.Id) { // Email address is already claimed by another user. Log the error and continue. - logger.LogWarning("{User} tried to claim email address: {Email} but it is already claimed by another user.", userId, sanitizedEmail); + logger.LogWarning("{User} tried to claim email address: {Email} but it is already claimed by another user.", user.UserName, sanitizedEmail); continue; } @@ -411,7 +411,7 @@ private async Task UpdateUserEmailClaims(AliasServerDbContext context, string us { context.UserEmailClaims.Add(new UserEmailClaim { - UserId = userId, + UserId = user.Id, Address = sanitizedEmail, AddressLocal = sanitizedEmail.Split('@')[0], AddressDomain = sanitizedEmail.Split('@')[1], @@ -422,7 +422,7 @@ private async Task UpdateUserEmailClaims(AliasServerDbContext context, string us catch (DbUpdateException ex) { // Error while adding email claim. Log the error and continue. - logger.LogWarning(ex, "Error while adding UserEmailClaim with email: {Email} for user: {UserId}.", sanitizedEmail, userId); + logger.LogWarning(ex, "Error while adding UserEmailClaim with email: {Email} for user: {UserId}.", sanitizedEmail, user.UserName); } } }