LANCommander/LANCommander.Server/UI/Pages/Settings/Users/Index.razor
2025-05-23 02:17:45 -05:00

189 lines
6.1 KiB
Text

@page "/Settings/Users"
@using LANCommander.Server.UI.Pages.Settings.Users.Components
@inject UserService UserService
@inject ModalService ModalService
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="Users" />
<DataTable
TItem="User"
@ref="Table"
Size="TableSize.Small"
@bind-Total="Total"
Responsive
ColumnPicker
Searchable
SearchProperty="u => u.UserName">
<Columns>
<BoundDataColumn Title="Username" Property="u => u.UserName" Sortable DefaultSortOrder="SortDirection.Ascending" />
<DataColumn TData="string" Title="Roles" Include="UserRoles,UserRoles.Role">
@foreach (var role in context.Roles)
{
<Tag>@role.Name</Tag>
}
</DataColumn>
<DataColumn TData="long" Title="Saves" Include="GameSaves">
<ByteSize Value="@(context?.GameSaves?.Sum(s => s.Size) ?? 0)" />
</DataColumn>
<BoundDataColumn Title="Created On" Property="u => u.CreatedOn" Sortable>
<LocalTime Value="context.CreatedOn" />
</BoundDataColumn>
@if (Settings.Authentication.RequireApproval)
{
<DataColumn TData="bool" Title="Approved">
<Checkbox Checked="context.Approved" />
</DataColumn>
<BoundDataColumn Title="Approved On" Property="u => u.ApprovedOn" Sortable>
<LocalTime Value="context.ApprovedOn" />
</BoundDataColumn>
}
<DataActions TData="string">
<DropdownButton Type="ButtonType.Primary" OnClick="@(() => NavigationManager.NavigateTo($"/Settings/Users/{context.UserName}"))">
<Overlay>
<Menu>
@if (Settings.Authentication.RequireApproval && !context.Approved)
{
<MenuItem OnClick="() => ApproveUser(context)" Type="@ButtonType.Primary">Approve</MenuItem>
}
<MenuItem OnClick="() => ManageRoles(context)">Manage Roles</MenuItem>
<MenuItem OnClick="() => ChangePassword(context)">Change Password</MenuItem>
@if (!context.Roles?.Any(r => r.Name == RoleService.AdministratorRoleName) ?? true)
{
<MenuItem OnClick="() => PromoteUser(context)">Promote</MenuItem>
}
else
{
<MenuItem OnClick="() => DemoteUser(context)">Demote</MenuItem>
}
</Menu>
</Overlay>
<ChildContent>
Edit
</ChildContent>
</DropdownButton>
<Popconfirm Title="Are you sure you want to delete this user?" OnConfirm="() => DeleteUser(context)">
<Button Type="ButtonType.Text" Icon="@IconType.Outline.Close" Danger />
</Popconfirm>
</DataActions>
</Columns>
<PaginationTemplate>
<Pagination
Total="context.Total"
PageSize="context.PageSize"
Current="context.PageIndex"
DefaultPageSize="25"
PageSizeOptions="new [] { 25, 50, 100, 200 }"
ShowSizeChanger
OnChange="context.HandlePageChange" />
</PaginationTemplate>
</DataTable>
@code {
ICollection<UserViewModel> UserList { get; set; } = new List<UserViewModel>();
Settings Settings = SettingService.GetSettings();
bool Loading = true;
DataTable<User> Table;
int Total;
private async Task ApproveUser(User user)
{
var dbUser = await UserService.GetAsync(user.Id);
if (dbUser != null)
{
dbUser.Approved = true;
dbUser.ApprovedOn = DateTime.UtcNow;
await UserService.UpdateAsync(dbUser);
user.Approved = true;
await MessageService.SuccessAsync($"Approved {user.UserName}!");
}
}
private async Task PromoteUser(User user)
{
var dbUser = await UserService.GetAsync(user.Id);
await UserService.AddToRoleAsync(dbUser.UserName, RoleService.AdministratorRoleName);
await MessageService.SuccessAsync($"Promoted {user.UserName}!");
Table.Reload();
}
private async Task DemoteUser(User user)
{
var dbUser = await UserService.GetAsync(user.Id);
if (UserList.SelectMany(u => u.Roles).Count(r => r == RoleService.AdministratorRoleName) == 1)
{
MessageService.Error("Cannot demote the only administrator!");
}
else
{
await UserService.RemoveFromRole(dbUser.UserName, RoleService.AdministratorRoleName);
Table.Reload();
}
}
private async Task DeleteUser(User user)
{
var dbUser = await UserService.GetAsync(user.Id);
if (UserList.SelectMany(u => u.Roles).Count(r => r == RoleService.AdministratorRoleName) == 1 && user.Roles.Any(r => r.Name == RoleService.AdministratorRoleName))
{
MessageService.Error("Cannot delete the only administrator!");
}
else
{
await UserService.DeleteAsync(dbUser);
Table.Reload();
MessageService.Success($"Deleted {user.UserName}!");
}
}
private void ChangePassword(User user)
{
NavigationManager.NavigateTo($"/Settings/Users/{user.Id}/ChangePassword", true);
}
private async Task ManageRoles(User user)
{
var modalOptions = new ModalOptions()
{
Title = "Manage Roles",
Maximizable = false,
DefaultMaximized = false,
Closable = true,
Draggable = true,
OkText = "Update Roles"
};
var modalRef = await ModalService.CreateModalAsync<ManageRolesDialog, Guid, Guid>(modalOptions, user.Id);
modalRef.OnOk = async (Guid) =>
{
Table.Reload();
};
}
}