1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-22 08:42:44 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web/Pages/Users.razor

107 lines
3.3 KiB
Plaintext

@page "/users"
@using System.Collections.Immutable
@using Microsoft.AspNetCore.Identity
@using Phantom.Server.Services.Users
@attribute [Authorize(Permission.ViewUsersPolicy)]
@inject UserManager<IdentityUser> UserManager
@inject PermissionManager PermissionManager
@inject IdentityLookup IdentityLookup
<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 = PermissionManager.CheckPermission(context.User, Permission.EditUsers); }
<table class="table align-middle">
<thead>
<tr>
<Column Width="320px">Identifier</Column>
<Column Width="125px; 40%">Username</Column>
<Column Width="125px; 60%">Roles</Column>
@if (canEdit) {
<Column Width="175px">Actions</Column>
}
</tr>
</thead>
<tbody>
@{ var myUserId = IdentityLookup.GetAuthenticatedUserId(context.User); }
@foreach (var user in allUsers) {
var isMe = myUserId == user.Id;
<tr>
<td>
<code class="text-uppercase">@user.Id</code>
</td>
@if (isMe) {
<td class="fw-semibold">@user.UserName</td>
}
else {
<td>@user.UserName</td>
}
<td>@(userRoles.TryGetValue(user.Id, out var roles) ? roles : "-")</td>
@if (canEdit) {
<td>
@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>
}
</td>
}
</tr>
}
</tbody>
</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 ImmutableArray<IdentityUser> allUsers = ImmutableArray<IdentityUser>.Empty;
private readonly Dictionary<string, string> userRoles = new();
private UserRolesDialog userRolesDialog = null!;
private UserDeleteDialog userDeleteDialog = null!;
protected override void OnInitialized() {
allUsers = UserManager.Users.OrderBy(static user => user.UserName).ToImmutableArray();
}
protected override async Task OnInitializedAsync() {
foreach (var user in allUsers) {
await RefreshUserRoles(user);
}
}
private async Task RefreshUserRoles(IdentityUser user) {
var roles = await UserManager.GetRolesAsync(user);
if (roles.Count > 0) {
userRoles[user.Id] = string.Join(", ", roles);
}
else {
userRoles.Remove(user.Id);
}
}
private void OnUserAdded(IdentityUser user) {
allUsers = allUsers.Add(user);
}
private async Task OnUserRolesChanged(IdentityUser user) {
await RefreshUserRoles(user);
}
private void OnUserDeleted(IdentityUser user) {
allUsers = allUsers.Remove(user);
}
}