How to prevent role customer from logging an event?

I thought it's working but when we tried in the staging sting customer role can log an event I tried the shouldLog as suggested <?php namespace App\Models\Traits; use App\Models\User; use App\Enums\User\Role; use Spatie\Activitylog\LogOptions; use Spatie\Activitylog\Contracts\Activity; use Spatie\Activitylog\Traits\LogsActivity; trait HasLogActivity { use LogsActivity; public function shouldLog(string $eventName): bool { if (auth()->check() && auth()->user()->role === Role::SuperAdmin) { return false; } return true; } protected function mergeActivityProperties(Activity $activity) { $activity->properties = $activity->properties->merge([ 'ip' => getRequestIP(request()), 'user_agent' => request()->userAgent(), ]); } public function tapActivity(Activity $activity, string $eventName) { $this->mergeActivityProperties($activity); if ($activity->causer && $activity->causer instanceof User) { $activity->causer_role = $activity->causer->role->value; } } public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logAll() ->logExcept(['created_at', 'updated_at']) ->logOnlyDirty() ->dontSubmitEmptyLogs() ->setDescriptionForEvent(fn(string $eventName) => "This model has been {$eventName}") ->useLogName(class_basename($this)); } } **
1 Reply
toeknee
toeknee2mo ago
What id you DD the tapActivity ? and debugged what's happening there.

Did you find this page helpful?