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
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.
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'));
}
}