This commit is contained in:
2026-05-17 20:54:09 -04:00
parent 6da2183583
commit 74c21ee5cc
3000 changed files with 11794 additions and 15301 deletions

View File

@@ -190,7 +190,93 @@
private async Task OnEditClicked(UserDto row)
{
// Save the old user dto for comparison
UserDto oldUserDto = new UserDto { Id = row.Id, Username = row.Username, Roles = new List<string>(row.Roles)};
var options = new DialogOptions { CloseOnEscapeKey = true, BackdropClick = false};
var parameters = new DialogParameters<UserManagementDialog>
{
{x=>x.Model, row},
{x=>x.IsUpdate, true}
};
var dialog = await DialogService.ShowAsync<UserManagementDialog>("Update User", parameters, options);
var dialogResult = await dialog.Result;
if (dialogResult is not null && !dialogResult.Canceled)
{
var newUserDto = (UserDto?)dialogResult.Data;
if (newUserDto is null)
{
Snackbar.Add("Application error, user dialog returned a missing value.", Severity.Error);
return;
}
ApplicationUser? appUser = await UserManager.FindByIdAsync(row.Id);
if (appUser is null)
{
Snackbar.Add("Could not find edited user in the database!", Severity.Error);
return;
}
if (oldUserDto.Username != newUserDto.Username)
{
await UserManager.SetEmailAsync(appUser, newUserDto.Username);
var emailConfirmationToken = await UserManager.GenerateEmailConfirmationTokenAsync(appUser);
var confirmResult = await UserManager.ConfirmEmailAsync(appUser, emailConfirmationToken);
if (confirmResult is null || confirmResult.Errors.Any())
{
Snackbar.Add("Failed to confirm user's email address internally.", Severity.Error);
return;
}
var usernameResult = await UserManager.SetUserNameAsync(appUser, newUserDto.Username);
if (usernameResult is null || usernameResult.Errors.Any())
{
Snackbar.Add("Failed to update username.", Severity.Error);
return;
}
}
// Validate new password here
if (!string.IsNullOrWhiteSpace(newUserDto.Password)) {
var removeResult = await UserManager.RemovePasswordAsync(appUser);
if (!removeResult.Succeeded)
{
Snackbar.Add("Failed to remove old password for user", Severity.Error);
return;
}
var addResult = await UserManager.AddPasswordAsync(appUser, newUserDto.Password);
if (!addResult.Succeeded)
{
Snackbar.Add("Failed to set user's new password.", Severity.Error);
return;
}
}
// Overwrite roles if they have changed
if (!(new HashSet<string>(oldUserDto.Roles).SetEquals(newUserDto.Roles)))
{
var currentRoles = await UserManager.GetRolesAsync(appUser);
var removeResult = await UserManager.RemoveFromRolesAsync(appUser, currentRoles);
if (!removeResult.Succeeded)
{
Snackbar.Add("Failed to update roles for user", Severity.Error);
return;
}
var addResult = await UserManager.AddToRolesAsync(appUser, newUserDto.Roles);
if (!addResult.Succeeded)
{
Snackbar.Add("Failed to add new roles to user.", Severity.Error);
return;
}
}
}
Snackbar.Add("User updated!", Severity.Success);
}
private async Task OnDeleteClicked(UserDto row)