feat: add audit log system for tracking admin-significant actions (#2759)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {API_CONFIG} from '../../../core/config/api-config';
|
||||
|
||||
export interface AuditLog {
|
||||
id: number;
|
||||
userId: number | null;
|
||||
username: string;
|
||||
action: string;
|
||||
entityType: string | null;
|
||||
entityId: number | null;
|
||||
description: string;
|
||||
ipAddress: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface PageableResponse<T> {
|
||||
content: T[];
|
||||
page: {
|
||||
totalElements: number;
|
||||
totalPages: number;
|
||||
number: number;
|
||||
size: number;
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuditLogService {
|
||||
private readonly http = inject(HttpClient);
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/audit-logs`;
|
||||
|
||||
getAuditLogs(page: number = 0, size: number = 25, action?: string, username?: string, from?: string, to?: string): Observable<PageableResponse<AuditLog>> {
|
||||
let params = new HttpParams()
|
||||
.set('page', page.toString())
|
||||
.set('size', size.toString());
|
||||
|
||||
if (action) {
|
||||
params = params.set('action', action);
|
||||
}
|
||||
if (username) {
|
||||
params = params.set('username', username);
|
||||
}
|
||||
if (from) {
|
||||
params = params.set('from', from);
|
||||
}
|
||||
if (to) {
|
||||
params = params.set('to', to);
|
||||
}
|
||||
|
||||
return this.http.get<PageableResponse<AuditLog>>(this.url, {params});
|
||||
}
|
||||
|
||||
getDistinctUsernames(): Observable<string[]> {
|
||||
return this.http.get<string[]>(`${this.url}/usernames`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<ng-container *transloco="let t; prefix: 'settingsAuditLogs'">
|
||||
<div class="main-container">
|
||||
<div class="settings-header">
|
||||
<h2 class="settings-title">
|
||||
<i class="pi pi-history"></i>
|
||||
{{ t('title') }}
|
||||
</h2>
|
||||
<p class="settings-description">{{ t('description') }}</p>
|
||||
</div>
|
||||
|
||||
<div class="settings-card">
|
||||
<div class="section-header">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-list"></i>
|
||||
{{ t('sectionTitle') }}
|
||||
</h3>
|
||||
<div class="filter-controls">
|
||||
<p-select
|
||||
[options]="actionOptions"
|
||||
[(ngModel)]="selectedAction"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
[placeholder]="t('filterPlaceholder')"
|
||||
(onChange)="onFilterChange()"
|
||||
class="action-filter">
|
||||
</p-select>
|
||||
<p-select
|
||||
[options]="usernameOptions"
|
||||
[(ngModel)]="selectedUsername"
|
||||
optionLabel="label"
|
||||
optionValue="value"
|
||||
[placeholder]="t('userFilterPlaceholder')"
|
||||
(onChange)="onFilterChange()"
|
||||
class="user-filter">
|
||||
</p-select>
|
||||
<p-datepicker
|
||||
[(ngModel)]="dateRange"
|
||||
(ngModelChange)="onDateRangeChange()"
|
||||
selectionMode="range"
|
||||
[placeholder]="t('dateRangePlaceholder')"
|
||||
dateFormat="yy-mm-dd"
|
||||
[showIcon]="true"
|
||||
[showButtonBar]="true"
|
||||
class="date-range-filter">
|
||||
</p-datepicker>
|
||||
</div>
|
||||
</div>
|
||||
<div class="section-body">
|
||||
<p-table
|
||||
[value]="logs"
|
||||
[lazy]="true"
|
||||
[paginator]="true"
|
||||
[rows]="rows"
|
||||
[totalRecords]="totalRecords"
|
||||
[loading]="loading"
|
||||
[showCurrentPageReport]="true"
|
||||
[currentPageReportTemplate]="t('pageReport')"
|
||||
(onLazyLoad)="onLazyLoad($event)"
|
||||
[rowsPerPageOptions]="[10, 25, 50]"
|
||||
styleClass="p-datatable-sm p-datatable-striped">
|
||||
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th style="width: 175px">{{ t('columns.timestamp') }}</th>
|
||||
<th style="width: 100px">{{ t('columns.user') }}</th>
|
||||
<th style="width: 140px">{{ t('columns.action') }}</th>
|
||||
<th>{{ t('columns.description') }}</th>
|
||||
<th style="width: 130px">{{ t('columns.ip') }}</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="body" let-log>
|
||||
<tr>
|
||||
<td class="timestamp-cell">{{ log.createdAt | date:'medium' }}</td>
|
||||
<td>{{ log.username }}</td>
|
||||
<td>
|
||||
<span class="action-badge" [ngClass]="getActionClass(log.action)">
|
||||
{{ formatAction(log.action) }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="description-cell">{{ log.description }}</td>
|
||||
<td class="ip-cell">{{ log.ipAddress || '—' }}</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="emptymessage">
|
||||
<tr>
|
||||
<td colspan="5" class="empty-message">
|
||||
<i class="pi pi-inbox"></i>
|
||||
<span>{{ t('empty') }}</span>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
@@ -0,0 +1,122 @@
|
||||
@use '../../../shared/styles/settings-shared' as settings;
|
||||
|
||||
.main-container {
|
||||
@include settings.settings-page-container;
|
||||
}
|
||||
|
||||
@include settings.settings-page-header;
|
||||
|
||||
.settings-title {
|
||||
@include settings.settings-page-title;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.settings-description {
|
||||
@include settings.settings-page-description;
|
||||
}
|
||||
|
||||
.settings-card {
|
||||
@include settings.settings-card;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.section-header {
|
||||
@include settings.settings-section-header;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
@include settings.settings-section-title;
|
||||
}
|
||||
|
||||
.section-body {
|
||||
@include settings.settings-section-body;
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
.filter-controls {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
::ng-deep .action-filter {
|
||||
min-width: 180px;
|
||||
}
|
||||
|
||||
::ng-deep .user-filter {
|
||||
min-width: 140px;
|
||||
}
|
||||
|
||||
::ng-deep .date-range-filter {
|
||||
min-width: 220px;
|
||||
}
|
||||
|
||||
::ng-deep .p-datatable-table {
|
||||
table-layout: fixed;
|
||||
}
|
||||
|
||||
.action-badge {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 12px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
white-space: nowrap;
|
||||
|
||||
&.action-danger {
|
||||
background: color-mix(in srgb, var(--p-red-500) 15%, transparent);
|
||||
color: var(--p-red-400);
|
||||
}
|
||||
|
||||
&.action-success {
|
||||
background: color-mix(in srgb, var(--p-green-500) 15%, transparent);
|
||||
color: var(--p-green-400);
|
||||
}
|
||||
|
||||
&.action-info {
|
||||
background: color-mix(in srgb, var(--p-primary-color) 15%, transparent);
|
||||
color: var(--p-primary-color);
|
||||
}
|
||||
|
||||
&.action-warning {
|
||||
background: color-mix(in srgb, var(--p-orange-500) 15%, transparent);
|
||||
color: var(--p-orange-400);
|
||||
}
|
||||
}
|
||||
|
||||
.timestamp-cell {
|
||||
white-space: nowrap;
|
||||
font-size: 0.875rem;
|
||||
color: var(--p-text-muted-color);
|
||||
}
|
||||
|
||||
.description-cell {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.ip-cell {
|
||||
font-family: 'SF Mono', 'Monaco', 'Courier New', monospace;
|
||||
font-size: 0.8rem;
|
||||
color: var(--p-text-muted-color);
|
||||
}
|
||||
|
||||
::ng-deep .p-datatable.p-datatable-sm .p-datatable-tbody > tr > td.empty-message {
|
||||
text-align: center;
|
||||
padding: 3rem 2rem;
|
||||
color: var(--p-text-muted-color);
|
||||
|
||||
.pi {
|
||||
display: block;
|
||||
font-size: 2rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {TableLazyLoadEvent, TableModule} from 'primeng/table';
|
||||
import {Select} from 'primeng/select';
|
||||
import {DatePicker} from 'primeng/datepicker';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {TranslocoDirective} from '@jsverse/transloco';
|
||||
import {AuditLog, AuditLogService} from './audit-log.service';
|
||||
|
||||
interface ActionOption {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
interface UsernameOption {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-audit-logs',
|
||||
standalone: true,
|
||||
imports: [CommonModule, TableModule, Select, DatePicker, FormsModule, TranslocoDirective],
|
||||
templateUrl: './audit-logs.component.html',
|
||||
styleUrl: './audit-logs.component.scss'
|
||||
})
|
||||
export class AuditLogsComponent implements OnInit {
|
||||
private readonly auditLogService = inject(AuditLogService);
|
||||
|
||||
logs: AuditLog[] = [];
|
||||
totalRecords = 0;
|
||||
rows = 25;
|
||||
loading = false;
|
||||
selectedAction: string | null = null;
|
||||
selectedUsername: string | null = null;
|
||||
dateRange: Date[] | null = null;
|
||||
|
||||
usernameOptions: UsernameOption[] = [{label: 'All Users', value: ''}];
|
||||
|
||||
actionOptions: ActionOption[] = [
|
||||
{label: 'All Actions', value: ''},
|
||||
{label: 'Login Success', value: 'LOGIN_SUCCESS'},
|
||||
{label: 'Login Failed', value: 'LOGIN_FAILED'},
|
||||
{label: 'User Created', value: 'USER_CREATED'},
|
||||
{label: 'User Updated', value: 'USER_UPDATED'},
|
||||
{label: 'User Deleted', value: 'USER_DELETED'},
|
||||
{label: 'Password Changed', value: 'PASSWORD_CHANGED'},
|
||||
{label: 'Library Created', value: 'LIBRARY_CREATED'},
|
||||
{label: 'Library Updated', value: 'LIBRARY_UPDATED'},
|
||||
{label: 'Library Deleted', value: 'LIBRARY_DELETED'},
|
||||
{label: 'Library Scanned', value: 'LIBRARY_SCANNED'},
|
||||
{label: 'Book Uploaded', value: 'BOOK_UPLOADED'},
|
||||
{label: 'Book Deleted', value: 'BOOK_DELETED'},
|
||||
{label: 'Permissions Changed', value: 'PERMISSIONS_CHANGED'},
|
||||
{label: 'Metadata Updated', value: 'METADATA_UPDATED'},
|
||||
{label: 'Settings Updated', value: 'SETTINGS_UPDATED'},
|
||||
{label: 'OIDC Config Changed', value: 'OIDC_CONFIG_CHANGED'},
|
||||
{label: 'Task Executed', value: 'TASK_EXECUTED'},
|
||||
{label: 'Book Sent', value: 'BOOK_SENT'},
|
||||
{label: 'Shelf Created', value: 'SHELF_CREATED'},
|
||||
{label: 'Shelf Updated', value: 'SHELF_UPDATED'},
|
||||
{label: 'Shelf Deleted', value: 'SHELF_DELETED'},
|
||||
{label: 'Magic Shelf Created', value: 'MAGIC_SHELF_CREATED'},
|
||||
{label: 'Magic Shelf Updated', value: 'MAGIC_SHELF_UPDATED'},
|
||||
{label: 'Magic Shelf Deleted', value: 'MAGIC_SHELF_DELETED'},
|
||||
{label: 'Email Provider Created', value: 'EMAIL_PROVIDER_CREATED'},
|
||||
{label: 'Email Provider Updated', value: 'EMAIL_PROVIDER_UPDATED'},
|
||||
{label: 'Email Provider Deleted', value: 'EMAIL_PROVIDER_DELETED'},
|
||||
{label: 'OPDS User Created', value: 'OPDS_USER_CREATED'},
|
||||
{label: 'OPDS User Deleted', value: 'OPDS_USER_DELETED'},
|
||||
{label: 'OPDS User Updated', value: 'OPDS_USER_UPDATED'},
|
||||
{label: 'Naming Pattern Changed', value: 'NAMING_PATTERN_CHANGED'},
|
||||
];
|
||||
|
||||
private currentPage = 0;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadUsernames();
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
loadUsernames(): void {
|
||||
this.auditLogService.getDistinctUsernames().subscribe({
|
||||
next: (usernames) => {
|
||||
this.usernameOptions = [
|
||||
{label: 'All Users', value: ''},
|
||||
...usernames.map(u => ({label: u, value: u}))
|
||||
];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
loadLogs(): void {
|
||||
this.loading = true;
|
||||
const action = this.selectedAction || undefined;
|
||||
const username = this.selectedUsername || undefined;
|
||||
const from = this.dateRange?.[0] ? this.formatDateTime(this.dateRange[0]) : undefined;
|
||||
const to = this.dateRange?.[1] ? this.formatDateTime(this.dateRange[1], true) : undefined;
|
||||
this.auditLogService.getAuditLogs(this.currentPage, this.rows, action, username, from, to).subscribe({
|
||||
next: (response) => {
|
||||
this.logs = response.content;
|
||||
this.totalRecords = response.page.totalElements;
|
||||
this.loading = false;
|
||||
},
|
||||
error: () => {
|
||||
this.loading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onLazyLoad(event: TableLazyLoadEvent): void {
|
||||
this.currentPage = (event.first ?? 0) / (event.rows ?? this.rows);
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
onFilterChange(): void {
|
||||
this.currentPage = 0;
|
||||
this.loadLogs();
|
||||
}
|
||||
|
||||
onDateRangeChange(): void {
|
||||
if (!this.dateRange || (this.dateRange[0] && this.dateRange[1])) {
|
||||
this.onFilterChange();
|
||||
}
|
||||
}
|
||||
|
||||
formatAction(action: string): string {
|
||||
return action.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()).replace(/\B\w+/g, c => c.toLowerCase());
|
||||
}
|
||||
|
||||
getActionClass(action: string): string {
|
||||
if (action.includes('FAILED') || action.includes('DELETED')) return 'action-danger';
|
||||
if (action.includes('CREATED') || action.includes('SUCCESS') || action.includes('UPLOADED')) return 'action-success';
|
||||
if (action.includes('OIDC') || action.includes('PERMISSIONS')) return 'action-warning';
|
||||
return 'action-info';
|
||||
}
|
||||
|
||||
private formatDateTime(date: Date, endOfDay = false): string {
|
||||
const d = new Date(date);
|
||||
if (endOfDay) {
|
||||
d.setHours(23, 59, 59);
|
||||
} else {
|
||||
d.setHours(0, 0, 0);
|
||||
}
|
||||
return d.getFullYear() + '-' +
|
||||
String(d.getMonth() + 1).padStart(2, '0') + '-' +
|
||||
String(d.getDate()).padStart(2, '0') + 'T' +
|
||||
String(d.getHours()).padStart(2, '0') + ':' +
|
||||
String(d.getMinutes()).padStart(2, '0') + ':' +
|
||||
String(d.getSeconds()).padStart(2, '0');
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,11 @@
|
||||
<i class="pi pi-list-check"></i> {{ t('tasks') }}
|
||||
</p-tab>
|
||||
}
|
||||
@if (userState.user.permissions.admin) {
|
||||
<p-tab [value]="SettingsTab.AuditLogs">
|
||||
<i class="pi pi-history"></i> {{ t('auditLogs') }}
|
||||
</p-tab>
|
||||
}
|
||||
<p-tab [value]="SettingsTab.OpdsV2">
|
||||
<i class="pi pi-globe"></i> {{ t('opds') }}
|
||||
</p-tab>
|
||||
@@ -116,6 +121,13 @@
|
||||
</ng-template>
|
||||
</p-tabpanel>
|
||||
}
|
||||
@if (userState.user.permissions.admin) {
|
||||
<p-tabpanel [value]="SettingsTab.AuditLogs">
|
||||
<ng-template #content>
|
||||
<app-audit-logs></app-audit-logs>
|
||||
</ng-template>
|
||||
</p-tabpanel>
|
||||
}
|
||||
<p-tabpanel [value]="SettingsTab.OpdsV2">
|
||||
<ng-template #content>
|
||||
<app-opds-settings></app-opds-settings>
|
||||
|
||||
@@ -11,6 +11,7 @@ import {ViewPreferencesParentComponent} from './view-preferences-parent/view-pre
|
||||
import {ReaderPreferences} from './reader-preferences/reader-preferences.component';
|
||||
import {FileNamingPatternComponent} from './file-naming-pattern/file-naming-pattern.component';
|
||||
import {TaskManagementComponent} from './task-management/task-management.component';
|
||||
import {AuditLogsComponent} from './audit-logs/audit-logs.component';
|
||||
import {OpdsSettings} from './opds-settings/opds-settings';
|
||||
import {MetadataSettingsComponent} from './metadata-settings/metadata-settings-component';
|
||||
import {DeviceSettingsComponent} from './device-settings/device-settings-component';
|
||||
@@ -32,6 +33,7 @@ export enum SettingsTab {
|
||||
AuthenticationSettings = 'authentication',
|
||||
OpdsV2 = 'opds',
|
||||
Tasks = 'task',
|
||||
AuditLogs = 'audit-logs',
|
||||
}
|
||||
|
||||
@Component({
|
||||
@@ -54,6 +56,7 @@ export enum SettingsTab {
|
||||
OpdsSettings,
|
||||
LibraryMetadataSettingsComponent,
|
||||
TaskManagementComponent,
|
||||
AuditLogsComponent,
|
||||
EmailV2Component,
|
||||
TranslocoDirective
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user