262 lines
12 KiB
Plaintext
262 lines
12 KiB
Plaintext
@page "/audit-logs"
|
|
@attribute [Authorize(Policy = "Admin.Manage")]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject ISnackbar Snackbar
|
|
@inject IJSRuntime JS
|
|
|
|
<MudText Typo="Typo.h1" Class="mb-6">
|
|
<i class="fas fa-history mr-2"></i>
|
|
Journal d'audit
|
|
</MudText>
|
|
|
|
<form @onsubmit="ApplyFilters" method="post">
|
|
<MudPaper Class="pa-4 mb-4" Elevation="1" Style="border-radius: 8px;">
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="3">
|
|
<MudTextField @bind-Value="_filterSearch" Label="Recherche"
|
|
Variant="Variant.Outlined" Immediate="true" Class="mb-0"
|
|
Placeholder="Nom, action, ressource..." />
|
|
</MudItem>
|
|
<MudItem xs="12" sm="3">
|
|
<MudTextField @bind-Value="_filterResourceType" Label="Type de ressource"
|
|
Variant="Variant.Outlined" Immediate="true" Class="mb-0"
|
|
Placeholder="Ex: User, Site, Camera..." />
|
|
</MudItem>
|
|
<MudItem xs="12" sm="3">
|
|
<MudSelect @bind-Value="_filterAction" Label="Action" Variant="Variant.Outlined"
|
|
Immediate="true" Class="mb-0" Clearable="true">
|
|
<MudSelectItem T="string" Value="@null">Toutes les actions</MudSelectItem>
|
|
<MudSelectItem Value="@("UserCreated")">Création utilisateur</MudSelectItem>
|
|
<MudSelectItem Value="@("UserRolesUpdated")">Rôles modifiés</MudSelectItem>
|
|
<MudSelectItem Value="@("UserPasswordReset")">Mot de passe réinitialisé</MudSelectItem>
|
|
<MudSelectItem Value="@("UserLocked")">Compte verrouillé</MudSelectItem>
|
|
<MudSelectItem Value="@("UserUnlocked")">Compte déverrouillé</MudSelectItem>
|
|
<MudSelectItem Value="@("UserDeleted")">Utilisateur supprimé</MudSelectItem>
|
|
<MudSelectItem Value="@("Site.Created")">Site créé</MudSelectItem>
|
|
<MudSelectItem Value="@("Site.Updated")">Site modifié</MudSelectItem>
|
|
<MudSelectItem Value="@("Site.Deleted")">Site supprimé</MudSelectItem>
|
|
<MudSelectItem Value="@("Camera.Created")">Caméra créée</MudSelectItem>
|
|
<MudSelectItem Value="@("Camera.Updated")">Caméra modifiée</MudSelectItem>
|
|
<MudSelectItem Value="@("Camera.Deleted")">Caméra supprimée</MudSelectItem>
|
|
<MudSelectItem Value="@("Camera.StatusChanged")">Statut caméra changé</MudSelectItem>
|
|
<MudSelectItem Value="@("User.Logout")">Déconnexion</MudSelectItem>
|
|
<MudSelectItem Value="@("Group.Created")">Groupe créé</MudSelectItem>
|
|
<MudSelectItem Value="@("Group.Updated")">Groupe modifié</MudSelectItem>
|
|
</MudSelect>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="1">
|
|
<MudDatePicker @bind-Date="_filterFromDate" Label="Du" Variant="Variant.Outlined"
|
|
Class="mb-0" />
|
|
</MudItem>
|
|
<MudItem xs="12" sm="1">
|
|
<MudDatePicker @bind-Date="_filterToDate" Label="Au" Variant="Variant.Outlined"
|
|
Class="mb-0" />
|
|
</MudItem>
|
|
<MudItem xs="12" sm="1" Class="d-flex align-center">
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" ButtonType="ButtonType.Submit" Class="mr-1">
|
|
<MudIcon Icon="@Icons.Material.Filled.Search" Class="mr-1" Size="Size.Small" />
|
|
Filtrer
|
|
</MudButton>
|
|
</MudItem>
|
|
</MudGrid>
|
|
<MudGrid Class="mt-1">
|
|
<MudItem xs="12" Class="d-flex align-center">
|
|
<MudButton Variant="Variant.Text" OnClick="@ResetFilters">Réinitialiser</MudButton>
|
|
<MudSpacer />
|
|
<MudButton Variant="Variant.Outlined" Color="Color.Secondary" OnClick="@ExportCsv" Class="mr-2"
|
|
Disabled="@(_logs is null || _logs.Count == 0)">
|
|
<MudIcon Icon="@Icons.Material.Filled.Download" Class="mr-1" Size="Size.Small" />
|
|
Exporter CSV
|
|
</MudButton>
|
|
<MudTooltip Text="Rafraîchir">
|
|
<MudIconButton Icon="@Icons.Material.Filled.Refresh" Size="Size.Small" OnClick="@LoadLogs" aria-label="Actualiser les journaux" />
|
|
</MudTooltip>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
</form>
|
|
|
|
@if (_loading)
|
|
{
|
|
<MudPaper Elevation="1" Class="pa-4" Style="border-radius: 8px;">
|
|
<MudSkeleton Width="100%" Height="40px" />
|
|
<MudSkeleton Width="100%" Height="40px" Class="mt-1" />
|
|
<MudSkeleton Width="100%" Height="40px" Class="mt-1" />
|
|
<MudSkeleton Width="100%" Height="40px" Class="mt-1" />
|
|
</MudPaper>
|
|
}
|
|
else if (_error)
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4" Variant="Variant.Filled">
|
|
<MudText>Erreur lors du chargement du journal d'audit.</MudText>
|
|
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadLogs" Class="mt-2">
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
|
|
</MudButton>
|
|
</MudAlert>
|
|
}
|
|
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>Détails</MudTh>
|
|
<MudTh>IP</MudTh>
|
|
</HeaderContent>
|
|
<RowTemplate>
|
|
<MudTd DataLabel="Date">
|
|
<MudTooltip Text="@context.Timestamp.ToString("F")">
|
|
<MudText Typo="Typo.body2">@context.Timestamp.ToString("g")</MudText>
|
|
</MudTooltip>
|
|
</MudTd>
|
|
<MudTd DataLabel="Utilisateur">
|
|
<MudText Typo="Typo.body2">@(context.UserEmail ?? "-")</MudText>
|
|
</MudTd>
|
|
<MudTd DataLabel="Action">
|
|
<MudChip T="string" Size="Size.Small" Color="@GetActionColor(context.Action)">
|
|
@context.Action
|
|
</MudChip>
|
|
</MudTd>
|
|
<MudTd DataLabel="Type">
|
|
<MudText Typo="Typo.body2">@context.ResourceType</MudText>
|
|
</MudTd>
|
|
<MudTd DataLabel="Ressource">
|
|
@if (!string.IsNullOrEmpty(context.ResourceName))
|
|
{
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">@context.ResourceName</MudText>
|
|
}
|
|
else
|
|
{
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">
|
|
@context.ResourceId?[..Math.Min(8, context.ResourceId?.Length ?? 0)]
|
|
</MudText>
|
|
}
|
|
</MudTd>
|
|
<MudTd DataLabel="Detils">
|
|
@if (!string.IsNullOrEmpty(context.OldValue) || !string.IsNullOrEmpty(context.NewValue))
|
|
{
|
|
<div style="max-width: 320px; overflow: hidden; text-overflow: ellipsis;">
|
|
@if (!string.IsNullOrEmpty(context.OldValue))
|
|
{
|
|
<MudText Typo="Typo.caption" Style="color: var(--mud-palette-error); text-decoration: line-through; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block;">
|
|
@context.OldValue
|
|
</MudText>
|
|
}
|
|
@if (!string.IsNullOrEmpty(context.NewValue))
|
|
{
|
|
<MudText Typo="Typo.caption" Style="color: var(--mud-palette-success); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block;">
|
|
@context.NewValue
|
|
</MudText>
|
|
}
|
|
</div>
|
|
}
|
|
</MudTd>
|
|
<MudTd DataLabel="IP">
|
|
<MudText Typo="Typo.body2">@(context.IpAddress ?? "-")</MudText>
|
|
</MudTd>
|
|
</RowTemplate>
|
|
<NoRecordsContent>
|
|
<MudPaper Class="pa-8 text-center" Style="border-radius: 12px;">
|
|
<MudIcon Icon="@Icons.Material.Filled.History" Size="Size.Large" Color="Color.Secondary" />
|
|
<MudText Typo="Typo.h6" Color="Color.Secondary" Class="mt-4">Aucune entrée d'audit</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">
|
|
Les actions d'administration seront journalisees ici.
|
|
</MudText>
|
|
</MudPaper>
|
|
</NoRecordsContent>
|
|
</MudTable>
|
|
|
|
<MudPaper Class="d-flex align-center pa-4 mt-4" Style="border-radius: 8px;">
|
|
<MudText Typo="Typo.body2" Class="mr-4">
|
|
Page @_page sur @_totalPages (@_total entrees)
|
|
</MudText>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" Class="mr-2"
|
|
Disabled="@(_page <= 1)" OnClick="@(() => GoToPage(_page - 1))">
|
|
← Precedent
|
|
</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 bool _error;
|
|
private int _page = 1;
|
|
private int _totalPages = 1;
|
|
private int _total;
|
|
private string? _filterResourceType;
|
|
private string? _filterAction;
|
|
private string? _filterSearch;
|
|
private DateTime? _filterFromDate;
|
|
private DateTime? _filterToDate;
|
|
|
|
protected override async Task OnInitializedAsync() => await LoadLogs();
|
|
|
|
private async Task LoadLogs()
|
|
{
|
|
_loading = true;
|
|
_error = false;
|
|
try
|
|
{
|
|
var result = await Api.GetAuditLogsAsync(_filterResourceType, _filterAction, _page,
|
|
search: _filterSearch, fromDate: _filterFromDate, toDate: _filterToDate);
|
|
if (result is not null)
|
|
{
|
|
_logs = result.Items;
|
|
_total = result.TotalCount;
|
|
_totalPages = Math.Max(1, (int)Math.Ceiling(result.TotalCount / (double)result.PageSize));
|
|
}
|
|
}
|
|
catch {
|
|
_error = true;
|
|
Snackbar.Add(UiErrors.OperationFailed(), 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; _filterSearch = null; _filterFromDate = null; _filterToDate = null; _page = 1; await LoadLogs(); }
|
|
|
|
private async Task ExportCsv()
|
|
{
|
|
if (_logs is null || _logs.Count == 0) return;
|
|
|
|
var header = "Date;Utilisateur;Action;Type;Ressource;Nom;Ancien;Nouveau;IP";
|
|
var lines = new List<string> { header };
|
|
foreach (var item in _logs)
|
|
{
|
|
var resourceName = item.ResourceName ?? item.ResourceId?[..Math.Min(8, item.ResourceId?.Length ?? 0)] ?? "";
|
|
var oldVal = EscapeCsv(item.OldValue);
|
|
var newVal = EscapeCsv(item.NewValue);
|
|
lines.Add($"{item.Timestamp:yyyy-MM-dd HH:mm:ss};{EscapeCsv(item.UserEmail ?? "-")};{EscapeCsv(item.Action)};{EscapeCsv(item.ResourceType)};{EscapeCsv(item.ResourceId ?? "")};{EscapeCsv(resourceName)};{oldVal};{newVal};{EscapeCsv(item.IpAddress ?? "-")}");
|
|
}
|
|
|
|
var csv = string.Join("\n", lines);
|
|
var bytes = System.Text.Encoding.UTF8.GetBytes("\uFEFF" + csv);
|
|
var base64 = Convert.ToBase64String(bytes);
|
|
await JS.InvokeVoidAsync("downloadFile", $"audit-logs-{DateTime.Now:yyyyMMdd-HHmmss}.csv", "text/csv;charset=utf-8", base64);
|
|
}
|
|
|
|
private static string EscapeCsv(string? value) => $"\"{(value ?? "").Replace("\"", "\"\"")}\"";
|
|
|
|
private static Color GetActionColor(string action) => action switch
|
|
{
|
|
"Site.Created" or "Camera.Created" or "UserCreated" or "Group.Created" => Color.Success,
|
|
"UserRolesUpdated" or "UserPasswordReset" or "Group.Updated" or "Site.Updated" or "Camera.Updated" => Color.Warning,
|
|
"Site.Deleted" or "Camera.Deleted" or "UserDeleted" => Color.Error,
|
|
"UserLocked" => Color.Error,
|
|
"UserUnlocked" or "Camera.StatusChanged" => Color.Info,
|
|
"User.Logout" => Color.Default,
|
|
_ => Color.Default
|
|
};
|
|
}
|