feat(audit): journal d'audit consultable dans l'UI
- IAuditLogService + AuditLogService avec pagination et filtres - AuditLogsController: GET /api/audit-logs (resourceType, action, page) - AuditLogsPage Blazor: liste paginée avec filtres + coloration par action - Lien vers le journal dans le menu Admin (visible pour les Admins)
This commit is contained in:
1 parent
cbd00443d4
commit
b816446f72
8 files changed
+254
No files matched your search
@@ -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<IActionResult> 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
@@ -0,0 +1,14 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
|
||||
namespace Heimdall.Application.Services;
|
||||
|
||||
public interface IAuditLogService
|
||||
{
|
||||
Task<ServiceResult<PagedResult<AuditLogResponse>>> ListAsync(
|
||||
string? resourceType = null,
|
||||
string? action = null,
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
CancellationToken ct = default);
|
||||
}
|
||||
@@ -61,6 +61,7 @@ public static class DependencyInjection
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddScoped<ICameraService, CameraService>();
|
||||
services.AddScoped<IStatsService, StatsService>();
|
||||
services.AddScoped<IAuditLogService, AuditLogService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -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<ServiceResult<PagedResult<AuditLogResponse>>> 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<PagedResult<AuditLogResponse>>.Success(
|
||||
new PagedResult<AuditLogResponse>(items, total, page, pageSize));
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,9 @@
|
||||
<MudNavLink Href="/users" Match="NavLinkMatch.Prefix">
|
||||
<MudIcon Icon="@Icons.Material.Filled.People" /> Utilisateurs
|
||||
</MudNavLink>
|
||||
<MudNavLink Href="/audit-logs" Match="NavLinkMatch.Prefix">
|
||||
<MudIcon Icon="@Icons.Material.Filled.History" /> Journal d'audit
|
||||
</MudNavLink>
|
||||
}
|
||||
|
||||
<MudDivider Class="my-4" />
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
@page "/audit-logs"
|
||||
@attribute [Authorize(Roles = "Admin")]
|
||||
@layout MainLayout
|
||||
@inject HeimdallApiClient Api
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudText Typo="Typo.h4" Class="mb-6">
|
||||
<MudIcon Icon="@Icons.Material.Filled.History" Class="mr-2" />
|
||||
Journal d'audit
|
||||
</MudText>
|
||||
|
||||
<MudPaper Class="pa-4 mb-4" Elevation="1" Style="border-radius: 8px;">
|
||||
<MudGrid>
|
||||
<MudItem xs="12" sm="4">
|
||||
<MudTextField @bind-Value="_filterResourceType" Label="Type de ressource"
|
||||
Variant="Variant.Outlined" Immediate="true" Class="mb-0" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4">
|
||||
<MudTextField @bind-Value="_filterAction" Label="Action" Variant="Variant.Outlined"
|
||||
Immediate="true" Class="mb-0" />
|
||||
</MudItem>
|
||||
<MudItem xs="12" sm="4" Class="d-flex align-center">
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@ApplyFilters" Class="mr-2">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Search" Class="mr-1" Size="Size.Small" />
|
||||
Filtrer
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Text" OnClick="@ResetFilters">Réinitialiser</MudButton>
|
||||
</MudItem>
|
||||
</MudGrid>
|
||||
</MudPaper>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<MudProgressCircular Indeterminate="true" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudTable Items="@_logs" Hover="true" Striped="true" Elevation="2" Dense="true">
|
||||
<HeaderContent>
|
||||
<MudTh>Date</MudTh>
|
||||
<MudTh>Utilisateur</MudTh>
|
||||
<MudTh>Action</MudTh>
|
||||
<MudTh>Type</MudTh>
|
||||
<MudTh>Ressource</MudTh>
|
||||
<MudTh>IP</MudTh>
|
||||
</HeaderContent>
|
||||
<RowTemplate>
|
||||
<MudTd DataLabel="Date">@context.Timestamp.ToString("g")</MudTd>
|
||||
<MudTd DataLabel="Utilisateur">@(context.UserEmail ?? "-")</MudTd>
|
||||
<MudTd DataLabel="Action">
|
||||
<MudText Typo="Typo.body2" Color="@GetActionColor(context.Action)">@context.Action</MudText>
|
||||
</MudTd>
|
||||
<MudTd DataLabel="Type">@context.ResourceType</MudTd>
|
||||
<MudTd DataLabel="Ressource">@context.ResourceId?[..Math.Min(8, context.ResourceId?.Length ?? 0)]</MudTd>
|
||||
<MudTd DataLabel="IP">@context.IpAddress</MudTd>
|
||||
</RowTemplate>
|
||||
<NoRecordsContent>
|
||||
<MudText Align="Align.Center" Class="py-4">Aucune entrée d'audit</MudText>
|
||||
</NoRecordsContent>
|
||||
</MudTable>
|
||||
|
||||
<MudPaper Class="d-flex align-center pa-4 mt-4">
|
||||
<MudText Typo="Typo.body2" Class="mr-4">
|
||||
Page @_page sur @_totalPages (@_total entrées)
|
||||
</MudText>
|
||||
<MudButton Variant="Variant.Outlined" Size="Size.Small" Class="mr-2"
|
||||
Disabled="@(_page <= 1)" OnClick="@(() => GoToPage(_page - 1))">
|
||||
← Précédent
|
||||
</MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Size="Size.Small"
|
||||
Disabled="@(_page >= _totalPages)" OnClick="@(() => GoToPage(_page + 1))">
|
||||
Suivant →
|
||||
</MudButton>
|
||||
</MudPaper>
|
||||
}
|
||||
|
||||
@code {
|
||||
private IReadOnlyList<AuditLogItem>? _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
|
||||
};
|
||||
}
|
||||
@@ -178,6 +178,18 @@ public class HeimdallApiClient
|
||||
|
||||
// ─── Stats ────────────────────────────────────────────────────
|
||||
|
||||
// ─── Audit Logs ──────────────────────────────────────────────
|
||||
|
||||
public async Task<PagedData<AuditLogItem>?> 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<PagedData<AuditLogItem>>(query, Json);
|
||||
}
|
||||
|
||||
public async Task<StatsResult?> 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; }
|
||||
|
||||
Reference in new issue
Block a user