Files
Heimdall/src/Heimdall.Application/Users/Commands/UpdateUser/UpdateUserHandler.cs
T
arnaud.dartois 622add46ad
Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Failing after 4m5s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 1s
refactor(identity): isolate ASP.NET Identity in Infrastructure
2026-11-04 11:37:53 +01:00

90 lines
3.6 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.Common.Persistence;
using Heimdall.Application.DTOs;
using Heimdall.Domain.Entities;
using MediatR;
using System.ComponentModel.DataAnnotations;
using Heimdall.Application.Services;
namespace Heimdall.Application.Users.Commands.UpdateUser;
public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResult<UserResponse>>
{
private readonly IIdentityAccountService _userManager;
private readonly IUserRepository _repository;
public UpdateUserHandler(IIdentityAccountService userManager, IUserRepository repository)
{
_userManager = userManager;
_repository = repository;
}
public async Task<ServiceResult<UserResponse>> Handle(UpdateUserCommand command, CancellationToken ct)
{
var user = await _userManager.FindByIdAsync(command.Id, ct).ConfigureAwait(false);
if (user is null)
return ServiceResult<UserResponse>.NotFound("User not found");
var language = command.Language?.Trim().ToLowerInvariant();
if (language is not null && language is not ("fr" or "en"))
return ServiceResult<UserResponse>.Failure("Langue non prise en charge", 400);
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, ct).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 = language ?? user.Language;
var result = await _userManager.UpdateAsync(user, ct).ConfigureAwait(false);
if (!result.Succeeded)
return ServiceResult<UserResponse>.Failure(
string.Join(", ", result.ErrorCodes));
if (emailChanged)
{
var stamp = await _userManager.UpdateSecurityStampAsync(user, ct).ConfigureAwait(false);
if (!stamp)
return ServiceResult<UserResponse>.Failure("Email updated but existing sessions could not be revoked", 500);
}
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!);
}
}