mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-05-09 14:34:10 +02:00
88 lines
3.1 KiB
Plaintext
88 lines
3.1 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 class="table">
|
|
<thead>
|
|
<tr>
|
|
<Column Width="165px" Class="text-end">Time</Column>
|
|
<Column Width="320px; 20%">User</Column>
|
|
<Column Width="160px">Event Type</Column>
|
|
<Column Width="320px; 20%">Subject</Column>
|
|
<Column Width="100px; 60%">Data</Column>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var logItem in logItems) {
|
|
DateTimeOffset time = logItem.UtcTime.ToLocalTime();
|
|
<tr>
|
|
<td class="text-end">
|
|
<time datetime="@time.ToString("o")">@time.ToString()</time>
|
|
</td>
|
|
<td>
|
|
@(logItem.UserName ?? "-")
|
|
<br>
|
|
<code class="text-uppercase">@logItem.UserGuid</code>
|
|
</td>
|
|
<td>@logItem.EventType.ToNiceString()</td>
|
|
<td>
|
|
@if (logItem.SubjectId is {} subjectId && GetSubjectName(logItem.SubjectType, subjectId) is {} subjectName) {
|
|
@subjectName
|
|
<br>
|
|
}
|
|
<code class="text-uppercase">@(logItem.SubjectId ?? "-")</code>
|
|
</td>
|
|
<td>
|
|
<code>@logItem.JsonData</code>
|
|
</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
|
|
@code {
|
|
|
|
private CancellationTokenSource? initializationCancellationTokenSource;
|
|
private ImmutableArray<AuditLogItem> logItems = ImmutableArray<AuditLogItem>.Empty;
|
|
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.Configuration.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) {}
|
|
}
|
|
|
|
}
|