From d30bf29b13db73c3baaa925cb5242f3b7e7deac6 Mon Sep 17 00:00:00 2001 From: Masroor Ehsan Date: Mon, 30 Dec 2024 22:13:25 +0600 Subject: [PATCH] wip autit --- app/Services/AuditTrail/Activity.php | 29 +++++ app/Services/AuditTrail/ActivityLogger.php | 102 ++++++++++++++++++ app/Services/AuditTrail/Category.php | 8 ++ app/helpers.php | 9 ++ database/factories/UserFactory.php | 1 + .../0001_01_01_000000_create_users_table.php | 1 + ...4_12_30_152300_create_audit_logs_table.php | 30 ++++++ 7 files changed, 180 insertions(+) create mode 100644 app/Services/AuditTrail/Activity.php create mode 100644 app/Services/AuditTrail/ActivityLogger.php create mode 100644 app/Services/AuditTrail/Category.php create mode 100644 database/migrations/2024_12_30_152300_create_audit_logs_table.php diff --git a/app/Services/AuditTrail/Activity.php b/app/Services/AuditTrail/Activity.php new file mode 100644 index 0000000..2141a0c --- /dev/null +++ b/app/Services/AuditTrail/Activity.php @@ -0,0 +1,29 @@ +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(), + ]); + } +} diff --git a/app/Services/AuditTrail/Category.php b/app/Services/AuditTrail/Category.php new file mode 100644 index 0000000..9e50c4a --- /dev/null +++ b/app/Services/AuditTrail/Category.php @@ -0,0 +1,8 @@ + 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'), diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php index ff26ead..b20e390 100644 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ b/database/migrations/0001_01_01_000000_create_users_table.php @@ -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(); }); diff --git a/database/migrations/2024_12_30_152300_create_audit_logs_table.php b/database/migrations/2024_12_30_152300_create_audit_logs_table.php new file mode 100644 index 0000000..30ffca1 --- /dev/null +++ b/database/migrations/2024_12_30_152300_create_audit_logs_table.php @@ -0,0 +1,30 @@ +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'); + } +};