@page "/management/users" @using System.IdentityModel.Claims @using Hcs.WebApp.Components.Dialogs @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 Пользователи @errorMessage @code { string currentUserId; IEnumerable usersWithRoles; bool isLoading; bool hasError; string errorMessage; protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (firstRender) { isLoading = true; StateHasChanged(); var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); if (state.User.IsInRole(AppRole.ADMINISTRATOR_TYPE)) { currentUserId = state.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; usersWithRoles = await UsersService.GetUsersWithRole(); } isLoading = false; StateHasChanged(); } } async Task AddUser() { var success = await DialogService.OpenAsync( "Создание пользователя", null, new DialogOptions() { Width = "420px", ShowClose = false, CloseDialogOnEsc = false, CloseDialogOnOverlayClick = false }); if (success) { await UpdateGrid(); } } async Task EditUser(AppUserWithRole userWithRole) { var success = await DialogService.OpenAsync( "Редактирование пользователя", new Dictionary() { { nameof(Dialogs.EditUser.CurrentUserId), currentUserId }, { 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) { try { if (await DialogService.Confirm( $"Вы уверены что хотите удалить пользователя '{userWithRole.User.UserName}'?", "Удаление пользователя", new ConfirmOptions() { OkButtonText = "Да", CancelButtonText = "Отмена" }) == true) { isLoading = true; var result = await UsersService.DeleteUser(userWithRole.User.Id); if (result.Succeeded) { await UpdateGrid(); } else { isLoading = false; hasError = true; errorMessage = string.Join(", ", result.Errors.Select(x => x.Description)); } } } catch (Exception e) { isLoading = false; hasError = true; errorMessage = e.Message; } } async Task UpdateGrid() { isLoading = true; hasError = false; usersWithRoles = await UsersService.GetUsersWithRole(); isLoading = false; } }