1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-06 15:34:03 +02:00
Minecraft-Phantom-Panel/Web/Phantom.Web/Pages/Audit.razor

82 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 Phantom.Web.Components.PhantomComponent
@inject AuditLogManager AuditLogManager
@inject InstanceManager InstanceManager
@inject UserManager UserManager
<h1>Audit Log</h1>
<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 CancellationTokenSource? initializationCancellationTokenSource;
private ImmutableArray<AuditLogItem>? logItems;
private ImmutableDictionary<Guid, string>? userNamesByGuid;
private ImmutableDictionary<Guid, string> instanceNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
protected override async Task OnInitializedAsync() {
initializationCancellationTokenSource = new CancellationTokenSource();
var cancellationToken = initializationCancellationTokenSource.Token;
try {
logItems = await AuditLogManager.GetMostRecentItems(50, cancellationToken);
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);
} finally {
initializationCancellationTokenSource.Dispose();
}
}
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
};
}
protected override void OnDisposed() {
try {
initializationCancellationTokenSource?.Cancel();
} catch (ObjectDisposedException) {}
}
}