Add user editing

This commit is contained in:
2025-10-20 16:40:12 +09:00
parent 772344649f
commit 7741b24859
4 changed files with 189 additions and 4 deletions

View File

@ -9,8 +9,6 @@
@inject UsersService UsersService
@inject DialogService DialogService
<PageTitle>Создание пользователя</PageTitle>
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
<Content>
<RadzenTemplateForm TItem="InputModel" Data=@Input Submit="@DoAddUser">

View 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);
}
}