41 lines
1.3 KiB
PHP
41 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace App\Services;
|
|
|
|
use App\Core\Database;
|
|
|
|
final class AuditService
|
|
{
|
|
public static function log(
|
|
string $action,
|
|
?string $tenantId = null,
|
|
?string $userId = null,
|
|
?string $entityType = null,
|
|
?string $entityId = null,
|
|
?array $oldData = null,
|
|
?array $newData = null,
|
|
?array $metadata = null
|
|
): void {
|
|
try {
|
|
$db = Database::getInstance();
|
|
$stmt = $db->prepare("INSERT INTO audit_logs
|
|
(tenant_id, user_id, action, entity_type, entity_id, old_data, new_data, ip_address, user_agent, metadata, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())");
|
|
$stmt->execute([
|
|
$tenantId,
|
|
$userId,
|
|
$action,
|
|
$entityType,
|
|
$entityId,
|
|
$oldData ? json_encode($oldData, JSON_UNESCAPED_UNICODE) : null,
|
|
$newData ? json_encode($newData, JSON_UNESCAPED_UNICODE) : null,
|
|
$_SERVER['REMOTE_ADDR'] ?? null,
|
|
$_SERVER['HTTP_USER_AGENT'] ?? null,
|
|
$metadata ? json_encode($metadata, JSON_UNESCAPED_UNICODE) : null,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
error_log('[Audit] Failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|