8

我正在使用带有 spatie/laravel-permission 版本 2.9 的 Laravel 5.6,还使用 ​​Laravel Passport 作为$guard = 'api'.

当我尝试在['edit_project', 'add_project' 'delete_project']此功能的帮助下将一组权限分配给角色时

public function assignPermissions($role, $permissions)
    {

        $role = Role::findByName($role);

        $role->givePermissionTo($permissions);

        return $role;
    }

但得到错误There is no permission namededit_project for guardapi`。

我也有 config/auth.php

return [

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

];

如果有任何解决方案,请帮助我,谢谢。

我也在 Larvel 播种器的帮助下播种权限表,我的权限表第一次看起来像下面的guard_nameweb。

在此处输入图像描述

但是我手动将guard_name字段更改为“api”,我的权限表变成了这样。

在此处输入图像描述

4

8 回答 8

23

创建权限后,运行以下命令应该对我有用。

php artisan cache:forget spatie.permission.cache 

然后

php artisan cache:clear
于 2018-11-13T15:22:53.120 回答
7

清除缓存php artisan cache:clear

如果这不起作用sudo php artisan cache:clear,一旦我使用 sudo,它就对我有用

于 2019-04-29T15:53:59.630 回答
4

将 web 和 api 位置从

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

'guards' => [

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],

        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ]

php artisan cache:clear

于 2018-03-04T07:12:30.650 回答
4

除非另有说明,否则该软件包使用默认保护。否则,指示它的方法是将以下内容添加到Rolepublic $guard_name = 'api';中。当然,将它添加到vendor目录中的类是一个坏主意,所以你想扩展它并像这样指定守卫

use Spatie\Permission\Models\Role as OriginalRole;

class Role extends OriginalRole
{
    public $guard_name = 'api';
}

然后,如果您还没有这样做,请生成配置文件php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

最后,您需要通过更改为来注册(当然这会根据您的班级所在位置而有所Role不同)config/permissions.php'role' => Spatie\Permission\Models\Role::class,'role' => \App\Models\Role::class,Role

此外,您的问题中的示例也提到add_project了,但数据库显示create_project,因此请确保您在任何地方都使用相同的名称。

于 2018-03-04T18:18:34.087 回答
1

您需要在创建角色或权限失败时指定保护,其中 spatie 将承担出现在 config/auth 中的第一个保护,在这种情况下为“web”

'defaults' => [
'guard' => 'web',
'passwords' => 'users',
   ],

您需要按以下方式处理:

// Create a manager role for users authenticating with the api guard:
$role = Role::create(['guard_name' => 'api', 'name' => 'manager']);

// Define a `edit_project` permission for the admin users belonging to the api guard
$permission = Permission::create(['guard_name' => 'api', 'name' => 'edit_project']);
于 2021-09-24T11:01:33.397 回答
0

可能是权限问题。在命令下方运行。

sudo php artisan permission:cache-reset
于 2022-01-05T10:18:21.753 回答
0

在您的用户模型中添加protected $guard_name = 'api';这将覆盖默认保护,即web

于 2018-08-01T15:41:07.813 回答
-1

清空数据库中的角色和权限表,然后重新填表。我有这个错误,我用这种方式修复了它

于 2021-06-11T20:28:31.500 回答