Skip to content

Commit

Permalink
fix: prevent hidden attributes from being logged (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Z3d0X authored Jul 8, 2023
1 parent e7bc5bc commit 4d08270
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Loggers/AbstractModelLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,27 @@ protected function getModelName(Model $model)
protected function activityLogger(string $logName = null): ActivityLogger
{
$defaultLogName = $this->getLogName();

$logStatus = app(ActivityLogStatus::class);

return app(ActivityLogger::class)
->useLog($logName ?? $defaultLogName)
->setLogStatus($logStatus);
}

protected function getLoggableAttributes(Model $model, array $values = []): array
{
if (count($model->getVisible()) > 0) {
$values = array_intersect_key($values, array_flip($model->getVisible()));
}

if (count($model->getHidden()) > 0) {
$values = array_diff_key($values, array_flip($model->getHidden()));
}

return $values;
}

protected function log(Model $model, string $event, ?string $description = null, mixed $attributes = null)
{
if(is_null($description)) {
Expand All @@ -53,15 +66,15 @@ protected function log(Model $model, string $event, ?string $description = null,
$this->activityLogger()
->event($event)
->performedOn($model)
->withProperties($attributes)
->withProperties($this->getLoggableAttributes($model, $attributes))
->log($description);
}

public function created(Model $model)
{
$this->log($model, 'Created', attributes:$model->getAttributes());
}

public function updated(Model $model)
{
$changes = $model->getChanges();
Expand All @@ -70,7 +83,7 @@ public function updated(Model $model)
if (count($changes) === 1 && array_key_exists('remember_token', $changes)) {
return;
}

$this->log($model, 'Updated', attributes:$changes);
}

Expand Down

0 comments on commit 4d08270

Please sign in to comment.