diff --git a/src/Heimdall.Api/Controllers/AuditLogsController.cs b/src/Heimdall.Api/Controllers/AuditLogsController.cs new file mode 100644 index 0000000..609c240 --- /dev/null +++ b/src/Heimdall.Api/Controllers/AuditLogsController.cs @@ -0,0 +1,29 @@ +using Heimdall.Application.DTOs; +using Heimdall.Application.Services; +using Heimdall.Infrastructure.Security; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Heimdall.Api.Controllers; + +[ApiController] +[Route("api/audit-logs")] +[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.AdminManage)] +public class AuditLogsController : ControllerBase +{ + private readonly IAuditLogService _auditLogService; + + public AuditLogsController(IAuditLogService auditLogService) => _auditLogService = auditLogService; + + [HttpGet] + public async Task List( + [FromQuery] string? resourceType, + [FromQuery] string? action, + [FromQuery] int page = 1, + [FromQuery] int pageSize = 20, + CancellationToken ct = default) + { + var result = await _auditLogService.ListAsync(resourceType, action, page, pageSize, ct); + return Ok(result.Data); + } +} diff --git a/src/Heimdall.Application/DTOs/AuditLogDto.cs b/src/Heimdall.Application/DTOs/AuditLogDto.cs new file mode 100644 index 0000000..b16a1bc --- /dev/null +++ b/src/Heimdall.Application/DTOs/AuditLogDto.cs @@ -0,0 +1,13 @@ +namespace Heimdall.Application.DTOs; + +public record AuditLogResponse( + Guid Id, + Guid? UserId, + string? UserEmail, + string Action, + string ResourceType, + string? ResourceId, + string? OldValue, + string? NewValue, + string? IpAddress, + DateTime Timestamp); diff --git a/src/Heimdall.Application/Services/IAuditLogService.cs b/src/Heimdall.Application/Services/IAuditLogService.cs new file mode 100644 index 0000000..e930cb2 --- /dev/null +++ b/src/Heimdall.Application/Services/IAuditLogService.cs @@ -0,0 +1,14 @@ +using Heimdall.Application.Common; +using Heimdall.Application.DTOs; + +namespace Heimdall.Application.Services; + +public interface IAuditLogService +{ + Task>> ListAsync( + string? resourceType = null, + string? action = null, + int page = 1, + int pageSize = 20, + CancellationToken ct = default); +} diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index c6bc24d..4a073b1 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -61,6 +61,7 @@ public static class DependencyInjection services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); return services; } diff --git a/src/Heimdall.Infrastructure/Services/AuditLogService.cs b/src/Heimdall.Infrastructure/Services/AuditLogService.cs new file mode 100644 index 0000000..9765a6d --- /dev/null +++ b/src/Heimdall.Infrastructure/Services/AuditLogService.cs @@ -0,0 +1,51 @@ +using Heimdall.Application.Common; +using Heimdall.Application.DTOs; +using Heimdall.Application.Services; +using Heimdall.Infrastructure.Data; +using Microsoft.EntityFrameworkCore; + +namespace Heimdall.Infrastructure.Services; + +public class AuditLogService : IAuditLogService +{ + private readonly AppDbContext _db; + + public AuditLogService(AppDbContext db) => _db = db; + + public async Task>> ListAsync( + string? resourceType = null, + string? action = null, + int page = 1, + int pageSize = 20, + CancellationToken ct = default) + { + var query = _db.AuditLogs.AsQueryable(); + + if (!string.IsNullOrEmpty(resourceType)) + query = query.Where(al => al.ResourceType == resourceType); + if (!string.IsNullOrEmpty(action)) + query = query.Where(al => al.Action == action); + + var total = await query.CountAsync(ct); + + var items = await query + .OrderByDescending(al => al.Timestamp) + .Skip((page - 1) * pageSize) + .Take(pageSize) + .Select(al => new AuditLogResponse( + al.Id, + al.UserId, + al.UserId != null ? _db.Users.Where(u => u.Id == al.UserId).Select(u => u.Email).FirstOrDefault() : null, + al.Action, + al.ResourceType, + al.ResourceId, + al.OldValue, + al.NewValue, + al.IpAddress, + al.Timestamp)) + .ToListAsync(ct); + + return ServiceResult>.Success( + new PagedResult(items, total, page, pageSize)); + } +} diff --git a/src/Heimdall.Web/Components/Layout/MainLayout.razor b/src/Heimdall.Web/Components/Layout/MainLayout.razor index 6b6c1b2..e095757 100644 --- a/src/Heimdall.Web/Components/Layout/MainLayout.razor +++ b/src/Heimdall.Web/Components/Layout/MainLayout.razor @@ -51,6 +51,9 @@ Utilisateurs + + Journal d'audit + } diff --git a/src/Heimdall.Web/Components/Pages/AuditLogsPage.razor b/src/Heimdall.Web/Components/Pages/AuditLogsPage.razor new file mode 100644 index 0000000..869463d --- /dev/null +++ b/src/Heimdall.Web/Components/Pages/AuditLogsPage.razor @@ -0,0 +1,117 @@ +@page "/audit-logs" +@attribute [Authorize(Roles = "Admin")] +@layout MainLayout +@inject HeimdallApiClient Api +@inject ISnackbar Snackbar + + + + Journal d'audit + + + + + + + + + + + + + + Filtrer + + Réinitialiser + + + + +@if (_loading) +{ + +} +else +{ + + + Date + Utilisateur + Action + Type + Ressource + IP + + + @context.Timestamp.ToString("g") + @(context.UserEmail ?? "-") + + @context.Action + + @context.ResourceType + @context.ResourceId?[..Math.Min(8, context.ResourceId?.Length ?? 0)] + @context.IpAddress + + + Aucune entrée d'audit + + + + + + Page @_page sur @_totalPages (@_total entrées) + + + ← Précédent + + + Suivant → + + +} + +@code { + private IReadOnlyList? _logs; + private bool _loading = true; + private int _page = 1; + private int _totalPages = 1; + private int _total; + private string? _filterResourceType; + private string? _filterAction; + + protected override async Task OnInitializedAsync() => await LoadLogs(); + + private async Task LoadLogs() + { + _loading = true; + try + { + var result = await Api.GetAuditLogsAsync(_filterResourceType, _filterAction, _page); + if (result is not null) + { + _logs = result.Items; + _total = result.TotalCount; + _totalPages = Math.Max(1, (int)Math.Ceiling(result.TotalCount / (double)result.PageSize)); + } + } + catch (Exception ex) { Snackbar.Add($"Erreur : {ex.Message}", Severity.Error); } + finally { _loading = false; } + } + + private async Task GoToPage(int p) { _page = p; await LoadLogs(); } + private async Task ApplyFilters() { _page = 1; await LoadLogs(); } + private async Task ResetFilters() { _filterResourceType = null; _filterAction = null; _page = 1; await LoadLogs(); } + + private static Color GetActionColor(string action) => action switch + { + "Site.Created" or "UserCreated" => Color.Success, + "UserRolesUpdated" or "UserPasswordReset" => Color.Warning, + "UserLocked" => Color.Error, + "UserUnlocked" => Color.Info, + _ => Color.Default + }; +} diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs index 71c28c4..abeea0c 100644 --- a/src/Heimdall.Web/Services/HeimdallApiClient.cs +++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs @@ -178,6 +178,18 @@ public class HeimdallApiClient // ─── Stats ──────────────────────────────────────────────────── + // ─── Audit Logs ────────────────────────────────────────────── + + public async Task?> GetAuditLogsAsync( + string? resourceType = null, string? action = null, int page = 1, int pageSize = 20) + { + var client = CreateClient(); + var query = $"api/audit-logs?page={page}&pageSize={pageSize}"; + if (!string.IsNullOrEmpty(resourceType)) query += $"&resourceType={resourceType}"; + if (!string.IsNullOrEmpty(action)) query += $"&action={action}"; + return await client.GetFromJsonAsync>(query, Json); + } + public async Task GetStatsAsync() { var client = CreateClient(); @@ -222,6 +234,20 @@ public record StatsResult( int OnlineAgents, int ActiveSites); +public class AuditLogItem +{ + public Guid Id { get; set; } + public Guid? UserId { get; set; } + public string? UserEmail { get; set; } + public string Action { get; set; } = ""; + public string ResourceType { get; set; } = ""; + public string? ResourceId { get; set; } + public string? OldValue { get; set; } + public string? NewValue { get; set; } + public string? IpAddress { get; set; } + public DateTime Timestamp { get; set; } +} + public class AgentItem { public Guid Id { get; set; }