- Register a `Gate::before` hook so the super-admin role bypasses every permission check, replacing the removed filament-shield gate - Add Role::superAdminName() as the single source for the bypass role - Gate module panel access behind per-module `access:<module>` permissions via the PermissionRegistry, falling back to `view:modules` - Bind PermissionRegistry as a singleton - Point config/permission.php at the app's Role and Permission models
88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\RoleFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Support\Carbon;
|
|
use Spatie\Activitylog\LogOptions;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role as SpatieRole;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $name
|
|
* @property string $guard_name
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property int $disable_activity_checks
|
|
* @property-read Collection<int, Activity> $activities
|
|
* @property-read int|null $activities_count
|
|
* @property-read Collection<int, Permission> $permissions
|
|
* @property-read int|null $permissions_count
|
|
* @property-read Collection<int, User> $users
|
|
* @property-read int|null $users_count
|
|
*
|
|
* @method static Builder<static>|Role byName(string $name)
|
|
* @method static RoleFactory factory($count = null, $state = [])
|
|
* @method static Builder<static>|Role newModelQuery()
|
|
* @method static Builder<static>|Role newQuery()
|
|
* @method static Builder<static>|Role permission($permissions, bool $without = false)
|
|
* @method static Builder<static>|Role query()
|
|
* @method static Builder<static>|Role whereCreatedAt($value)
|
|
* @method static Builder<static>|Role whereDisableActivityChecks($value)
|
|
* @method static Builder<static>|Role whereGuardName($value)
|
|
* @method static Builder<static>|Role whereId($value)
|
|
* @method static Builder<static>|Role whereName($value)
|
|
* @method static Builder<static>|Role whereUpdatedAt($value)
|
|
* @method static Builder<static>|Role withoutPermission($permissions)
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Role extends SpatieRole
|
|
{
|
|
use HasFactory;
|
|
use LogsActivity;
|
|
|
|
protected $fillable = [
|
|
'id',
|
|
'name',
|
|
'guard_name',
|
|
'disable_activity_checks',
|
|
];
|
|
|
|
/**
|
|
* The name of the role that bypasses every permission check.
|
|
*
|
|
* Replaces the removed filament-shield `Utils::getSuperAdminName()`.
|
|
*/
|
|
public static function superAdminName(): string
|
|
{
|
|
return config('roles.super_admin', 'super_admin');
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly($this->fillable)
|
|
->logOnlyDirty()
|
|
->dontSubmitEmptyLogs();
|
|
}
|
|
|
|
/**
|
|
* Filter by exact role name. Case-sensitivity follows the database
|
|
* collation (case-sensitive on SQLite default; case-insensitive on
|
|
* MySQL `utf8mb4_unicode_ci`).
|
|
*/
|
|
#[Scope]
|
|
protected function byName(Builder $q, string $name): Builder
|
|
{
|
|
return $q->where('name', $name);
|
|
}
|
|
}
|