Skip to content

Commit

Permalink
ref: log description
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 17, 2024
1 parent 57f5092 commit 53f42b7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 38 deletions.
33 changes: 17 additions & 16 deletions app/Models/PermissionRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ public function getTable()
return config('permission.table_names.role_has_permissions', parent::getTable());
}

public function logDescription(string $eventName): string
protected function getLogFirstSubject(): object
{
return __('Permission <strong>:permission.name</strong> was <strong>:event</strong> :to Role <strong>:role.name</strong> by :causer.name', [
'permission.name' => e($this->permission->label),
'event' => match ($eventName) {
'created' => __('attached'),
'deleted' => __('detached'),
default => $eventName,
},
'to' => match ($eventName) {
'created' => __('to'),
'deleted' => __('from'),
default => __('for'),
},
'role.name' => e($this->role->name),
'causer.name' => $this->getLogCauserName(),
]);
return $this->permission;
}

protected function getLogFirstSubjectName(): string
{
return $this->permission->label;
}

protected function getLogSecondSubject(): object
{
return $this->role;
}

protected function getLogSecondSubjectName(): string
{
return $this->role->name;
}

/**
Expand Down
33 changes: 17 additions & 16 deletions app/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ public function getTable()
return config('permission.table_names.model_has_roles', parent::getTable());
}

public function logDescription(string $eventName): string
protected function getLogFirstSubject(): object
{
return __('Role <strong>:role.name</strong> was <strong>:event</strong> :to User <strong>:user.name</strong> by :causer.name', [
'role.name' => e($this->role->name),
'event' => match ($eventName) {
'created' => __('assigned'),
'deleted' => __('revoked'),
default => $eventName,
},
'to' => match ($eventName) {
'created' => __('to'),
'deleted' => __('from'),
default => __('for'),
},
'user.name' => e($this->user->name),
'causer.name' => $this->getLogCauserName(),
]);
return $this->role;
}

protected function getLogFirstSubjectName(): string
{
return $this->role->name;
}

protected function getLogSecondSubject(): object
{
return $this->user;
}

protected function getLogSecondSubjectName(): string
{
return $this->user->name;
}

/**
Expand Down
53 changes: 47 additions & 6 deletions app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use App\Support\LogMasksAttribute;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\HtmlString;
use Nette\Utils\Html;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

Expand All @@ -25,10 +25,10 @@ public function getActivitylogOptions(): LogOptions
)
->logOnlyDirty()
->dontSubmitEmptyLogs()
->setDescriptionForEvent(fn(string $eventName) => $this->logDescription($eventName));
->setDescriptionForEvent(fn(string $eventName) => $this instanceof Pivot ? $this->logDescriptionPivot($eventName) : $this->logDescription($eventName));
}

public function logExcept(): array
protected function logExcept(): array
{
return [];
}
Expand All @@ -43,12 +43,12 @@ public function logIncludes(Builder $query): Builder
return $query;
}

public function getLogSubjectName(): string
protected function getLogSubjectName(): string
{
return $this->name;
}

public function getLogCauserName(): HtmlString
protected function getLogCauserName(): HtmlString
{
$user = auth('web')->user();

Expand All @@ -58,7 +58,27 @@ public function getLogCauserName(): HtmlString
->toHtmlString();
}

public function logDescription(string $eventName): string
protected function getLogFirstSubject(): object
{
return $this;
}

protected function getLogFirstSubjectName(): string
{
return $this->name;
}

protected function getLogSecondSubject(): object
{
return $this;
}

protected function getLogSecondSubjectName(): string
{
return $this->name;
}

protected function logDescription(string $eventName): string
{
return __(':subject.type <strong>:subject.name</strong> was <strong>:event</strong> by :causer.name', [
'subject.type' => str(class_basename(get_class($this)))->headline(),
Expand All @@ -67,4 +87,25 @@ public function logDescription(string $eventName): string
'causer.name' => $this->getLogCauserName(),
]);
}

protected function logDescriptionPivot(string $eventName): string
{
return __(':subject.first.type <strong>:subject.first.name</strong> was <strong>:event</strong> :to :subject.second.type <strong>:subject.second.name</strong> by :causer.name', [
'subject.first.type' => str(class_basename(get_class($this->getLogFirstSubject())))->headline(),
'subject.first.name' => e($this->getLogFirstSubjectName()),
'event' => match ($eventName) {
'created' => __('attached'),
'deleted' => __('detached'),
default => $eventName,
},
'to' => match ($eventName) {
'created' => __('to'),
'deleted' => __('from'),
default => __('for'),
},
'subject.second.type' => str(class_basename(get_class($this->getLogSecondSubject())))->headline(),
'subject.second.name' => e($this->getLogSecondSubjectName()),
'causer.name' => $this->getLogCauserName(),
]);
}
}

0 comments on commit 53f42b7

Please sign in to comment.