问题描述
我在laravel应用程序中使用spatie / laravel权限用户管理。我需要授予特定用户的超级管理员权限。如何在Spatie / laravel权限中执行此操作?我希望使用超级管理员帐户的原因是,当我们删除所有用户角色时,我们无法访问laravel应用程序。
这是我的控制器,我是这样的验证权限。
public function __construct()
{
$this->middleware('auth');
$this->middleware('permission:view-student');
$this->middleware('permission:add-student',['only' => ['create','store']]);
$this->middleware('permission:edit-student',['only' => ['edit','update']]);
}
解决方法
您可以定义该AppServiceProvider,因为您需要一个没有用户角色的超级管理员(因为您删除了所有用户角色),因此可以通过电子邮件等其他列进行验证:
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
public function boot()
{
$this->registerPolicies();
// Implicitly grant "Super Admin" role all permissions
// This works in the app by using gate-related functions like auth()->user->can() and @can()
Gate::before(function ($user,$ability) {
return $user->email == '[email protected]' ?? null;
});
}
}
更多信息,请访问here