36 lines
1.2 KiB
PHP
36 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AuditLog;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class AuditLogService
|
|
{
|
|
public function __construct(private readonly Request $request) {}
|
|
|
|
public function log(
|
|
string $action,
|
|
array $options = []
|
|
): AuditLog {
|
|
$actor = Auth::user();
|
|
|
|
return AuditLog::create([
|
|
'actor_user_id' => $actor?->id ?? $options['actor_user_id'] ?? null,
|
|
'actor_role' => $actor?->role ?? $options['actor_role'] ?? 'system',
|
|
'action' => $action,
|
|
'subject_type' => $options['subject_type'] ?? null,
|
|
'subject_id' => $options['subject_id'] ?? null,
|
|
'target_user_id'=> $options['target_user_id'] ?? null,
|
|
'project_id' => $options['project_id'] ?? null,
|
|
'old_values' => $options['old_values'] ?? null,
|
|
'new_values' => $options['new_values'] ?? null,
|
|
'justification' => $options['justification'] ?? null,
|
|
'ip_address' => $this->request->ip(),
|
|
'user_agent' => $this->request->userAgent(),
|
|
'created_at' => now(),
|
|
]);
|
|
}
|
|
}
|