Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Latest commit

 

History

History
56 lines (39 loc) · 1.46 KB

05-creating-a-super-admin.md

File metadata and controls

56 lines (39 loc) · 1.46 KB
title weight
Creating a super admin
5

To add a super admin to your system, you should use the global Gate::before or Gate::after rules.

Then you can just use the permission-based controls in your application, without always checking for isAdmin or anything else everywhere

Gate::before

If you want your super admin to return true for all permissions checking without assigning all permissions to that user, you should use the laravel default Gate::before method.

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerPolicies();
        
        Gate::before(fn ($user, $ability) => $user->hasGroup('Super Admin') ? true : null);
    }
}

The closure passed to Gate::before will be used before any policy gets called.

The Gate::before method needs to return null instead of false in order to not interfere with normal policy operations.

Gate::after

With Gate::after instead of Gate::before, the policies will be called first, even for superadmins.

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerPolicies();
        
        Gate::after(fn ($user, $ability) => $user->hasGroup('Super Admin'));
    }
}