feat(users): allow admins to update email
This commit is contained in:
1 parent
1107aa6170
commit
d2e291c4b1
9 files changed
+185
-8
No files matched your search
@@ -21,7 +21,7 @@ public record CreateUserRequest(
|
||||
string DisplayName,
|
||||
IList<string>? Roles);
|
||||
|
||||
public record UpdateUserRequest(string DisplayName, string? Theme, string? Language);
|
||||
public record UpdateUserRequest(string DisplayName, string? Theme, string? Language, string? Email = null);
|
||||
|
||||
public record AssignRolesRequest(IList<string> Roles);
|
||||
|
||||
|
||||
@@ -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!);
|
||||
}
|
||||
|
||||
@@ -151,7 +151,8 @@ else
|
||||
<MudDialog @bind-Visible="_showEdit" Class="edit-user-dialog">
|
||||
<TitleContent><MudText Typo="Typo.h6">Utilisateur : @_editUser!.DisplayName</MudText></TitleContent>
|
||||
<DialogContent>
|
||||
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">@_editUser!.Email</MudText>
|
||||
<MudTextField @bind-Value="_editEmail" Label="Adresse email" Variant="Variant.Outlined"
|
||||
Class="mb-4" Immediate="true" />
|
||||
|
||||
@* Section: Informations *@
|
||||
<MudText Typo="Typo.subtitle1" Class="mb-2">Informations</MudText>
|
||||
@@ -323,6 +324,7 @@ else
|
||||
private UserItem? _editUser;
|
||||
private bool _showEdit = false;
|
||||
private string _editDisplayName = "";
|
||||
private string _editEmail = "";
|
||||
private string _editTheme = "";
|
||||
private string _editLanguage = "fr";
|
||||
private bool _editRoleAdmin;
|
||||
@@ -395,6 +397,7 @@ else
|
||||
{
|
||||
_editUser = user;
|
||||
_editDisplayName = user.DisplayName;
|
||||
_editEmail = user.Email;
|
||||
_editTheme = user.Theme ?? "Auto";
|
||||
_editLanguage = user.Language;
|
||||
_editRoleAdmin = user.Roles?.Contains("Admin") == true;
|
||||
@@ -415,6 +418,11 @@ else
|
||||
private async Task SaveEdit()
|
||||
{
|
||||
if (_editUser is null) return;
|
||||
if (!new System.ComponentModel.DataAnnotations.EmailAddressAttribute().IsValid(_editEmail))
|
||||
{
|
||||
_editError = "Adresse email invalide";
|
||||
return;
|
||||
}
|
||||
_saving = true;
|
||||
_editError = "";
|
||||
try
|
||||
@@ -432,8 +440,18 @@ else
|
||||
return;
|
||||
}
|
||||
|
||||
await Api.UpdateUserAsync(_editUser.Id, _editDisplayName, _editTheme, _editLanguage);
|
||||
await Api.AssignRolesAsync(_editUser.Id, selectedRoles);
|
||||
var update = await Api.UpdateUserWithEmailAsync(
|
||||
_editUser.Id, _editEmail, _editDisplayName, _editTheme, _editLanguage);
|
||||
if (!update.Succeeded)
|
||||
{
|
||||
_editError = update.ErrorMessage ?? "Erreur lors de la mise à jour";
|
||||
return;
|
||||
}
|
||||
if (!await Api.AssignRolesAsync(_editUser.Id, selectedRoles))
|
||||
{
|
||||
_editError = "Erreur lors de la mise à jour des rôles";
|
||||
return;
|
||||
}
|
||||
Snackbar.Add("Utilisateur mis à jour", Severity.Success);
|
||||
CloseEdit();
|
||||
await LoadUsers();
|
||||
|
||||
@@ -119,7 +119,11 @@ public class UsersController : ControllerBase
|
||||
public async Task<IActionResult> Update(
|
||||
Guid id, [FromBody] UpdateUserRequest request, CancellationToken ct)
|
||||
{
|
||||
var command = new UpdateUserCommand(id, request.DisplayName, request.Theme, request.Language);
|
||||
if (!this.TryGetUserId(out var actorId))
|
||||
return Unauthorized();
|
||||
|
||||
var command = new UpdateUserCommand(id, request.DisplayName, request.Theme, request.Language,
|
||||
request.Email, actorId);
|
||||
var result = await _mediator.Send(command, ct);
|
||||
return result.IsSuccess
|
||||
? Ok(result.Data)
|
||||
|
||||
@@ -297,6 +297,21 @@ public class HeimdallApiClient : IDisposable
|
||||
return response.IsSuccessStatusCode;
|
||||
}
|
||||
|
||||
public async Task<ApiResult<UserItem>> UpdateUserWithEmailAsync(
|
||||
Guid id, string email, string displayName, string? theme, string? language)
|
||||
{
|
||||
var client = CreateClient();
|
||||
var response = await client.PutAsJsonAsync($"api/users/{id}",
|
||||
new { email, displayName, theme, language }, Json);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
return ApiResult<UserItem>.Failure(await ReadErrorMessageAsync(response));
|
||||
|
||||
var user = await response.Content.ReadFromJsonAsync<UserItem>(Json);
|
||||
return user is null
|
||||
? ApiResult<UserItem>.Failure(InvalidServerResponse)
|
||||
: ApiResult<UserItem>.Success(user);
|
||||
}
|
||||
|
||||
public async Task<bool> AssignRolesAsync(Guid userId, IList<string> roles)
|
||||
{
|
||||
var client = CreateClient();
|
||||
|
||||
Reference in new issue
Block a user