This commit is contained in:
Masroor Ehsan 2024-12-30 22:32:18 +06:00
parent d1f8a2ef53
commit 7f770512cd
3 changed files with 46 additions and 8 deletions

View File

@ -4,6 +4,7 @@
use App\Http\Controllers\HashidControllerBase;
use App\Models\Study;
use App\Services\AuditTrail\Activity;
class StudiesController extends HashidControllerBase
{
@ -20,6 +21,10 @@ public function details()
{
$this->decodeKeys();
$study = Study::with(['series', 'details'])->findOrFail($this->key);
audit()
->did(Activity::Study_Metadata_View)
->on($study)
->log();
//return view('staff.studies.details', compact('study'));
return response()->json($study);

View File

@ -7,11 +7,13 @@ final class Activity
// studies
public const int Study_Open = 101;
public const int Study_Metadata_Edit = 102;
public const int Study_Metadata_View = 102;
public const int Study_History_View = 103;
public const int Study_Metadata_Edit = 103;
public const int Study_History_Update = 104;
public const int Study_History_View = 104;
public const int Study_History_Update = 105;
// report

View File

@ -22,6 +22,13 @@ class ActivityLogger
private ?string $userAgent = null;
private ?string $ipAddr = null;
/**
* @var true
*/
private bool $anonymous = false;
public function __construct()
{
$this->category = Category::GENERAL;
@ -34,10 +41,10 @@ public function on(Study $study): static
return $this;
}
public function by(Authenticatable|int $user): static
public function by(Authenticatable|int|null $user = null): static
{
if ($user == null) {
return $this;
if ($user === null) {
$user = auth()->user();
}
if ($user instanceof Authenticatable) {
@ -84,14 +91,38 @@ public function ua(?string $agent = null): static
return $this;
}
public function log(): bool
public function ip(?string $addr = null): static
{
$this->ipAddr = $addr ?? request()->ip();
return $this;
}
public function anon()
{
$this->anonymous = true;
$this->userId = null;
return $this;
}
public function log(bool $initDefaults = true): bool
{
if ($initDefaults) {
$this->ip();
$this->url();
$this->ua();
if ($this->userId === null && ! $this->anonymous) {
$this->by();
}
}
return DB::table('audit_logs')->insert([
'study_id' => $this->studyId,
'user_id' => $this->userId,
'category' => $this->category,
'activity' => $this->activity,
'ip_addr' => request()->ip(),
'ip_addr' => $this->ipAddr,
'user_agent' => $this->userAgent,
'url' => $this->url,
'notes' => $this->notes,