mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-21 15:54:06 +02:00
.config
.run
.workdir
Agent
Common
Controller
Phantom.Controller
Phantom.Controller.Database
Phantom.Controller.Database.Postgres
Phantom.Controller.Minecraft
Phantom.Controller.Rpc
Phantom.Controller.Services
Agents
Audit
AuditLog.EventTypes.cs
AuditLog.cs
AuditLogItem.cs
Events
Instances
Rpc
Users
ControllerServices.cs
Phantom.Controller.Services.csproj
Docker
Utils
Web
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json
50 lines
2.1 KiB
C#
50 lines
2.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Phantom.Controller.Database;
|
|
using Phantom.Controller.Database.Entities;
|
|
using Phantom.Controller.Database.Enums;
|
|
using Phantom.Utils.Tasks;
|
|
|
|
namespace Phantom.Controller.Services.Audit;
|
|
|
|
public sealed partial class AuditLog {
|
|
private readonly IDatabaseProvider databaseProvider;
|
|
private readonly TaskManager taskManager;
|
|
private readonly CancellationToken cancellationToken;
|
|
|
|
public AuditLog(IDatabaseProvider databaseProvider, TaskManager taskManager, CancellationToken cancellationToken) {
|
|
this.databaseProvider = databaseProvider;
|
|
this.taskManager = taskManager;
|
|
this.cancellationToken = cancellationToken;
|
|
}
|
|
|
|
private Task<Guid?> GetCurrentAuthenticatedUserId() {
|
|
return Task.FromResult<Guid?>(null); // TODO
|
|
}
|
|
|
|
private async Task AddEntityToDatabase(AuditLogEntity logEntity) {
|
|
await using var ctx = databaseProvider.Provide();
|
|
ctx.AuditLog.Add(logEntity);
|
|
await ctx.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
private void AddItem(Guid? userGuid, AuditLogEventType eventType, string subjectId, Dictionary<string, object?>? extra = null) {
|
|
var logEntity = new AuditLogEntity(userGuid, eventType, subjectId, extra);
|
|
taskManager.Run("Store audit log item to database", () => AddEntityToDatabase(logEntity));
|
|
}
|
|
|
|
private async Task AddItem(AuditLogEventType eventType, string subjectId, Dictionary<string, object?>? extra = null) {
|
|
AddItem(await GetCurrentAuthenticatedUserId(), eventType, subjectId, extra);
|
|
}
|
|
|
|
public async Task<AuditLogItem[]> GetItems(int count, CancellationToken cancellationToken) {
|
|
await using var ctx = databaseProvider.Provide();
|
|
return await ctx.AuditLog
|
|
.Include(static entity => entity.User)
|
|
.AsQueryable()
|
|
.OrderByDescending(static entity => entity.UtcTime)
|
|
.Take(count)
|
|
.Select(static entity => new AuditLogItem(entity.UtcTime, entity.UserGuid, entity.User == null ? null : entity.User.Name, entity.EventType, entity.SubjectType, entity.SubjectId, entity.Data))
|
|
.ToArrayAsync(cancellationToken);
|
|
}
|
|
}
|