Skip to content

Commit

Permalink
LogsModel trait
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 16, 2024
1 parent bb6c632 commit dbad88e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
6 changes: 1 addition & 5 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Activitylog\Models\Activity as ModelsActivity;

class Activity extends ModelsActivity
{
use HasFactory;
}
class Activity extends ModelsActivity {}
2 changes: 2 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Models;

use App\Enums\Role as EnumsRole;
use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Spatie\Permission\Models\Role as ModelsRole;

class Role extends ModelsRole
{
use HasFactory;
use LogsModel;

public function isSuperAdmin(): bool
{
Expand Down
3 changes: 2 additions & 1 deletion app/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace App\Models;

use App\Models\Traits\LogsModel;
use Illuminate\Database\Eloquent\Relations\MorphPivot;

class RoleUser extends MorphPivot
{
//
use LogsModel;
}
38 changes: 38 additions & 0 deletions app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models\Traits;

use App\Support\LogMasksAttributes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

trait LogsModel
{
use LogsActivity;

protected static array $maskedAttributes = ['password'];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logAll()
->logExcept(
collect($this->logExcept())
->add($this->getKeyName())
->when($this->usesTimestamps(), fn($collection) => $collection->add($this->getCreatedAtColumn())->add($this->getUpdatedAtColumn()))
->toArray()
)
->logOnlyDirty()
->dontSubmitEmptyLogs();
}

public function logExcept(): array
{
return [];
}

public static function bootLogsModel(): void
{
static::addLogChange(new LogMasksAttributes(static::$maskedAttributes));
}
}
7 changes: 7 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Models\Traits\HasSuperAdmin;
use App\Models\Traits\LogsModel;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -21,6 +22,7 @@ class User extends Authenticatable implements FilamentUser
roles as protected originalRoles;
}
use HasSuperAdmin;
use LogsModel;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -66,6 +68,11 @@ public function canAccessPanel(Panel $panel): bool
return true;
}

public function logExcept(): array
{
return ['remember_token'];
}

/**
* The roles that belong to the user.
*
Expand Down
27 changes: 27 additions & 0 deletions app/Support/LogMasksAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Support;

use Spatie\Activitylog\Contracts\LoggablePipe;
use Spatie\Activitylog\EventLogBag;

class LogMasksAttributes implements LoggablePipe
{
public function __construct(public array $attributes) {}

public function handle(EventLogBag $event, \Closure $next): EventLogBag
{
collect($this->attributes)
->each(function (string $attribute) use ($event) {
if (data_get($event->changes, "attributes.{$attribute}")) {
data_set($event->changes, "attributes.{$attribute}", '********');
}

if (data_get($event->changes, "old.{$attribute}")) {
data_set($event->changes, "old.{$attribute}", '********');
}
});

return $next($event);
}
}

0 comments on commit dbad88e

Please sign in to comment.