Add user editing
This commit is contained in:
@ -9,8 +9,6 @@
|
|||||||
@inject UsersService UsersService
|
@inject UsersService UsersService
|
||||||
@inject DialogService DialogService
|
@inject DialogService DialogService
|
||||||
|
|
||||||
<PageTitle>Создание пользователя</PageTitle>
|
|
||||||
|
|
||||||
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
|
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
|
||||||
<Content>
|
<Content>
|
||||||
<RadzenTemplateForm TItem="InputModel" Data=@Input Submit="@DoAddUser">
|
<RadzenTemplateForm TItem="InputModel" Data=@Input Submit="@DoAddUser">
|
||||||
|
|||||||
117
Hcs.WebApp/Components/Dialogs/EditUser.razor
Normal file
117
Hcs.WebApp/Components/Dialogs/EditUser.razor
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
@using Hcs.WebApp.Services
|
||||||
|
@using Microsoft.AspNetCore.Authorization
|
||||||
|
@using Microsoft.AspNetCore.Identity
|
||||||
|
@using Microsoft.EntityFrameworkCore
|
||||||
|
|
||||||
|
@attribute [Authorize]
|
||||||
|
|
||||||
|
@inject UserManager<AppUser> UserManager;
|
||||||
|
@inject RoleManager<AppRole> RoleManager
|
||||||
|
@inject UsersService UsersService
|
||||||
|
@inject DialogService DialogService
|
||||||
|
|
||||||
|
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
|
||||||
|
<Content>
|
||||||
|
<RadzenTemplateForm TItem="InputModel" Data=@Input Submit="@DoEditUser">
|
||||||
|
<RadzenAlert Visible="@hasError" AlertStyle="AlertStyle.Danger" Variant="Variant.Flat" Shade="Shade.Lighter" AllowClose="false">
|
||||||
|
@errorMessage
|
||||||
|
</RadzenAlert>
|
||||||
|
<RadzenStack Gap="1rem" class="rz-p-sm-4">
|
||||||
|
<RadzenFormField Text="Роль" Variant="Variant.Outlined">
|
||||||
|
<ChildContent>
|
||||||
|
<RadzenDropDown Data="@roles" TextProperty="Name" ValueProperty="Id" @bind-Value="@Input.RoleId" Name="Role" style="width: 100%" />
|
||||||
|
</ChildContent>
|
||||||
|
</RadzenFormField>
|
||||||
|
<RadzenFormField Text="Новый пароль" Variant="Variant.Outlined">
|
||||||
|
<ChildContent>
|
||||||
|
<RadzenPassword Name="Password" @bind-Value=@Input.Password AutoCompleteType="AutoCompleteType.NewPassword" />
|
||||||
|
</ChildContent>
|
||||||
|
<Helper>
|
||||||
|
@if (!string.IsNullOrEmpty(Input.Password))
|
||||||
|
{
|
||||||
|
<RadzenLengthValidator Component="Password" Min="6" Text="Длина поля 'Новый пароль' должна быть не меньше 6" />
|
||||||
|
<RadzenLengthValidator Component="Password" Max="100" Text="Длина поля 'Новый пароль' должна быть не больше 100" />
|
||||||
|
}
|
||||||
|
</Helper>
|
||||||
|
</RadzenFormField>
|
||||||
|
<RadzenFormField Text="Повторите новый пароль" Variant="Variant.Outlined">
|
||||||
|
<ChildContent>
|
||||||
|
<RadzenPassword Name="ConfirmPassword" @bind-Value=@Input.ConfirmPassword AutoCompleteType="AutoCompleteType.NewPassword" />
|
||||||
|
</ChildContent>
|
||||||
|
<Helper>
|
||||||
|
<RadzenCompareValidator Value=@Input.Password Component="ConfirmPassword" Text="Пароли должны совпадать" />
|
||||||
|
</Helper>
|
||||||
|
</RadzenFormField>
|
||||||
|
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.End" Gap="0.5rem">
|
||||||
|
<RadzenButton ButtonType="ButtonType.Submit" Disabled="@inProgress" Text="Сохранить"></RadzenButton>
|
||||||
|
<RadzenButton Click="@DoClose" ButtonStyle="ButtonStyle.Light" Disabled="@inProgress" Text="Отмена"></RadzenButton>
|
||||||
|
</RadzenStack>
|
||||||
|
</RadzenStack>
|
||||||
|
</RadzenTemplateForm>
|
||||||
|
</Content>
|
||||||
|
</AuthorizedContent>
|
||||||
|
|
||||||
|
@code {
|
||||||
|
sealed class InputModel
|
||||||
|
{
|
||||||
|
public string RoleId { get; set; }
|
||||||
|
|
||||||
|
public string Password { get; set; } = "";
|
||||||
|
|
||||||
|
public string ConfirmPassword { get; set; } = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<AppRole> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -94,7 +94,25 @@
|
|||||||
|
|
||||||
async Task EditUser(AppUserWithRole userWithRole)
|
async Task EditUser(AppUserWithRole userWithRole)
|
||||||
{
|
{
|
||||||
// TODO: Implement method
|
var success = await DialogService.OpenAsync<EditUser>(
|
||||||
|
"Редактирование пользователя",
|
||||||
|
new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
{ 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)
|
async Task DeleteUser(AppUserWithRole userWithRole)
|
||||||
|
|||||||
@ -4,10 +4,14 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace Hcs.WebApp.Services
|
namespace Hcs.WebApp.Services
|
||||||
{
|
{
|
||||||
public class UsersService(IDbContextFactory<AppIdentityDbContext> factory, UserManager<AppUser> userManager)
|
public class UsersService(
|
||||||
|
IDbContextFactory<AppIdentityDbContext> factory,
|
||||||
|
UserManager<AppUser> userManager,
|
||||||
|
IPasswordHasher<AppUser> passwordHasher)
|
||||||
{
|
{
|
||||||
private readonly IDbContextFactory<AppIdentityDbContext> factory = factory;
|
private readonly IDbContextFactory<AppIdentityDbContext> factory = factory;
|
||||||
private readonly UserManager<AppUser> userManager = userManager;
|
private readonly UserManager<AppUser> userManager = userManager;
|
||||||
|
private readonly IPasswordHasher<AppUser> passwordHasher = passwordHasher;
|
||||||
|
|
||||||
public async Task<IEnumerable<AppUserWithRole>> GetUsersWithRole()
|
public async Task<IEnumerable<AppUserWithRole>> GetUsersWithRole()
|
||||||
{
|
{
|
||||||
@ -36,5 +40,53 @@ namespace Hcs.WebApp.Services
|
|||||||
}
|
}
|
||||||
return result;
|
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<string>()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user