diff --git a/Hcs.WebApp/Components/Dialogs/AddUser.razor b/Hcs.WebApp/Components/Dialogs/AddUser.razor
index 241ea57..66a45f2 100644
--- a/Hcs.WebApp/Components/Dialogs/AddUser.razor
+++ b/Hcs.WebApp/Components/Dialogs/AddUser.razor
@@ -9,8 +9,6 @@
@inject UsersService UsersService
@inject DialogService DialogService
-Создание пользователя
-
diff --git a/Hcs.WebApp/Components/Dialogs/EditUser.razor b/Hcs.WebApp/Components/Dialogs/EditUser.razor
new file mode 100644
index 0000000..95ef91f
--- /dev/null
+++ b/Hcs.WebApp/Components/Dialogs/EditUser.razor
@@ -0,0 +1,117 @@
+@using Hcs.WebApp.Services
+@using Microsoft.AspNetCore.Authorization
+@using Microsoft.AspNetCore.Identity
+@using Microsoft.EntityFrameworkCore
+
+@attribute [Authorize]
+
+@inject UserManager UserManager;
+@inject RoleManager RoleManager
+@inject UsersService UsersService
+@inject DialogService DialogService
+
+
+
+
+
+ @errorMessage
+
+
+
+
+
+
+
+
+
+
+
+
+ @if (!string.IsNullOrEmpty(Input.Password))
+ {
+
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+@code {
+ sealed class InputModel
+ {
+ public string RoleId { get; set; }
+
+ public string Password { get; set; } = "";
+
+ public string ConfirmPassword { get; set; } = "";
+ }
+
+ IEnumerable roles;
+ bool inProgress;
+ bool hasError;
+ string errorMessage;
+
+ [Parameter]
+ public string UserId { get; set; }
+
+ [Parameter]
+ public string RoleId { get; set; }
+
+ [SupplyParameterFromForm]
+ InputModel Input { get; set; } = new();
+
+ protected override async Task OnInitializedAsync()
+ {
+ await base.OnInitializedAsync();
+
+ roles = await RoleManager.Roles.OrderBy(x => x.Priority).ToListAsync();
+
+ Input.RoleId = RoleId;
+ }
+
+ async Task DoEditUser(InputModel input)
+ {
+ if (inProgress) return;
+
+ inProgress = true;
+ hasError = false;
+
+ try
+ {
+ await UsersService.UpdateUser(UserId, input.RoleId, input.Password);
+
+ DialogService.Close(true);
+ }
+ catch (Exception e)
+ {
+ hasError = true;
+ errorMessage = e.Message;
+ }
+ finally
+ {
+ inProgress = false;
+ }
+ }
+
+ void DoClose()
+ {
+ if (inProgress) return;
+
+ DialogService.Close(false);
+ }
+}
diff --git a/Hcs.WebApp/Components/Pages/Management/Users.razor b/Hcs.WebApp/Components/Pages/Management/Users.razor
index 1f35867..bb44bce 100644
--- a/Hcs.WebApp/Components/Pages/Management/Users.razor
+++ b/Hcs.WebApp/Components/Pages/Management/Users.razor
@@ -94,7 +94,25 @@
async Task EditUser(AppUserWithRole userWithRole)
{
- // TODO: Implement method
+ var success = await DialogService.OpenAsync(
+ "Редактирование пользователя",
+ new Dictionary()
+ {
+ { nameof(Dialogs.EditUser.UserId), userWithRole.User.Id },
+ { nameof(Dialogs.EditUser.RoleId), userWithRole.Role.Id }
+ },
+ new DialogOptions()
+ {
+ Width = "420px",
+ ShowClose = false,
+ CloseDialogOnEsc = false,
+ CloseDialogOnOverlayClick = false
+ });
+
+ if (success)
+ {
+ await UpdateGrid();
+ }
}
async Task DeleteUser(AppUserWithRole userWithRole)
diff --git a/Hcs.WebApp/Services/UsersService.cs b/Hcs.WebApp/Services/UsersService.cs
index 70bcf79..5f735c7 100644
--- a/Hcs.WebApp/Services/UsersService.cs
+++ b/Hcs.WebApp/Services/UsersService.cs
@@ -4,10 +4,14 @@ using Microsoft.EntityFrameworkCore;
namespace Hcs.WebApp.Services
{
- public class UsersService(IDbContextFactory factory, UserManager userManager)
+ public class UsersService(
+ IDbContextFactory factory,
+ UserManager userManager,
+ IPasswordHasher passwordHasher)
{
private readonly IDbContextFactory factory = factory;
private readonly UserManager userManager = userManager;
+ private readonly IPasswordHasher passwordHasher = passwordHasher;
public async Task> GetUsersWithRole()
{
@@ -36,5 +40,53 @@ namespace Hcs.WebApp.Services
}
return result;
}
+
+ public async Task UpdateUser(string userId, string roleId, string password)
+ {
+ using var context = factory.CreateDbContext();
+ using var transaction = await context.Database.BeginTransactionAsync();
+ try
+ {
+ var curUserRole = await (from userRole in context.UserRoles
+ where userRole.UserId == userId
+ select userRole).SingleAsync();
+ var curRole = await (from role in context.Roles
+ where role.Id == curUserRole.RoleId
+ select role).SingleAsync();
+ if (curRole.Id != roleId)
+ {
+ context.UserRoles.Remove(curUserRole);
+
+ var newRole = await (from role in context.Roles
+ where role.Id == roleId
+ select role).SingleAsync();
+ await context.UserRoles.AddAsync(new IdentityUserRole()
+ {
+ UserId = userId,
+ RoleId = newRole.Id
+ });
+ }
+
+ var curUser = await (from user in context.Users
+ where user.Id == userId
+ select user).SingleAsync();
+ var newPasswordHash = passwordHasher.HashPassword(curUser, password);
+ if (curUser.PasswordHash != newPasswordHash)
+ {
+ curUser.PasswordHash = newPasswordHash;
+
+ context.Users.Update(curUser);
+ }
+
+ await context.SaveChangesAsync();
+ }
+ catch (Exception)
+ {
+ await transaction.RollbackAsync();
+
+ throw;
+ }
+ await transaction.CommitAsync();
+ }
}
}