wip autit
This commit is contained in:
parent
105bea6e15
commit
d30bf29b13
29
app/Services/AuditTrail/Activity.php
Normal file
29
app/Services/AuditTrail/Activity.php
Normal 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;
|
||||
}
|
102
app/Services/AuditTrail/ActivityLogger.php
Normal file
102
app/Services/AuditTrail/ActivityLogger.php
Normal 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(),
|
||||
]);
|
||||
}
|
||||
}
|
8
app/Services/AuditTrail/Category.php
Normal file
8
app/Services/AuditTrail/Category.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\AuditTrail;
|
||||
|
||||
final class Category
|
||||
{
|
||||
public const int GENERAL = 1;
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\AuditTrail\ActivityLogger;
|
||||
|
||||
if (! function_exists('_h')) {
|
||||
function _h(int $key): string
|
||||
{
|
||||
@ -13,3 +15,10 @@ function unhash_it(string $hashid): int
|
||||
return (int) Hashids::decode($hashid)[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('audit')) {
|
||||
function audit(): ActivityLogger
|
||||
{
|
||||
return app(ActivityLogger::class);
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ public function definition(): array
|
||||
'username' => fake()->unique()->userName(),
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'email_verified_at' => now(),
|
||||
'last_seen_at' => fake()->dateTime(),
|
||||
'phone' => fake()->phoneNumber(),
|
||||
'user_role' => static::$role ??= UserRole::Guest->value,
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
|
@ -27,6 +27,7 @@ public function up(): void
|
||||
$table->string('profile_photo_path')->nullable();
|
||||
$table->foreignIdFor(Institute::class)->nullable()->index();
|
||||
$table->string('timezone')->default('Asia/Dhaka');
|
||||
$table->timestamp('last_seen_at')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
|
@ -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');
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user