簡體   English   中英

Laravel - 設置 Spatie 權限的問題 - 角色關系返回 NULL

[英]Laravel - Issues setting up Spatie Permissons - Roles Relationship Returns NULL

我有一個 laravel 應用程序,當前有兩個 sql 數據庫連接,默認的“mysql”連接和另一個“主”連接。

默認包含應用程序的數據,其中主連接包含用戶、角色和權限。

我可以確認用戶已分配給角色,因為主連接當前用於分配了角色和權限的另一個應用程序。

我正在嘗試創建一個管理員登錄名,它將從主連接驗證用戶。 我為此創建了一個 serpate auth guard 和中間件。 但是我只需要讓某些用戶角色登錄。

我已經更新了我的登錄 POST,以便它

if (!$user->hasAnyRole(['administrator', 'manager', 'senior_manager', 'operations_manager', 'head_office_operations', 'hr'])) {
   throw new \Exception('Invalid user role');
}

我遇到的問題是 hasAnyRole() 方法只返回一個空集合。 雖然肯定有分配的角色。

我的用戶 Model:

<?php

namespace App\Models\Master;

use App\Traits\Encryptable;
use Spatie\Permission\Traits\HasRoles;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Encryptable, HasRoles, HasFactory, Notifiable, SoftDeletes;

    protected $connection = 'app_master';
    protected $table = 'users';

   [...]

}

我創建了一個 Role 和 Permisson Model,它們都擴展了相關的 spatie model:

class 角色擴展 \Spatie\Permission\Models\Role

class 權限擴展 \Spatie\Permission\Models\Permission

並包括:

protected $connection = 'app_master';
protected $table = 'permissions';

ETC..

將 permission.php 配置更新為:

<?php

return [

    'models' => [

        /*
         * When using the "HasPermissions" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your permissions. Of course, it
         * is often just the "Permission" model but you may use whatever you like.
         *
         * The model you want to use as a Permission model needs to implement the
         * `Spatie\Permission\Contracts\Permission` contract.
         */

//        'permission' => Spatie\Permission\Models\Permission::class,
        'permission' => App\Models\Master\Permission::class,

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your roles. Of course, it
         * is often just the "Role" model but you may use whatever you like.
         *
         * The model you want to use as a Role model needs to implement the
         * `Spatie\Permission\Contracts\Role` contract.
         */

//        'role' => Spatie\Permission\Models\Role::class,
        'role' => App\Models\Master\Role::class,

    ],

    'table_names' => [

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your roles. We have chosen a basic
         * default value but you may easily change it to any table you like.
         */

        'roles' => 'roles',

        /*
         * When using the "HasPermissions" trait from this package, we need to know which
         * table should be used to retrieve your permissions. We have chosen a basic
         * default value but you may easily change it to any table you like.
         */

        'permissions' => 'permissions',

        /*
         * When using the "HasPermissions" trait from this package, we need to know which
         * table should be used to retrieve your models permissions. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'model_has_permissions' => 'model_has_permissions',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your models roles. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'model_has_roles' => 'model_has_roles',

        /*
         * When using the "HasRoles" trait from this package, we need to know which
         * table should be used to retrieve your roles permissions. We have chosen a
         * basic default value but you may easily change it to any table you like.
         */

        'role_has_permissions' => 'role_has_permissions',
    ],

    'column_names' => [

        /*
         * Change this if you want to name the related model primary key other than
         * `model_id`.
         *
         * For example, this would be nice if your primary keys are all UUIDs. In
         * that case, name this `model_uuid`.
         */

        'model_morph_key' => 'model_id',
    ],

    /*
     * When set to true, the required permission names are added to the exception
     * message. This could be considered an information leak in some contexts, so
     * the default setting is false here for optimum safety.
     */

    'display_permission_in_exception' => false,

    /*
     * When set to true, the required role names are added to the exception
     * message. This could be considered an information leak in some contexts, so
     * the default setting is false here for optimum safety.
     */

    'display_role_in_exception' => false,

    /*
     * By default wildcard permission lookups are disabled.
     */

    'enable_wildcard_permission' => false,

    'cache' => [

        /*
         * By default all permissions are cached for 24 hours to speed up performance.
         * When permissions or roles are updated the cache is flushed automatically.
         */

        'expiration_time' => \DateInterval::createFromDateString('24 hours'),

        /*
         * The cache key used to store all permissions.
         */

        'key' => 'spatie.permission.cache',

        /*
         * When checking for a permission against a model by passing a Permission
         * instance to the check, this key determines what attribute on the
         * Permissions model is used to cache against.
         *
         * Ideally, this should match your preferred way of checking permissions, eg:
         * `$user->can('view-posts')` would be 'name'.
         */

        'model_key' => 'name',

        /*
         * You may optionally indicate a specific cache driver to use for permission and
         * role caching using any of the `store` drivers listed in the cache.php config
         * file. Using 'default' here means to use the `default` set in cache.php.
         */

        'store' => 'default',
    ],
];

我已經嘗試更新 model 並將表更新為 'roles' => 'app_master.roles' 以嘗試引用表名,但它似乎沒有根據 HasRoles Trait 建立正確的關系?

使用默認 permisson.php 配置時出現同樣的問題。

為了讓關系正常工作並返回主連接中定義的正確用戶角色和權限,我做錯了什么?

任何幫助將非常感激。

為用戶添加角色后嘗試重置權限緩存。

命令: php artisan permission:cache-reset

不確定您使用的是哪個 spatie 版本以及如何添加角色,但是如果您沒有使用 spatie 提供的方法添加角色或低於 v4.4.0 的 spatie,spatie 將不會自動重建權限緩存,這可能會導致問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM