wip autit

This commit is contained in:
Masroor Ehsan 2024-12-30 22:13:25 +06:00
parent 105bea6e15
commit d30bf29b13
7 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace App\Services\AuditTrail;
final class Activity
{
// studies
public const int Study_Open = 101;
public const int Study_Metadata_Edit = 102;
public const int Study_History_View = 103;
public const int Study_History_Update = 104;
// report
public const int Report_Save = 201;
public const int Report_Delete = 202;
public const int Report_Finalize = 203;
public const int User_Login = 301;
public const int User_Failed_Login = 302;
public const int User_Logout = 303;
}

View File

@ -0,0 +1,102 @@
<?php
namespace App\Services\AuditTrail;
use App\Models\Study;
use DB;
use Illuminate\Contracts\Auth\Authenticatable;
class ActivityLogger
{
private ?int $studyId = null;
private ?int $userId = null;
private int $activity;
private int $category;
private ?string $url = null;
private ?string $notes = null;
private ?string $userAgent = null;
public function __construct()
{
$this->category = Category::GENERAL;
}
public function performedOn(Study $study): static
{
$this->studyId = $study->id;
return $this;
}
public function causedBy(Authenticatable|int $user): static
{
if ($user == null) {
return $this;
}
if ($user instanceof Authenticatable) {
$this->userId = (int) $user->getAuthIdentifier();
} else {
$this->userId = $user;
}
return $this;
}
public function did(int $activity): static
{
$this->activity = $activity;
return $this;
}
public function category(int $category): static
{
$this->category = $category;
return $this;
}
public function url(?string $url = null): static
{
$this->url = $url ?? request()->url();
return $this;
}
public function notes(string $notes): static
{
$this->notes = $notes;
return $this;
}
public function withAgent(): static
{
$this->userAgent = request()->userAgent();
return $this;
}
public function log(): bool
{
return DB::table('audit_logs')->insert([
'study_id' => $this->studyId,
'user_id' => $this->userId,
'category' => $this->category,
'activity' => $this->activity,
'ip_addr' => request()->ip(),
'user_agent' => $this->userAgent,
'url' => $this->url,
'notes' => $this->notes,
'created_at' => now(),
'updated_at' => now(),
]);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Services\AuditTrail;
final class Category
{
public const int GENERAL = 1;
}

View File

@ -1,5 +1,7 @@
<?php <?php
use App\Services\AuditTrail\ActivityLogger;
if (! function_exists('_h')) { if (! function_exists('_h')) {
function _h(int $key): string function _h(int $key): string
{ {
@ -13,3 +15,10 @@ function unhash_it(string $hashid): int
return (int) Hashids::decode($hashid)[0]; return (int) Hashids::decode($hashid)[0];
} }
} }
if (! function_exists('audit')) {
function audit(): ActivityLogger
{
return app(ActivityLogger::class);
}
}

View File

@ -34,6 +34,7 @@ public function definition(): array
'username' => fake()->unique()->userName(), 'username' => fake()->unique()->userName(),
'email' => fake()->unique()->safeEmail(), 'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(), 'email_verified_at' => now(),
'last_seen_at' => fake()->dateTime(),
'phone' => fake()->phoneNumber(), 'phone' => fake()->phoneNumber(),
'user_role' => static::$role ??= UserRole::Guest->value, 'user_role' => static::$role ??= UserRole::Guest->value,
'password' => static::$password ??= Hash::make('password'), 'password' => static::$password ??= Hash::make('password'),

View File

@ -27,6 +27,7 @@ public function up(): void
$table->string('profile_photo_path')->nullable(); $table->string('profile_photo_path')->nullable();
$table->foreignIdFor(Institute::class)->nullable()->index(); $table->foreignIdFor(Institute::class)->nullable()->index();
$table->string('timezone')->default('Asia/Dhaka'); $table->string('timezone')->default('Asia/Dhaka');
$table->timestamp('last_seen_at')->nullable();
$table->timestamps(); $table->timestamps();
}); });

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('audit_logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->nullable();
$table->unsignedBigInteger('study_id')->nullable()->index();
$table->unsignedTinyInteger('category');
$table->unsignedSmallInteger('activity');
$table->ipAddress('ip_addr')->nullable();
$table->string('user_agent')->nullable();
$table->text('url')->nullable();
$table->text('notes')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('audit_logs');
}
};