[英]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.