200 lines
8.0 KiB
Plaintext
200 lines
8.0 KiB
Plaintext
@using LucideBlazor
|
|
@using Microsoft.AspNetCore.Authorization
|
|
@using Microsoft.AspNetCore.Components.Authorization
|
|
@using Moonlight.Frontend.UI.Admin.Modals
|
|
@using Moonlight.Shared
|
|
@using ShadcnBlazor.Buttons
|
|
@using ShadcnBlazor.DataGrids
|
|
@using ShadcnBlazor.Dropdowns
|
|
@using ShadcnBlazor.Extras.AlertDialogs
|
|
@using ShadcnBlazor.Extras.Toasts
|
|
@using ShadcnBlazor.Tabels
|
|
@using Moonlight.Shared.Http.Requests
|
|
@using Moonlight.Shared.Http.Requests.Users
|
|
@using Moonlight.Shared.Http.Responses
|
|
@using Moonlight.Shared.Http.Responses.Users
|
|
@using ShadcnBlazor.Extras.Dialogs
|
|
|
|
@inject HttpClient HttpClient
|
|
@inject AlertDialogService AlertDialogService
|
|
@inject DialogService DialogService
|
|
@inject ToastService ToastService
|
|
@inject IAuthorizationService AuthorizationService
|
|
|
|
<div class="flex flex-row justify-between mt-5">
|
|
<div class="flex flex-col">
|
|
<h1 class="text-xl font-semibold">Users</h1>
|
|
<div class="text-muted-foreground">
|
|
Manage users registered in your instance
|
|
</div>
|
|
</div>
|
|
<div class="flex flex-row gap-x-1.5">
|
|
<Button @onclick="CreateAsync" disabled="@(!CreateAccess.Succeeded)">
|
|
<PlusIcon/>
|
|
Create
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-3">
|
|
<DataGrid @ref="Grid" TGridItem="UserDto" Loader="LoadAsync" PageSize="10" ClassName="bg-card">
|
|
<PropertyColumn HeadClassName="text-left" CellClassName="text-left" Field="u => u.Id"/>
|
|
<PropertyColumn HeadClassName="text-left" CellClassName="text-left" IsFilterable="true"
|
|
Identifier="@nameof(UserDto.Username)" Field="u => u.Username"/>
|
|
<PropertyColumn HeadClassName="text-left" CellClassName="text-left" IsFilterable="true"
|
|
Identifier="@nameof(UserDto.Email)" Field="u => u.Email"/>
|
|
<TemplateColumn>
|
|
<CellTemplate>
|
|
<TableCell>
|
|
<div class="flex flex-row items-center justify-end me-3">
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger>
|
|
<Slot Context="dropdownSlot">
|
|
<Button Size="ButtonSize.IconSm" Variant="ButtonVariant.Ghost"
|
|
@attributes="dropdownSlot">
|
|
<EllipsisIcon/>
|
|
</Button>
|
|
</Slot>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent SideOffset="2">
|
|
<DropdownMenuItem OnClick="() => LogoutAsync(context)" Disabled="@(!LogoutAccess.Succeeded)">
|
|
Logout
|
|
<DropdownMenuShortcut>
|
|
<LogOutIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem OnClick="() => EditAsync(context)" Disabled="@(!EditAccess.Succeeded)">
|
|
Edit
|
|
<DropdownMenuShortcut>
|
|
<PenIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem OnClick="() => DeleteAsync(context)"
|
|
Variant="DropdownMenuItemVariant.Destructive"
|
|
Disabled="@(!DeleteAccess.Succeeded)">
|
|
Delete
|
|
<DropdownMenuShortcut>
|
|
<TrashIcon/>
|
|
</DropdownMenuShortcut>
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</TableCell>
|
|
</CellTemplate>
|
|
</TemplateColumn>
|
|
</DataGrid>
|
|
</div>
|
|
|
|
@code
|
|
{
|
|
[CascadingParameter] public Task<AuthenticationState> AuthState { get; set; }
|
|
|
|
private DataGrid<UserDto> Grid;
|
|
|
|
private AuthorizationResult LogoutAccess;
|
|
private AuthorizationResult EditAccess;
|
|
private AuthorizationResult DeleteAccess;
|
|
private AuthorizationResult CreateAccess;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
var authState = await AuthState;
|
|
|
|
LogoutAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Users.Logout);
|
|
EditAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Users.Edit);
|
|
DeleteAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Users.Delete);
|
|
CreateAccess = await AuthorizationService.AuthorizeAsync(authState.User, Permissions.Users.Create);
|
|
}
|
|
|
|
private async Task<DataGridResponse<UserDto>> LoadAsync(DataGridRequest<UserDto> request)
|
|
{
|
|
var query = $"?startIndex={request.StartIndex}&length={request.Length}";
|
|
var filterOptions = request.Filters.Count > 0 ? new FilterOptions(request.Filters) : null;
|
|
|
|
var response = await HttpClient.GetFromJsonAsync<PagedData<UserDto>>(
|
|
$"api/admin/users{query}&filterOptions={filterOptions}",
|
|
Constants.SerializerOptions
|
|
);
|
|
|
|
return new DataGridResponse<UserDto>(response!.Data, response.TotalLength);
|
|
}
|
|
|
|
private async Task CreateAsync()
|
|
{
|
|
await DialogService.LaunchAsync<CreateUserDialog>(parameters =>
|
|
{
|
|
parameters[nameof(CreateUserDialog.OnSubmit)] = async (CreateUserDto dto) =>
|
|
{
|
|
await HttpClient.PostAsJsonAsync(
|
|
"/api/admin/users",
|
|
dto,
|
|
Constants.SerializerOptions
|
|
);
|
|
|
|
await ToastService.SuccessAsync(
|
|
"User creation",
|
|
$"Successfully created user {dto.Username}"
|
|
);
|
|
|
|
await Grid.RefreshAsync();
|
|
};
|
|
});
|
|
}
|
|
|
|
private async Task EditAsync(UserDto user)
|
|
{
|
|
await DialogService.LaunchAsync<UpdateUserDialog>(parameters =>
|
|
{
|
|
parameters[nameof(UpdateUserDialog.User)] = user;
|
|
parameters[nameof(CreateUserDialog.OnSubmit)] = async (UpdateUserDto dto) =>
|
|
{
|
|
await HttpClient.PatchAsJsonAsync(
|
|
$"/api/admin/users/{user.Id}",
|
|
dto
|
|
);
|
|
|
|
await ToastService.SuccessAsync(
|
|
"User update",
|
|
$"Successfully updated user {dto.Username}"
|
|
);
|
|
|
|
await Grid.RefreshAsync();
|
|
};
|
|
});
|
|
}
|
|
|
|
private async Task DeleteAsync(UserDto user)
|
|
{
|
|
await AlertDialogService.ConfirmDangerAsync(
|
|
$"Deletion of user {user.Username}",
|
|
"Do you really want to delete this user? This action cannot be undone",
|
|
async () =>
|
|
{
|
|
var response = await HttpClient.DeleteAsync($"api/admin/users/{user.Id}");
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await ToastService.SuccessAsync("User deletion", $"Successfully deleted user {user.Username}");
|
|
|
|
await Grid.RefreshAsync();
|
|
}
|
|
);
|
|
}
|
|
|
|
private async Task LogoutAsync(UserDto user)
|
|
{
|
|
await AlertDialogService.ConfirmDangerAsync(
|
|
$"Logout all session of user {user.Username}",
|
|
"Do you really want to logout all session of this user? This action cannot be undone",
|
|
async () =>
|
|
{
|
|
var response = await HttpClient.PostAsync($"api/admin/users/{user.Id}/logout", null);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await ToastService.SuccessAsync("User logout", $"Successfully logged out all session of user {user.Username}");
|
|
|
|
await Grid.RefreshAsync();
|
|
}
|
|
);
|
|
}
|
|
} |