1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-10-17 03:42:50 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web/Pages/Users.razor

116 lines
4.1 KiB
Plaintext

@page "/users"
@attribute [Authorize(Permission.ViewUsersPolicy)]
@using System.Collections.Immutable
@using Phantom.Web.Services.Authentication
@using Phantom.Web.Services.Authorization
@using Phantom.Web.Services.Users
@using Phantom.Common.Data.Web.Users
@inherits Phantom.Web.Components.PhantomComponent
@inject UserManager UserManager
@inject RoleManager RoleManager
@inject UserRoleManager UserRoleManager
<h1>Users</h1>
<PermissionView Permission="Permission.EditUsers">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#add-user">Add User...</button>
</PermissionView>
<AuthorizeView>
<Authorized>
@{ var canEdit = context.CheckPermission(Permission.EditUsers); }
<Table TItem="UserInfo" Items="allUsers">
<HeaderRow>
<Column>Username</Column>
<Column Width="100%">Roles</Column>
@if (canEdit) {
<Column MinWidth="175px">Actions</Column>
}
</HeaderRow>
<ItemRow Context="user">
@{ var isMe = me == user.Guid; }
<Cell>
<p class="fw-semibold">@user.Name</p>
<small class="font-monospace text-uppercase">@user.Guid.ToString()</small>
</Cell>
<Cell>
@(userGuidToRoleDescription.TryGetValue(user.Guid, out var roles) ? roles : "?")
</Cell>
@if (canEdit) {
<Cell>
@if (!isMe) {
<button class="btn btn-primary btn-sm" @onclick="() => userRolesDialog.Show(user)">Edit Roles</button>
<button class="btn btn-danger btn-sm" @onclick="() => userDeleteDialog.Show(user)">Delete...</button>
}
</Cell>
}
</ItemRow>
<NoItemsRow>
No users found.
</NoItemsRow>
</Table>
</Authorized>
</AuthorizeView>
<PermissionView Permission="Permission.EditUsers">
<UserAddDialog ModalId="add-user" UserAdded="OnUserAdded" />
<UserRolesDialog @ref="userRolesDialog" ModalId="manage-user-roles" UserModified="OnUserRolesChanged" />
<UserDeleteDialog @ref="userDeleteDialog" ModalId="delete-user" UserModified="OnUserDeleted" />
</PermissionView>
@code {
private Guid? me = null;
private ImmutableArray<UserInfo>? allUsers;
private ImmutableDictionary<Guid, RoleInfo> allRolesByGuid = ImmutableDictionary<Guid, RoleInfo>.Empty;
private readonly Dictionary<Guid, string> userGuidToRoleDescription = new ();
private ImmutableArray<UserInfo> AllUsers => allUsers.GetValueOrDefault(ImmutableArray<UserInfo>.Empty);
private UserRolesDialog userRolesDialog = null!;
private UserDeleteDialog userDeleteDialog = null!;
protected override async Task OnInitializedAsync() {
me = (await GetAuthenticatedUser())?.Info.Guid;
allUsers = (await UserManager.GetAll(CancellationToken)).Sort(static (a, b) => a.Name.CompareTo(b.Name));
allRolesByGuid = (await RoleManager.GetAll(CancellationToken)).ToImmutableDictionary(static role => role.Guid, static role => role);
var allUserGuids = allUsers
.Value
.Select(static user => user.Guid)
.ToImmutableHashSet();
foreach (var (userGuid, roleGuids) in await UserRoleManager.GetUserRoles(allUserGuids, CancellationToken)) {
userGuidToRoleDescription[userGuid] = StringifyRoles(roleGuids);
}
}
private async Task RefreshUserRoles(UserInfo user) {
userGuidToRoleDescription[user.Guid] = StringifyRoles(await UserRoleManager.GetUserRoles(user.Guid, CancellationToken));
}
private string StringifyRoles(ImmutableArray<Guid> roleGuids) {
return roleGuids.IsEmpty ? "-" : string.Join(", ", roleGuids.Select(StringifyRole));
}
private string StringifyRole(Guid role) {
return allRolesByGuid.TryGetValue(role, out var roleInfo) ? roleInfo.Name : "?";
}
private Task OnUserAdded(UserInfo user) {
allUsers = AllUsers.Add(user);
return RefreshUserRoles(user);
}
private Task OnUserRolesChanged(UserInfo user) {
return RefreshUserRoles(user);
}
private void OnUserDeleted(UserInfo user) {
allUsers = AllUsers.Remove(user);
userGuidToRoleDescription.Remove(user.Guid);
}
}