Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
35 lines
1 KiB
PHP
35 lines
1 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use App\Models\AuditLog;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Throwable;
|
||
|
||
/**
|
||
* Protokoll für Admin-Aktionen. Schreibt zusätzlich ins Log – falls die
|
||
* Datenbank gerade nicht erreichbar ist (z. B. mitten in einem Update).
|
||
*/
|
||
class Audit
|
||
{
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
*/
|
||
public static function log(string $action, array $data = [], ?int $userId = null): void
|
||
{
|
||
$userId ??= auth()->id();
|
||
|
||
// Bei „Als Person anmelden“: wer tatsächlich gehandelt hat
|
||
if (app()->bound('session') && session()->isStarted() && ($impersonator = session()->get(Impersonation::SESSION))) {
|
||
$data['impersonator_id'] = (int) $impersonator;
|
||
}
|
||
|
||
Log::info("audit: {$action}", [...$data, 'user_id' => $userId]);
|
||
|
||
try {
|
||
AuditLog::query()->create(['user_id' => $userId, 'action' => $action, 'data' => $data]);
|
||
} catch (Throwable $e) {
|
||
Log::warning('Audit-Eintrag konnte nicht gespeichert werden: '.$e->getMessage());
|
||
}
|
||
}
|
||
}
|