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

85 lines
3.0 KiB
Plaintext

@page "/audit"
@attribute [Authorize(Permission.ViewAuditPolicy)]
@using System.Collections.Immutable
@using Phantom.Common.Data.Web.AuditLog
@using Phantom.Common.Data.Web.Users
@using Phantom.Web.Services.Users
@using Phantom.Web.Services.Instances
@inherits PhantomComponent
@inject AuditLogManager AuditLogManager
@inject InstanceManager InstanceManager
@inject UserManager UserManager
<h1>Audit Log</h1>
@if (loadError is {} error) {
<p role="alert">@error</p>
return;
}
<Table TItem="AuditLogItem" Items="logItems">
<HeaderRow>
<Column Class="text-end" MinWidth="200px">Time</Column>
<Column>User</Column>
<Column>Event Type</Column>
<Column>Subject</Column>
<Column Width="100%">Data</Column>
</HeaderRow>
<ItemRow Context="logItem">
<Cell class="text-end">
<TimeWithOffset Time="logItem.UtcTime.ToLocalTime()" />
</Cell>
<Cell>
<p class="fw-semibold">@(logItem.UserName ?? "-")</p>
<small class="font-monospace text-uppercase">@logItem.UserGuid.ToString()</small>
</Cell>
<Cell>
<p>@logItem.EventType.ToNiceString()</p>
</Cell>
<Cell>
<p class="fw-semibold">@(logItem.SubjectId is {} subjectId && GetSubjectName(logItem.SubjectType, subjectId) is {} subjectName ? subjectName : "-")</p>
<small class="font-monospace text-uppercase">@(logItem.SubjectId ?? "-")</small>
</Cell>
<Cell>
<code>@logItem.JsonData</code>
</Cell>
</ItemRow>
<NoItemsRow>
No audit log entries found.
</NoItemsRow>
</Table>
@code {
private ImmutableArray<AuditLogItem>? logItems;
private string? loadError;
private ImmutableDictionary<Guid, string>? userNamesByGuid;
private ImmutableDictionary<Guid, string> instanceNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
protected override async Task OnInitializedAsync() {
var result = await AuditLogManager.GetMostRecentItems(await GetAuthenticatedUser(), 50, CancellationToken);
if (result) {
logItems = result.Value;
userNamesByGuid = (await UserManager.GetAll(CancellationToken)).ToImmutableDictionary(static user => user.Guid, static user => user.Name);
instanceNamesByGuid = InstanceManager.GetAll().Values.ToImmutableDictionary(static instance => instance.InstanceGuid, static instance => instance.Configuration.InstanceName);
}
else {
logItems = ImmutableArray<AuditLogItem>.Empty;
loadError = result.Error switch {
UserActionFailure.NotAuthorized => "You do not have permission to view the audit log.",
_ => "Unknown error."
};
}
}
private string? GetSubjectName(AuditLogSubjectType type, string id) {
return type switch {
AuditLogSubjectType.Instance => instanceNamesByGuid.TryGetValue(Guid.Parse(id), out var name) ? name : null,
AuditLogSubjectType.User => userNamesByGuid != null && userNamesByGuid.TryGetValue(Guid.Parse(id), out var name) ? name : null,
_ => null
};
}
}