diff --git a/app/Models/Activity.php b/app/Models/Activity.php index e1864f0..fec9283 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -4,10 +4,21 @@ use App\Models\Scopes\OrderByIdDesc; use Illuminate\Database\Eloquent\Attributes\ScopedBy; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; use Spatie\Activitylog\Models\Activity as ModelsActivity; #[ScopedBy(OrderByIdDesc::class)] class Activity extends ModelsActivity { - // + public function scopeForSubject(Builder $query, Model $subject): Builder + { + return $query->where(function ($q) use ($subject) { + parent::scopeForSubject($q, $subject); + + if (method_exists($subject, 'logIncludes')) { + $q->orWhere(fn($q2) => $subject->logIncludes($q2)); + } + }); + } } diff --git a/app/Models/Role.php b/app/Models/Role.php index 29e3708..95c4e37 100644 --- a/app/Models/Role.php +++ b/app/Models/Role.php @@ -4,6 +4,7 @@ use App\Enums\Role as EnumsRole; use App\Models\Traits\LogsModel; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Spatie\Permission\Models\Role as ModelsRole; @@ -13,6 +14,13 @@ class Role extends ModelsRole use HasFactory; use LogsModel; + public function logIncludes(Builder $query): Builder + { + return $query->whereHasMorph('subject', [PermissionRole::class], function ($q) { + $q->where(config('permission.column_names.role_pivot_key'), $this->getKey()); + }); + } + public function isSuperAdmin(): bool { return $this->name === EnumsRole::SUPER_ADMIN->value; diff --git a/app/Models/Traits/LogsModel.php b/app/Models/Traits/LogsModel.php index 643060e..4b66230 100644 --- a/app/Models/Traits/LogsModel.php +++ b/app/Models/Traits/LogsModel.php @@ -3,6 +3,7 @@ namespace App\Models\Traits; use App\Support\LogMasksAttribute; +use Illuminate\Database\Eloquent\Builder; use Spatie\Activitylog\LogOptions; use Spatie\Activitylog\Traits\LogsActivity; @@ -33,4 +34,9 @@ public static function bootLogsModel(): void { static::addLogChange(new LogMasksAttribute); } + + public function logIncludes(Builder $query): Builder + { + return $query; + } } diff --git a/app/Models/User.php b/app/Models/User.php index 0fb8078..bf90745 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,6 +8,7 @@ use App\Models\Traits\LogsModel; use Filament\Models\Contracts\FilamentUser; use Filament\Panel; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Foundation\Auth\User as Authenticatable; @@ -73,6 +74,15 @@ public function logExcept(): array return ['remember_token']; } + public function logIncludes(Builder $query): Builder + { + return $query->whereHasMorph('subject', [RoleUser::class], function ($q) { + $q + ->where('model_type', $this->getMorphClass()) + ->where(config('permission.column_names.model_morph_key'), $this->getKey()); + }); + } + /** * The roles that belong to the user. *