Add user deletion
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Identity
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using System.IdentityModel.Claims
|
||||
|
||||
@attribute [Authorize]
|
||||
|
||||
@ -29,14 +30,19 @@
|
||||
</RadzenRow>
|
||||
<RadzenRow>
|
||||
<RadzenColumn SizeMD="12">
|
||||
<!--<RadzenAlert AlertStyle="AlertStyle.Danger" Variant="Variant.Flat" Shade="Shade.Lighter" Title="" Visible="@errorVisible">@error</RadzenAlert>-->
|
||||
<RadzenAlert Visible="@hasError" AlertStyle="AlertStyle.Danger" Variant="Variant.Flat" Shade="Shade.Lighter" AllowClose="false">
|
||||
@errorMessage
|
||||
</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" />
|
||||
@if (userWithRole.User.Id != currentUserId)
|
||||
{
|
||||
<RadzenButton Click="@(() => DeleteUser(userWithRole))" @onclick:stopPropagation="true" ButtonStyle="ButtonStyle.Danger" Icon="close" Size="ButtonSize.Small" />
|
||||
}
|
||||
</Template>
|
||||
</RadzenDataGridColumn>
|
||||
</Columns>
|
||||
@ -48,8 +54,11 @@
|
||||
</AuthorizedContent>
|
||||
|
||||
@code {
|
||||
string currentUserId;
|
||||
IEnumerable<AppUserWithRole> usersWithRoles;
|
||||
bool isLoading;
|
||||
bool hasError;
|
||||
string errorMessage;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
@ -64,6 +73,7 @@
|
||||
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
if (state.User.Identity?.IsAuthenticated ?? false)
|
||||
{
|
||||
currentUserId = state.User.FindFirst(ClaimTypes.NameIdentifier).Value;
|
||||
usersWithRoles = await UsersService.GetUsersWithRole();
|
||||
}
|
||||
|
||||
@ -117,12 +127,44 @@
|
||||
|
||||
async Task DeleteUser(AppUserWithRole userWithRole)
|
||||
{
|
||||
// TODO: Implement method
|
||||
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user