From dcb20be9bcd74c40fb14d6001b572e4d5d7e2a14 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 6 Oct 2026 20:57:17 +0000 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Data/Repositories/AuditRepository.cs | 12 +++++++++--- src/Titano.Server/Api/AdminEndpoints.cs | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Titano.Data/Repositories/AuditRepository.cs b/src/Titano.Data/Repositories/AuditRepository.cs index d2199979..1330b21e 100644 --- a/src/Titano.Data/Repositories/AuditRepository.cs +++ b/src/Titano.Data/Repositories/AuditRepository.cs @@ -20,13 +20,19 @@ public sealed class AuditRepository public Task> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) => ListAsync(userId, action: null, limit, cursor, cancellationToken); - /// Entries newest first, optionally of one user and one action (e.g. the sign-in log). - public Task> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) + /// Entries newest first, optionally of one user and one action. + public Task> ListAsync(Guid? userId, string? action, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) => + ListByActionsAsync(userId, action is null ? [] : [action], limit, cursor, cancellationToken); + + /// Entries newest first, optionally of one user, of any of the actions (none: all), e.g. the sign-in log with failed attempts. + public Task> ListByActionsAsync(Guid? userId, IReadOnlyCollection actions, int limit = 100, string? cursor = null, + CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(actions); var p = new SqlParameters(); var where = new List(); 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); diff --git a/src/Titano.Server/Api/AdminEndpoints.cs b/src/Titano.Server/Api/AdminEndpoints.cs index 45826700..cf9447cc 100644 --- a/src/Titano.Server/Api/AdminEndpoints.cs +++ b/src/Titano.Server/Api/AdminEndpoints.cs @@ -81,10 +81,11 @@ public static class AdminEndpoints return Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto); } + /// The audit log, newest first; action may list several actions separated by commas (login,login_failed). private static async Task 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([.. page.Items.Select(Dto.Audit)], page.NextCursor, null), ProtocolJson.Default.PageAuditEntryDto); }