-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add logging to account actions in admin
- Loading branch information
Showing
9 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Filament\Helpers\Pages; | ||
|
||
use App\Models\AdminAccessLog; | ||
|
||
trait LogPageAccess | ||
{ | ||
/** | ||
* Returns the name of the action to be logged. | ||
* Designed to be overridden by the class using this trait | ||
* when something other than the default return value is used. | ||
*/ | ||
protected function getLogActionName(): string | ||
{ | ||
return 'View'; | ||
} | ||
|
||
public function mount($record): void | ||
{ | ||
parent::mount($record); | ||
|
||
AdminAccessLog::create([ | ||
'accessor_account_id' => auth()->user()->id, | ||
'loggable_id' => $this->record->id, | ||
'loggable_type' => get_class($this->record), | ||
'action' => $this->getLogActionName(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Filament\Helpers\Pages; | ||
|
||
use App\Models\AdminAccessLog; | ||
|
||
trait LogRelationAccess | ||
{ | ||
/** | ||
* Returns the name of the action to be logged. | ||
* Designed to be overridden by the class using this trait | ||
* when something other than the default return value is used. | ||
*/ | ||
protected function getLogActionName(): string | ||
{ | ||
return 'ViewRelation'; | ||
} | ||
|
||
public function mount(): void | ||
{ | ||
parent::mount(); | ||
|
||
AdminAccessLog::create([ | ||
'accessor_account_id' => auth()->user()->id, | ||
'loggable_id' => $this->ownerRecord->id, | ||
'loggable_type' => get_class($this->ownerRecord), | ||
'action' => $this->getLogActionName(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class AdminAccessLog extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
public function accessor() | ||
{ | ||
return $this->belongsTo(User::class, 'accessor_account_id'); | ||
} | ||
|
||
public function loggable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\AdminAccessLog> | ||
*/ | ||
class AdminAccessLogFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'accessor_account_id' => 1, | ||
'loggable_id' => 1, | ||
'loggable_type' => 'App\Models\User', | ||
'action' => 'View', | ||
]; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
database/migrations/2023_11_15_184316_create_admin_access_logs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('admin_access_logs', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('accessor_account_id'); | ||
$table->morphs('loggable'); | ||
$table->string('action')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('admin_access_logs'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters