Skip to content

Commit

Permalink
Audit Logs for operations done from SS14.Admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
dffdff2423 committed Sep 2, 2024
1 parent 8a69c3c commit b123b80
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 6 deletions.
84 changes: 84 additions & 0 deletions SS14.Admin/Helpers/AuditHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Text;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.EntityFrameworkCore;

namespace SS14.Admin.Helpers;

public static class AuditHelper
{
public static async Task AddUnsavedLogAsync(ServerDbContext db, AuditLogType ty, LogImpact impact, Guid? author, string message, List<Guid>? effected = null)
{
var dbEffected= effected == null
? []
: effected.Select(id => new AuditLogEffectedPlayer() { EffectedUserId = id }).ToList();
var log = new AuditLog()
{
Type = ty,
Impact = impact,
AuthorUserId = author,
Message = message,
Date = DateTime.UtcNow,
Effected = dbEffected,
};
db.AuditLog.Add(log);
}

public static async Task<string> GetNameFromUidOrDefault(ServerDbContext db, Guid? author)
{
if (author == null)
return "System";

return (await db.Player.FirstOrDefaultAsync(p => p.UserId == author))?.LastSeenUserName ?? "System";
}

// I know this takes 1000 parameters, but this is needed for a consistent format for reachability.
public static async Task UnsavedLogForAddRemarkAsync(ServerDbContext db, NoteType type, int noteId, bool secret, Guid? authorUid, string message, DateTime? expiryTime, Guid? target, NoteSeverity? severity = null)
{
var authorName = await GetNameFromUidOrDefault(db, authorUid);
var sb = new StringBuilder($"{authorName} added");

if (secret && type == NoteType.Note)
{
sb.Append(" secret");
}

sb.Append($" {type} {noteId} with message \"{message}\"");

switch (type)
{
case NoteType.Note:
sb.Append($" with {severity} severity");
break;
case NoteType.Message:
break;
case NoteType.Watchlist:
break;
case NoteType.ServerBan:
break;
case NoteType.RoleBan:
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown note type");
}

if (expiryTime is not null)
{
sb.Append($" which expires on {expiryTime.Value.ToUniversalTime(): yyyy-MM-dd HH:mm:ss} UTC");
}

await AddUnsavedLogAsync(
db, AuditLogUtil.LogTypeForRemark(type), AuditLogUtil.LogImpactForRemark(type, severity),
authorUid, sb.ToString(), target != null ? [target.Value] : []);
}

public static async Task UnsavedLogForRemoveRemakAsync(ServerDbContext db, NoteType type, int noteId, Guid? remover,
Guid? target, NoteSeverity? severity = null)
{
var removerName = await GetNameFromUidOrDefault(db, remover);
var logStr = $"{removerName} has deleted {type} {noteId}";
await AddUnsavedLogAsync(db, AuditLogUtil.LogTypeForRemark(type),
AuditLogUtil.LogImpactForRemark(type, severity),
remover, logStr, target != null ? [target.Value] : []);
}
}
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/AuditLogs/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<td class="text-center colored" style="--cell-color: var(@entry.Impact.CssColorVarName())">@entry.Impact.ToString()</td>
<td>@entry.Date</td>
<td>@entry.Type</td>
<td>@Html.Raw(await Model.RetrievePlayerLink(entry.AuthorUserId))</td>
<td>@Html.Raw(entry.AuthorUserId != null ? await Model.RetrievePlayerLink(entry.AuthorUserId.Value) : "Server")</td>
<td>@entry.Message</td>
<template>
<div class="log-details-header flex-row" style="--cell-color: var(@entry.Impact.CssColorVarName())">
Expand Down
4 changes: 3 additions & 1 deletion SS14.Admin/Pages/Bans/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public async Task<IActionResult> OnPostCreateAsync()
return Page();
}

_dbContext.Ban.Add(ban);
var dbBan = _dbContext.Ban.Add(ban);
await AuditHelper.UnsavedLogForAddRemarkAsync(_dbContext, NoteType.ServerBan, dbBan.Entity.Id, false,
ban.BanningAdmin, ban.Reason, ban.ExpirationTime, ban.PlayerUserId);
await _dbContext.SaveChangesAsync();
TempData["HighlightNewBan"] = ban.Id;
TempData["StatusMessage"] = "Ban created";
Expand Down
4 changes: 3 additions & 1 deletion SS14.Admin/Pages/Bans/CreateMassBan.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -84,7 +85,8 @@ public async Task<IActionResult> OnPostAsync(IFormFile file)
return Page();
}

_dbContext.Ban.Add(ban);
var dbBan = _dbContext.Ban.Add(ban);
await AuditHelper.UnsavedLogForAddRemarkAsync(_dbContext, NoteType.ServerBan, dbBan.Entity.Id, false, ban.BanningAdmin, ban.Reason, ban.ExpirationTime, ban.PlayerUserId);
}

await _dbContext.SaveChangesAsync();
Expand Down
3 changes: 3 additions & 0 deletions SS14.Admin/Pages/Bans/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Server.Database;
using Content.Shared.Database;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -84,6 +85,8 @@ public async Task<IActionResult> OnPostUnbanAsync([FromForm] UnbanModel model)
UnbanTime = DateTime.UtcNow
};

await AuditHelper.UnsavedLogForRemoveRemakAsync(_dbContext, NoteType.ServerBan, ban.Id, ban.Unban.UnbanningAdmin,
ban.PlayerUserId);
await _dbContext.SaveChangesAsync();
TempData.Add("StatusMessage", "Unban done");
return RedirectToPage("./Index");
Expand Down
5 changes: 4 additions & 1 deletion SS14.Admin/Pages/RoleBans/Create.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -78,7 +79,9 @@ public async Task<IActionResult> OnPostCreateAsync()
return Page();
}

_dbContext.RoleBan.Add(roleBan);
var insertedBan = _dbContext.RoleBan.Add(roleBan);
await AuditHelper.UnsavedLogForAddRemarkAsync(_dbContext, NoteType.RoleBan, insertedBan.Entity.Id, false,
roleBan.BanningAdmin, roleBan.Reason, DateTime.Now, roleBan.PlayerUserId);
await _dbContext.SaveChangesAsync();
TempData["HighlightNewBan"] = roleBan.Id;
TempData["StatusMessage"] = "Role ban created";
Expand Down
3 changes: 3 additions & 0 deletions SS14.Admin/Pages/RoleBans/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Server.Database;
using Content.Shared.Database;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand Down Expand Up @@ -84,6 +85,8 @@ public async Task<IActionResult> OnPostUnbanAsync([FromForm] UnbanModel model)
UnbanTime = DateTime.UtcNow
};

await AuditHelper.UnsavedLogForRemoveRemakAsync(_dbContext, NoteType.RoleBan, ban.Id, ban.Unban.UnbanningAdmin,
ban.PlayerUserId);
await _dbContext.SaveChangesAsync();
TempData.Add("StatusMessage", "Unban done");
return RedirectToPage("./Index");
Expand Down
7 changes: 6 additions & 1 deletion SS14.Admin/Pages/Whitelist/AddWhitelist.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
Expand Down Expand Up @@ -60,10 +61,14 @@ public async Task<IActionResult> OnPostAsync()
{
UserId = uid.Value
});
var whitelisterUid = User.Claims.GetUserId();
var playerName = await AuditHelper.GetNameFromUidOrDefault(_dbContext, uid);
await AuditHelper.AddUnsavedLogAsync(_dbContext, AuditLogType.Whitelist, LogImpact.Medium, whitelisterUid,
$"{playerName} was whitelisted", [uid.Value]);
await _dbContext.SaveChangesAsync();

TempData["HighlightNewWhitelist"] = uid;
TempData.SetStatusInformation("Successfully added player to whitelist");
TempData.SetStatusInformation($"Successfully added {playerName} to the whitelist");

return RedirectToPage("./Index");
}
Expand Down
7 changes: 6 additions & 1 deletion SS14.Admin/Pages/Whitelist/RemoveWhitelist.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -38,9 +39,13 @@ public async Task<IActionResult> OnPostAsync(Guid userId)
return NotFound();

_dbContext.Remove(whitelist);
var removedPlayerName = await AuditHelper.GetNameFromUidOrDefault(_dbContext, userId);
await AuditHelper.AddUnsavedLogAsync(_dbContext, AuditLogType.Whitelist, LogImpact.Medium,
User.Claims.GetUserId(),
$"{removedPlayerName} was removed from the whitelist", [userId]);
await _dbContext.SaveChangesAsync();

TempData.SetStatusInformation("Successfully removed whitelist");
TempData.SetStatusInformation($"Successfully removed whitelist from {removedPlayerName}.");
return RedirectToPage("./Index");
}
}

0 comments on commit b123b80

Please sign in to comment.