From 57f5092c33a2c73a30292d85496c6e65c2f9a893 Mon Sep 17 00:00:00 2001 From: Faizal Nugraha Date: Wed, 18 Sep 2024 01:20:07 +0700 Subject: [PATCH] fix: log description --- app/Filament/Resources/ActivityResource.php | 15 ++++--- app/Models/Activity.php | 49 --------------------- app/Models/PermissionRole.php | 31 ++++++++++--- app/Models/RoleUser.php | 31 ++++++++++--- app/Models/Traits/LogsModel.php | 30 ++++++++++++- database/seeders/RoleSeeder.php | 2 +- 6 files changed, 90 insertions(+), 68 deletions(-) diff --git a/app/Filament/Resources/ActivityResource.php b/app/Filament/Resources/ActivityResource.php index 0abbf1c..46b71a3 100644 --- a/app/Filament/Resources/ActivityResource.php +++ b/app/Filament/Resources/ActivityResource.php @@ -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 @@ -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([ diff --git a/app/Models/Activity.php b/app/Models/Activity.php index aa63865..fec9283 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -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)] @@ -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 %s was %s to %s %s 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 ? "{$this->causer->name}" : '' . __('System') . '', - )); - } - - return new HtmlString(sprintf( - '%s %s was %s by %s', - str(class_basename($this->subject))->headline(), - $this->subject->name, - $this->event, - $this->causer?->name ? "{$this->causer->name}" : '' . __('System') . '', - )); - }, - ); - } } diff --git a/app/Models/PermissionRole.php b/app/Models/PermissionRole.php index 7d7137d..5954358 100644 --- a/app/Models/PermissionRole.php +++ b/app/Models/PermissionRole.php @@ -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. * @@ -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 :permission.name was :event :to Role :role.name 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 diff --git a/app/Models/RoleUser.php b/app/Models/RoleUser.php index 2af8b5b..67d360a 100644 --- a/app/Models/RoleUser.php +++ b/app/Models/RoleUser.php @@ -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. * @@ -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 :role.name was :event :to User :user.name 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 diff --git a/app/Models/Traits/LogsModel.php b/app/Models/Traits/LogsModel.php index 4b66230..ac58960 100644 --- a/app/Models/Traits/LogsModel.php +++ b/app/Models/Traits/LogsModel.php @@ -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; @@ -22,7 +24,8 @@ public function getActivitylogOptions(): LogOptions ->toArray() ) ->logOnlyDirty() - ->dontSubmitEmptyLogs(); + ->dontSubmitEmptyLogs() + ->setDescriptionForEvent(fn(string $eventName) => $this->logDescription($eventName)); } public function logExcept(): array @@ -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 :subject.name was :event by :causer.name', [ + 'subject.type' => str(class_basename(get_class($this)))->headline(), + 'subject.name' => e($this->getLogSubjectName()), + 'event' => $eventName, + 'causer.name' => $this->getLogCauserName(), + ]); + } } diff --git a/database/seeders/RoleSeeder.php b/database/seeders/RoleSeeder.php index ae65555..440e4b4 100644 --- a/database/seeders/RoleSeeder.php +++ b/database/seeders/RoleSeeder.php @@ -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.'] ); }