spatie/laravel-permission 没有名为`edit_project` 的权限用于guard `api`
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49086974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spatie/laravel-permission There is no permission named `edit_project` for guard `api`
提问by Barakzai
I am using Laravel 5.6 with spatie/laravel-permission version 2.9 also using Laravel Passport as auth driver with $guard = 'api'
.
我正在使用 Laravel 5.6 和 spatie/laravel-permission 2.9 版,还使用 Laravel Passport 作为$guard = 'api'
.
When I am trying to assign an array of permission like ['edit_project', 'add_project' 'delete_project']
to a role with help of this function
当我尝试在['edit_project', 'add_project' 'delete_project']
此功能的帮助下为角色分配一系列权限时
public function assignPermissions($role, $permissions)
{
$role = Role::findByName($role);
$role->givePermissionTo($permissions);
return $role;
}
but getting the error There is no permission named
edit_projectfor guard
api`.
但得到错误There is no permission named
edit_project for guard
api`。
Also I have at config/auth.php
我也在 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,
],
],
];
if there is any solution please help me with it thanks.
如果有任何解决方案,请帮助我,谢谢。
as well I am seeding the permission table by help of Larvel seeder which my permission table looks at the first time like below which the guard_name
is web.
我也在 Larvel 播种机的帮助下为权限表播种,我的权限表第一次看起来像下面的guard_name
网络。
but manually I am changing the guard_name
field to "api" which my permission table became like this.
但是我手动将guard_name
字段更改为“api”,我的权限表变成了这样。
回答by Qasim Ali
After creating permissions, running the following commands should work as it worked for me.
创建权限后,运行以下命令应该可以正常工作。
php artisan cache:forget spatie.permission.cache
then
php artisan cache:clear
回答by beatusfk
Clear your cache php artisan cache:clear
清除缓存 php artisan cache:clear
if this does not work use sudo php artisan cache:clear
it worked for me once i use sudo
如果这不起作用sudo php artisan cache:clear
,一旦我使用 sudo,它就对我有用
回答by Barakzai
Move the web and api places from
将 web 和 api 位置从
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
To
到
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
'web' => [
'driver' => 'session',
'provider' => 'users',
],
]
run php artisan cache:clear
跑 php artisan cache:clear
回答by joelrosenthal
The package uses the default guard unless instructed otherwise. The way to instruct it otherwise is to add the following to the Role
class public $guard_name = 'api';
. Of course adding that to the class in the vendor
directory is a bad idea so you'd want to extend it and specify the guard like this
除非另有说明,否则包使用默认保护。否则指示它的方法是将以下内容添加到Role
类中public $guard_name = 'api';
。当然,将它添加到vendor
目录中的类是一个坏主意,所以你想扩展它并像这样指定守卫
use Spatie\Permission\Models\Role as OriginalRole;
class Role extends OriginalRole
{
public $guard_name = 'api';
}
Then if you haven't done so already, generate the config file with php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
然后,如果您还没有这样做,请使用以下命令生成配置文件 php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"
Lastly you'll want to register your Role
in config/permissions.php
by changing 'role' => Spatie\Permission\Models\Role::class,
to 'role' => \App\Models\Role::class,
(of course this will vary based on where your Role
class is)
最后你要注册Role
的config/permissions.php
改变'role' => Spatie\Permission\Models\Role::class,
来'role' => \App\Models\Role::class,
(当然这会根据您的Role
类)
Also the example from your question mentions add_project
but the database shows create_project
so make sure you're using the same names everywhere.
您的问题中的示例也提到了add_project
但数据库显示,create_project
因此请确保您在任何地方都使用相同的名称。
回答by GotaloveCode
In your user model add protected $guard_name = 'api';
This will override the default guard which is web.
在您的用户模型中添加protected $guard_name = 'api';
这将覆盖默认保护web。