feat(server): audit log filter accepts several actions

GET /api/v1/audit?action=login,login_failed lists sign-ins with failed
attempts (AuditRepository.ListByActionsAsync).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-06 20:57:17 +00:00
1 parent b0fedf2caf
commit dcb20be9bc
2 files changed
+12 -5

No files matched your search

@@ -20,13 +20,19 @@ public sealed class AuditRepository
public Task<Page<AuditEntry>> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) =>
ListAsync(userId, action: null, limit, cursor, cancellationToken);
/// <summary>Entries newest first, optionally of one user and one action (e.g. the sign-in log).</summary>
public Task<Page<AuditEntry>> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default)
/// <summary>Entries newest first, optionally of one user and one action.</summary>
public Task<Page<AuditEntry>> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) =>
ListByActionsAsync(userId, action is null ? [] : [action], limit, cursor, cancellationToken);
/// <summary>Entries newest first, optionally of one user, of any of the actions (none: all), e.g. the sign-in log with failed attempts.</summary>
public Task<Page<AuditEntry>> ListByActionsAsync(Guid? userId, IReadOnlyCollection<string> actions, int limit = 100, string? cursor = null,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(actions);
var p = new SqlParameters();
var where = new List<string>();
if (userId is { } user) where.Add($"user_id = {p.Add(user)}");
if (action is not null) where.Add($"action = {p.Add(action)}");
if (actions.Count > 0) where.Add($"action IN ({string.Join(", ", actions.Select(a => p.Add(a)))})");
if (cursor is not null)
{
var (value, id) = Cursor.Decode("audit", cursor);
+3 -2
View File
@@ -81,10 +81,11 @@ public static class AdminEndpoints
return Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto);
}
/// <summary>The audit log, newest first; <c>action</c> may list several actions separated by commas (<c>login,login_failed</c>).</summary>
private static async Task<IResult> AuditAsync(HttpContext context, TitanoDatabase db, Guid? user, string? action, string? cursor, int? limit)
{
var page = await db.Audit.ListAsync(user, string.IsNullOrWhiteSpace(action) ? null : action, limit ?? 100,
string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted);
var actions = (action ?? string.Empty).Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var page = await db.Audit.ListByActionsAsync(user, actions, limit ?? 100, string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted);
return Results.Json(new Page<AuditEntryDto>([.. page.Items.Select(Dto.Audit)], page.NextCursor, null), ProtocolJson.Default.PageAuditEntryDto);
}