Add user deletion

This commit is contained in:
2025-10-20 17:10:08 +09:00
parent 7741b24859
commit d7d1b09837
2 changed files with 51 additions and 3 deletions

View File

@ -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">
@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();

View File

@ -88,5 +88,11 @@ namespace Hcs.WebApp.Services
}
await transaction.CommitAsync();
}
public async Task<IdentityResult> DeleteUser(string userId)
{
var user = await userManager.FindByIdAsync(userId);
return await userManager.DeleteAsync(user);
}
}
}