我有一个问题spatie/laravel-permissions
...
我使用Gate
inAuthServiceProvider.php
来定义Superadmin
(可以绕过所有权限而不将其注册到角色)...
它与can('the-permission')
助手完美配合。
但它不适用于Auth::user()->hasPermissionTo('the-permission')
...
.
.
下面是我的代码:
.
在AuthServiceProvider.php
:
public function boot()
{
$this->registerPolicies();
Gate::before(function ($user, $ability) {
$superadmin_rolename = 'Superadmin';
$guard_name = 'web-admin';
return $user->hasRole($superadmin_rolename , $guard_name ) ? true : null;
});
}
.
.
在刀片中:
@can('add products')
<button type="submit">Add Product</button>
@endcan
// this will work perfectly, the button will be shown
.
.
在控制器中:
public function addProduct()
{
$admin = Auth::guard('web-admin')->user();
if($admin->hasPermissionTo('add products')) return true;
return false;
}
// this is not working (it return false)... i dont know why.... it should return true....
所以,正如我在上面向您展示的那样:
- 我
Gate
用来定义超级管理员 - 超级管理员应授予所有访问权限
- 它与
can()
和完美配合$user->can()
- 但它不适用于
$user->hasPermissionTo()
<--------- 这是我想知道的
谢谢