-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
77 additions
and
6 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
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,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)); | ||
} | ||
} |
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,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); | ||
} | ||
} |