Skip to content

Commit

Permalink
activity log subject includes
Browse files Browse the repository at this point in the history
  • Loading branch information
fzldn committed Sep 17, 2024
1 parent 5990c1e commit 4517296
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});
}
}
8 changes: 8 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Traits/LogsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -33,4 +34,9 @@ public static function bootLogsModel(): void
{
static::addLogChange(new LogMasksAttribute);
}

public function logIncludes(Builder $query): Builder
{
return $query;
}
}
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 4517296

Please sign in to comment.