feat(users): allow admins to update email
Heimdall Gitea / Build and tests (pull_request) Successful in 27m54s
Heimdall Gitea / Publish versioned release (pull_request) Skipped

This commit is contained in:
Arnaud Dartois committed 2026-10-21 10:09:14 +02:00
1 parent 1107aa6170
commit d2e291c4b1
9 files changed
+185 -8

No files matched your search

@@ -8,4 +8,6 @@ public record UpdateUserCommand(
Guid Id,
string? DisplayName,
string? Theme,
string? Language) : IRequest<ServiceResult<UserResponse>>;
string? Language,
string? Email = null,
Guid? ActorId = null) : IRequest<ServiceResult<UserResponse>>;
@@ -4,6 +4,7 @@ using Heimdall.Application.DTOs;
using Heimdall.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
namespace Heimdall.Application.Users.Commands.UpdateUser;
@@ -24,6 +25,28 @@ public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResul
if (user is null)
return ServiceResult<UserResponse>.NotFound("User not found");
var oldEmail = user.Email;
var oldEmailConfirmed = user.EmailConfirmed;
var emailChanged = false;
if (command.Email is not null)
{
var newEmail = command.Email.Trim();
if (!new EmailAddressAttribute().IsValid(newEmail))
return ServiceResult<UserResponse>.Failure("Adresse email invalide", 400);
if (!string.Equals(oldEmail, newEmail, StringComparison.OrdinalIgnoreCase))
{
var existingUser = await _userManager.FindByEmailAsync(newEmail).ConfigureAwait(false);
if (existingUser is not null && existingUser.Id != user.Id)
return ServiceResult<UserResponse>.Failure("Cette adresse email est déjà utilisée", 409);
user.Email = newEmail;
user.UserName = newEmail;
user.EmailConfirmed = false;
emailChanged = true;
}
}
user.DisplayName = command.DisplayName ?? user.DisplayName;
user.Theme = command.Theme ?? user.Theme;
user.Language = command.Language ?? user.Language;
@@ -33,6 +56,22 @@ public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResul
return ServiceResult<UserResponse>.Failure(
string.Join(", ", result.Errors.Select(e => e.Description)));
if (emailChanged)
{
await _repository.AddAuditLogAsync(command.ActorId, "User.EmailUpdated", "User", user.Id.ToString(),
oldValue: System.Text.Json.JsonSerializer.Serialize(new
{
email = oldEmail,
emailConfirmed = oldEmailConfirmed
}),
newValue: System.Text.Json.JsonSerializer.Serialize(new
{
email = user.Email,
emailConfirmed = user.EmailConfirmed
}),
ct: ct).ConfigureAwait(false);
}
var response = await _repository.GetByIdAsync(user.Id, ct).ConfigureAwait(false);
return ServiceResult<UserResponse>.Success(response!);
}