40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Core\Database;
|
|
|
|
final class AuditService
|
|
{
|
|
public static function log(
|
|
string $action,
|
|
?string $entityType = null,
|
|
?string $entityId = null,
|
|
?array $oldData = null,
|
|
?array $newData = null,
|
|
?array $metadata = null
|
|
): void {
|
|
$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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
|
|
// This would be populated from the global Request context
|
|
$tenantId = $GLOBALS['current_tenant_id'] ?? null;
|
|
$userId = $GLOBALS['current_user_id'] ?? null;
|
|
|
|
$stmt->execute([
|
|
$tenantId,
|
|
$userId,
|
|
$action,
|
|
$entityType,
|
|
$entityId,
|
|
$oldData ? json_encode($oldData) : null,
|
|
$newData ? json_encode($newData) : null,
|
|
$_SERVER['REMOTE_ADDR'] ?? null,
|
|
$_SERVER['HTTP_USER_AGENT'] ?? null,
|
|
$metadata ? json_encode($metadata) : null
|
|
]);
|
|
}
|
|
}
|