Skip to content

Commit

Permalink
handle super admin skip gate checks
Browse files Browse the repository at this point in the history
Signed-off-by: Lloric Mayuga Garcia <[email protected]>
  • Loading branch information
lloricode committed Aug 24, 2024
1 parent cb66ddd commit 55f278e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@ return [
* role_names.*.admin is required
*/
'role_names' => [

// keyed by guard name
'web' => [

// required this cannot rename or delete or modify permissions
'super_admin' => 'super_admin', // no permission attached to this role, but it always skips gate checks
'admin' => 'admin', // all permissions attached to this role
/**
* no permission attached to this role, but it always skips gate checks
* see https://freek.dev/1325-when-to-use-gateafter-in-laravel
*/
'super_admin' => 'super_admin',

/**
* all permissions attached to this role
*/
'admin' => 'admin',

// as many as you want, below it can edit permissions but cannot rename or delete role names
// sample 'user' => 'user',
Expand All @@ -55,7 +65,6 @@ return [
'translated' => false,
];


```

## Usage
Expand Down
14 changes: 12 additions & 2 deletions config/filament-permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@
* role_names.*.admin is required
*/
'role_names' => [

// keyed by guard name
'web' => [

// required this cannot rename or delete or modify permissions
'super_admin' => 'super_admin', // no permission attached to this role, but it always skips gate checks
'admin' => 'admin', // all permissions attached to this role
/**
* no permission attached to this role, but it always skips gate checks
* see https://freek.dev/1325-when-to-use-gateafter-in-laravel
*/
'super_admin' => 'super_admin',

/**
* all permissions attached to this role
*/
'admin' => 'admin',

// as many as you want, below it can edit permissions but cannot rename or delete role names
// sample 'user' => 'user',
Expand Down
15 changes: 15 additions & 0 deletions src/Contracts/HasPermissionUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Lloricode\FilamentSpatieLaravelPermissionPlugin\Contracts;

interface HasPermissionUser
{
/**
* This will skipp all gate checks
*/
public function isSuperAdmin(?string $guardName = null): bool;

public function isAdminOrSuperAdmin(?string $guardName = null): bool;
}
12 changes: 12 additions & 0 deletions src/FilamentSpatieLaravelPermissionPluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Lloricode\FilamentSpatieLaravelPermissionPlugin;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Gate;
use Livewire\Features\SupportTesting\Testable;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Commands\PermissionSyncCommand;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Contracts\HasPermissionUser;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Policies\RolePolicy;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Testing\TestsFilamentSpatieLaravelPermissionPlugin;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -32,6 +34,16 @@ public function packageRegistered(): void

public function packageBooted(): void
{
Gate::after(function (Authenticatable $user) {

if ($user instanceof HasPermissionUser && $user->isSuperAdmin()) {
/** @see https://freek.dev/1325-when-to-use-gateafter-in-laravel */
return true;
}

return null;
});

// Testing
Testable::mixin(new TestsFilamentSpatieLaravelPermissionPlugin);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use Filament\Models\Contracts\HasName;
use Filament\Panel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Concern\PermissionUser;
use Lloricode\FilamentSpatieLaravelPermissionPlugin\Concern\HasUserPermission;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements FilamentUser, HasName
{
use HasRoles;
use PermissionUser;
use HasUserPermission;

protected $guard_name = 'web';

Expand Down

0 comments on commit 55f278e

Please sign in to comment.