Files
Heimdall/src/Heimdall.Web/Components/Pages/AuditLogsPage.razor
T

132 lines
4.9 KiB
Plaintext
Raw Normal View History

@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 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>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 bool _error;
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;
_error = false;
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)
{
_error = true;
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
};
}