Skip to content

Commit

Permalink
fix: log description
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 17, 2024
1 parent 4d08590 commit 57f5092
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 68 deletions.
15 changes: 10 additions & 5 deletions app/Filament/Resources/ActivityResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Support\Enums\FontFamily;
use Filament\Tables;
use Filament\Tables\Columns\Layout\Stack;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class ActivityResource extends Resource
Expand All @@ -33,11 +35,14 @@ public static function table(Table $table): Table
->heading(__('Activity Logs'))
->description(__('A log of all activity in the system.'))
->columns([
Stack::make([
Tables\Columns\TextColumn::make('description_formatted'),
Tables\Columns\Layout\Split::make([
Tables\Columns\TextColumn::make('description')->html(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->color('warning'),
->since()
->dateTimeTooltip()
->badge()
->color('warning')
->grow(false),
]),
])
->filters([
Expand Down
49 changes: 0 additions & 49 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use App\Models\Scopes\OrderByIdDesc;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\HtmlString;
use Spatie\Activitylog\Models\Activity as ModelsActivity;

#[ScopedBy(OrderByIdDesc::class)]
Expand All @@ -24,50 +21,4 @@ public function scopeForSubject(Builder $query, Model $subject): Builder
}
});
}

/**
* Get the activity's description formatted.
*/
protected function descriptionFormatted(): Attribute
{
return Attribute::make(
get: function () {
if ($this->subject instanceof Pivot) {
return new HtmlString(sprintf(
'%s <strong>%s</strong> was <strong>%s</strong> to %s <strong>%s</strong> by %s',
match ($this->subject_type) {
RoleUser::class => 'Role',
PermissionRole::class => 'Permission',
},
match ($this->subject_type) {
RoleUser::class => $this->subject->role->name,
PermissionRole::class => $this->subject->permission->label,
},
match ($this->event) {
'created' => 'attached',
'deleted' => 'detached',
default => $this->event,
},
match ($this->subject_type) {
RoleUser::class => 'User',
PermissionRole::class => 'Role',
},
match ($this->subject_type) {
RoleUser::class => $this->subject->user->name,
PermissionRole::class => $this->subject->role->name,
},
$this->causer?->name ? "<strong>{$this->causer->name}</strong>" : '<em>' . __('System') . '</em>',
));
}

return new HtmlString(sprintf(
'%s <strong>%s</strong> was <strong>%s</strong> by %s',
str(class_basename($this->subject))->headline(),
$this->subject->name,
$this->event,
$this->causer?->name ? "<strong>{$this->causer->name}</strong>" : '<em>' . __('System') . '</em>',
));
},
);
}
}
31 changes: 25 additions & 6 deletions app/Models/PermissionRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class PermissionRole extends Pivot
{
use LogsModel;

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;

/**
* Get the table associated with the model.
*
Expand All @@ -20,12 +27,24 @@ public function getTable()
return config('permission.table_names.role_has_permissions', parent::getTable());
}

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
public function logDescription(string $eventName): string
{
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 BelongsTo<Role>
Expand Down
31 changes: 25 additions & 6 deletions app/Models/RoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class RoleUser extends MorphPivot
{
use LogsModel;

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;

/**
* Get the table associated with the model.
*
Expand All @@ -21,12 +28,24 @@ public function getTable()
return config('permission.table_names.model_has_roles', parent::getTable());
}

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = true;
public function logDescription(string $eventName): string
{
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 BelongsTo<Role>
Expand Down
30 changes: 29 additions & 1 deletion app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

Expand All @@ -22,7 +24,8 @@ public function getActivitylogOptions(): LogOptions
->toArray()
)
->logOnlyDirty()
->dontSubmitEmptyLogs();
->dontSubmitEmptyLogs()
->setDescriptionForEvent(fn(string $eventName) => $this->logDescription($eventName));
}

public function logExcept(): array
Expand All @@ -39,4 +42,29 @@ public function logIncludes(Builder $query): Builder
{
return $query;
}

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

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

return str($user->name ?? __('system'))
->wrap($user ? '**' : '*')
->inlineMarkdown()
->toHtmlString();
}

public 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(),
'subject.name' => e($this->getLogSubjectName()),
'event' => $eventName,
'causer.name' => $this->getLogCauserName(),
]);
}
}
2 changes: 1 addition & 1 deletion database/seeders/RoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RoleSeeder extends Seeder
public function run(): void
{
Role::updateOrCreate(
['name' => EnumsRole::SUPER_ADMIN],
['name' => EnumsRole::SUPER_ADMIN->value],
['description' => 'Has full access to all system features and settings.']
);
}
Expand Down

0 comments on commit 57f5092

Please sign in to comment.