diff --git a/app/Models/Activity.php b/app/Models/Activity.php index c41975c..97b2325 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -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 {} diff --git a/app/Models/Role.php b/app/Models/Role.php index b9a628a..3567394 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -3,6 +3,7 @@ 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; @@ -10,6 +11,7 @@ class Role extends ModelsRole { use HasFactory; + use LogsModel; public function isSuperAdmin(): bool { diff --git a/app/Models/RoleUser.php b/app/Models/RoleUser.php index c64ff9e..76c1ec0 100644 --- a/app/Models/RoleUser.php +++ b/app/Models/RoleUser.php @@ -2,9 +2,10 @@ namespace App\Models; +use App\Models\Traits\LogsModel; use Illuminate\Database\Eloquent\Relations\MorphPivot; class RoleUser extends MorphPivot { - // + use LogsModel; } diff --git a/app/Models/Traits/LogsModel.php b/app/Models/Traits/LogsModel.php new file mode 100644 index 0000000..a460a62 --- /dev/null +++ b/app/Models/Traits/LogsModel.php @@ -0,0 +1,38 @@ +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)); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index bf2f59f..0fb8078 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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; @@ -21,6 +22,7 @@ class User extends Authenticatable implements FilamentUser roles as protected originalRoles; } use HasSuperAdmin; + use LogsModel; /** * The attributes that are mass assignable. @@ -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. * diff --git a/app/Support/LogMasksAttributes.php b/app/Support/LogMasksAttributes.php new file mode 100644 index 0000000..945eaaa --- /dev/null +++ b/app/Support/LogMasksAttributes.php @@ -0,0 +1,27 @@ +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); + } +}