Add new user creation
This commit is contained in:
@ -1,82 +0,0 @@
|
||||
@page "/account/register"
|
||||
|
||||
@inject NotificationService NotificationService
|
||||
|
||||
<PageTitle>Регистрация аккаунта</PageTitle>
|
||||
|
||||
<RadzenCard class="rz-mx-auto" Style="max-width: 420px">
|
||||
<RadzenTemplateForm TItem="InputModel" Data=@Input Method="post" Action="@($"identity/register?returnUrl={ReturnUrl}")">
|
||||
<RadzenStack Gap="1rem" class="rz-p-sm-12">
|
||||
<RadzenText TextStyle="TextStyle.H5" TextAlign="TextAlign.Center">Регистрация</RadzenText>
|
||||
<RadzenFormField Text="Логин" Variant="Variant.Outlined">
|
||||
<ChildContent>
|
||||
<RadzenTextBox Name="UserName" @bind-Value=@Input.UserName AutoCompleteType="AutoCompleteType.Username" />
|
||||
</ChildContent>
|
||||
<Helper>
|
||||
<RadzenRequiredValidator Component="UserName" Text="Поле 'Логин' обязательно к заполнению" />
|
||||
<RadzenLengthValidator Component="UserName" Min="5" Text="Длина поля 'Логин' должна быть не меньше 5" />
|
||||
<RadzenLengthValidator Component="UserName" Max="30" Text="Длина поля 'Логин' должна быть не больше 30" />
|
||||
</Helper>
|
||||
</RadzenFormField>
|
||||
<RadzenFormField Text="Пароль" Variant="Variant.Outlined">
|
||||
<ChildContent>
|
||||
<RadzenPassword Name="Password" @bind-Value=@Input.Password AutoCompleteType="AutoCompleteType.NewPassword" />
|
||||
</ChildContent>
|
||||
<Helper>
|
||||
<RadzenRequiredValidator Component="Password" Text="Поле 'Пароль' обязательно к заполнению" />
|
||||
<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>
|
||||
<RadzenRequiredValidator Component="ConfirmPassword" Text="Поле 'Повторите пароль' обязательно к заполнению" />
|
||||
<RadzenCompareValidator Value=@Input.Password Component="ConfirmPassword" Text="Пароли должны совпадать" />
|
||||
</Helper>
|
||||
</RadzenFormField>
|
||||
<RadzenButton ButtonType="ButtonType.Submit" Text="Зарегистрировать"></RadzenButton>
|
||||
</RadzenStack>
|
||||
</RadzenTemplateForm>
|
||||
</RadzenCard>
|
||||
|
||||
@code {
|
||||
sealed class InputModel
|
||||
{
|
||||
public string UserName { get; set; } = "";
|
||||
|
||||
public string Password { get; set; } = "";
|
||||
|
||||
public string ConfirmPassword { get; set; } = "";
|
||||
}
|
||||
|
||||
[SupplyParameterFromForm]
|
||||
InputModel Input { get; set; } = new();
|
||||
|
||||
[SupplyParameterFromQuery]
|
||||
string? Error { get; set; }
|
||||
|
||||
[SupplyParameterFromQuery]
|
||||
string? ReturnUrl { get; set; }
|
||||
|
||||
protected override void OnAfterRender(bool firstRender)
|
||||
{
|
||||
base.OnAfterRender(firstRender);
|
||||
|
||||
if (firstRender)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Error))
|
||||
{
|
||||
NotificationService.Notify(new NotificationMessage()
|
||||
{
|
||||
Severity = NotificationSeverity.Error,
|
||||
Summary = "Ошибка",
|
||||
Detail = Error,
|
||||
Duration = -1d
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
132
Hcs.WebApp/Components/Pages/Management/AddUser.razor
Normal file
132
Hcs.WebApp/Components/Pages/Management/AddUser.razor
Normal file
@ -0,0 +1,132 @@
|
||||
@page "/management/add-user"
|
||||
|
||||
@using Hcs.WebApp.Services
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Identity
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
|
||||
@attribute [Authorize]
|
||||
|
||||
@inject RoleManager<AppRole> RoleManager
|
||||
@inject UsersService UsersService
|
||||
@inject DialogService DialogService
|
||||
|
||||
<PageTitle>Создание пользователя</PageTitle>
|
||||
|
||||
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
|
||||
<Content>
|
||||
<RadzenTemplateForm TItem="InputModel" Data=@Input Submit="@DoAddUser">
|
||||
<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>
|
||||
<RadzenTextBox Name="UserName" @bind-Value=@Input.UserName AutoCompleteType="AutoCompleteType.Username" />
|
||||
</ChildContent>
|
||||
<Helper>
|
||||
<RadzenRequiredValidator Component="UserName" Text="Поле 'Логин' обязательно к заполнению" />
|
||||
<RadzenLengthValidator Component="UserName" Min="5" Text="Длина поля 'Логин' должна быть не меньше 5" />
|
||||
<RadzenLengthValidator Component="UserName" Max="30" Text="Длина поля 'Логин' должна быть не больше 30" />
|
||||
</Helper>
|
||||
</RadzenFormField>
|
||||
<RadzenFormField Text="Роль" Variant="Variant.Outlined">
|
||||
<ChildContent>
|
||||
<RadzenDropDown Data="@roles" TextProperty="Name" @bind-Value="@Input.Role" 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>
|
||||
<RadzenRequiredValidator Component="Password" Text="Поле 'Пароль' обязательно к заполнению" />
|
||||
<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>
|
||||
<RadzenRequiredValidator Component="ConfirmPassword" Text="Поле 'Повторите пароль' обязательно к заполнению" />
|
||||
<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 UserName { get; set; } = "";
|
||||
|
||||
public AppRole Role { get; set; }
|
||||
|
||||
public string Password { get; set; } = "";
|
||||
|
||||
public string ConfirmPassword { get; set; } = "";
|
||||
}
|
||||
|
||||
IEnumerable<AppRole> roles;
|
||||
bool inProgress;
|
||||
bool hasError;
|
||||
string errorMessage;
|
||||
|
||||
[SupplyParameterFromForm]
|
||||
InputModel Input { get; set; } = new();
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
|
||||
roles = await RoleManager.Roles.OrderBy(x => x.Priority).ToListAsync();
|
||||
|
||||
Input.Role = roles.First();
|
||||
}
|
||||
|
||||
async Task DoAddUser(InputModel input)
|
||||
{
|
||||
if (inProgress) return;
|
||||
|
||||
inProgress = true;
|
||||
hasError = false;
|
||||
|
||||
try
|
||||
{
|
||||
var result = await UsersService.CreateUser(input.UserName, input.Role.Name, input.Password);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
DialogService.Close(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
hasError = true;
|
||||
errorMessage = string.Join(", ", result.Errors.Select(x => x.Description));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
hasError = true;
|
||||
errorMessage = e.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
inProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
void DoClose()
|
||||
{
|
||||
if (inProgress) return;
|
||||
|
||||
DialogService.Close(false);
|
||||
}
|
||||
}
|
||||
112
Hcs.WebApp/Components/Pages/Management/Users.razor
Normal file
112
Hcs.WebApp/Components/Pages/Management/Users.razor
Normal file
@ -0,0 +1,112 @@
|
||||
@page "/management/users"
|
||||
|
||||
@using Hcs.WebApp.Services
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Identity
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
|
||||
@attribute [Authorize]
|
||||
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@inject UsersService UsersService
|
||||
@inject DialogService DialogService
|
||||
|
||||
<PageTitle>Пользователи</PageTitle>
|
||||
|
||||
<AuthorizedContent Roles="@AppRole.ADMINISTRATOR_TYPE">
|
||||
<Content>
|
||||
<RadzenStack>
|
||||
<RadzenRow AlignItems="AlignItems.Center">
|
||||
<RadzenColumn Size="12" SizeMD="6">
|
||||
<RadzenText Text="Пользователи" TextStyle="TextStyle.H5" class="rz-m-0" />
|
||||
</RadzenColumn>
|
||||
<RadzenColumn Size="12" SizeMD="6">
|
||||
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.End" Gap="0.5rem">
|
||||
<RadzenButton Icon="add_circle_outline" Text="Создать" Click="@AddUser" ButtonStyle="ButtonStyle.Primary" />
|
||||
</RadzenStack>
|
||||
</RadzenColumn>
|
||||
</RadzenRow>
|
||||
<RadzenRow>
|
||||
<RadzenColumn SizeMD="12">
|
||||
<!--<RadzenAlert AlertStyle="AlertStyle.Danger" Variant="Variant.Flat" Shade="Shade.Lighter" Title="" Visible="@errorVisible">@error</RadzenAlert>-->
|
||||
<RadzenDataGrid TItem="AppUserWithRole" Data="@usersWithRoles" RowSelect="@EditUser" IsLoading="@isLoading" AllowFiltering="true" AllowPaging="true" ShowPagingSummary="true" PageSizeOptions=@(new int[] { 5, 10, 20, 30 }) AllowSorting="true">
|
||||
<Columns>
|
||||
<RadzenDataGridColumn TItem="AppUserWithRole" Property="User.UserName" Title="Логин" />
|
||||
<RadzenDataGridColumn TItem="AppUserWithRole" Property="Role.Name" Title="Роль" />
|
||||
<RadzenDataGridColumn TItem="AppUserWithRole" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="70px">
|
||||
<Template Context="userWithRole">
|
||||
<RadzenButton Click="@(() => DeleteUser(userWithRole))" @onclick:stopPropagation="true" ButtonStyle="ButtonStyle.Danger" Icon="close" Size="ButtonSize.Small" />
|
||||
</Template>
|
||||
</RadzenDataGridColumn>
|
||||
</Columns>
|
||||
</RadzenDataGrid>
|
||||
</RadzenColumn>
|
||||
</RadzenRow>
|
||||
</RadzenStack>
|
||||
</Content>
|
||||
</AuthorizedContent>
|
||||
|
||||
@code {
|
||||
IEnumerable<AppUserWithRole> usersWithRoles;
|
||||
bool isLoading;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
|
||||
if (firstRender)
|
||||
{
|
||||
isLoading = true;
|
||||
|
||||
StateHasChanged();
|
||||
|
||||
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (state.User.Identity?.IsAuthenticated ?? false)
|
||||
{
|
||||
usersWithRoles = await UsersService.GetUsersWithRole();
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
async Task AddUser()
|
||||
{
|
||||
var success = await DialogService.OpenAsync<AddUser>(
|
||||
"Создание пользователя",
|
||||
null,
|
||||
new DialogOptions()
|
||||
{
|
||||
Width = "420px",
|
||||
ShowClose = false,
|
||||
CloseDialogOnEsc = false,
|
||||
CloseDialogOnOverlayClick = false
|
||||
});
|
||||
|
||||
if (success)
|
||||
{
|
||||
await UpdateGrid();
|
||||
}
|
||||
}
|
||||
|
||||
async Task EditUser(AppUserWithRole userWithRole)
|
||||
{
|
||||
// TODO: Implement method
|
||||
}
|
||||
|
||||
async Task DeleteUser(AppUserWithRole userWithRole)
|
||||
{
|
||||
// TODO: Implement method
|
||||
}
|
||||
|
||||
async Task UpdateGrid()
|
||||
{
|
||||
isLoading = true;
|
||||
|
||||
usersWithRoles = await UsersService.GetUsersWithRole();
|
||||
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user