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/Audit.razor

89 lines
3.0 KiB
Plaintext

@page "/audit"
@attribute [Authorize(Permission.ViewAuditPolicy)]
@using Phantom.Server.Database.Enums
@using Phantom.Server.Services.Audit
@using Phantom.Server.Services.Instances
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Identity
@using System.Collections.Immutable
@implements IDisposable
@inject AuditLog AuditLog
@inject InstanceManager InstanceManager
@inject UserManager<IdentityUser> 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.UserId</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.Data?.RootElement.ToString()</code>
</td>
</tr>
}
</tbody>
</table>
@code {
private CancellationTokenSource? initializationCancellationTokenSource;
private AuditLogItem[] logItems = Array.Empty<AuditLogItem>();
private Dictionary<string, string>? userNamesById;
private ImmutableDictionary<Guid, string> instanceNamesByGuid = ImmutableDictionary<Guid, string>.Empty;
protected override async Task OnInitializedAsync() {
initializationCancellationTokenSource = new CancellationTokenSource();
var cancellationToken = initializationCancellationTokenSource.Token;
try {
logItems = await AuditLog.GetItems(50, cancellationToken);
userNamesById = await UserManager.Users.ToDictionaryAsync(static user => user.Id, static user => user.UserName ?? user.Id, cancellationToken);
instanceNamesByGuid = InstanceManager.GetInstanceNames();
} 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 => userNamesById != null && userNamesById.TryGetValue(id, out var name) ? name : null,
_ => null
};
}
public void Dispose() {
try {
initializationCancellationTokenSource?.Cancel();
} catch (ObjectDisposedException) {}
}
}