空间/laravel权限-门和hasPermissionTo不工作

空间/laravel权限-门和hasPermissionTo不工作,laravel,permissions,spatie,Laravel,Permissions,Spatie,我对空间/laravel权限有问题 我在AuthServiceProvider.php中使用Gate来定义Superadmin(可以绕过所有权限而不将其注册到角色) 它与can('the-permission')helper完美配合 但是它不能与Auth::user()->hasPermissionTo('the-permission')一起使用 下面是我的代码: 在AuthServiceProvider.php中: public function boot() { $this-&

我对
空间/laravel权限有问题

我在
AuthServiceProvider.php
中使用
Gate
来定义
Superadmin
(可以绕过所有权限而不将其注册到角色)

它与
can('the-permission')
helper完美配合

但是它不能与
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
因此,正如我在上面向大家展示的:

  • 我使用
    Gate
    来定义超级管理员
  • 超级管理员应授予所有访问权限
  • 它与
    can()
    $user->can()

  • 但是它不适用于
    $user->hasPermissionTo()
    根据@Remul的评论,我发现只有
    can()
    $user->can()
    才能完美地适用于
    Gate::before

    那么,如果我想使用另一种方法,如
    $user->hasAnyPermission
    $user->hasAllPermissions

    这就是我要做的…我决定在
    Admin
    模型中创建一个自定义方法

    <?php
    
    namespace Model;
    
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Spatie\Permission\Traits\HasRoles;
    
    class Admin extends Authenticatable
    {
        use HasRoles;
    
        protected $guard_name = "web-admin";
    
        protected $fillable = ['name', 'email', 'password'];
    
        public function canAny(array $permissions)
        {
            foreach($permissions as $e){
                if($this->can($e)) return true;
            }
    
            return false;
        }
    
        public function canAll(array $permissions)
        {
            foreach($permissions as $e){
                if(!$this->can($e)) return false;
            }
    
            return true;
        }
    }
    

    haspmissionto
    只检查数据库,它不运行
    gate::before
    ,应该会回答你的问题。感谢分享答案……我在我的用户模式中直接编写了这两个函数。我从Spatial中获取了函数名(hasAnyPermission和hasAllPermissions),这很有效。
    <?php
    
    namespace Model;
    
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Spatie\Permission\Traits\HasRoles;
    
    class Admin extends Authenticatable
    {
        use HasRoles;
    
        protected $guard_name = "web-admin";
    
        protected $fillable = ['name', 'email', 'password'];
    
        public function canAny(array $permissions)
        {
            foreach($permissions as $e){
                if($this->can($e)) return true;
            }
    
            return false;
        }
    
        public function canAll(array $permissions)
        {
            foreach($permissions as $e){
                if(!$this->can($e)) return false;
            }
    
            return true;
        }
    }