Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spatie/laravel-permission There is no permission named `edit_project` for guard `api`

I am using Laravel 5.6 with spatie/laravel-permission version 2.9 also using Laravel Passport as auth driver with $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

public function assignPermissions($role, $permissions)
    {

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

        $role->givePermissionTo($permissions);

        return $role;
    }

but getting the error There is no permission namededit_projectfor guardapi`.

Also I have at 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.

enter image description here

but manually I am changing the guard_name field to "api" which my permission table became like this.

enter image description here

like image 313
Barakzai Avatar asked Mar 03 '18 17:03

Barakzai


People also ask

How to check if a user has a permission in Laravel?

So you can check if a user has a permission with Laravel's default can function: $user->assignRole ('writer'); // You can also assign multiple roles at once $user->assignRole ('writer', 'admin'); // or as an array $user->assignRole ( ['writer', 'admin']);

Is it possible to edit articles in guard'web'in Laravel?

There is no permission named `edit articles` for guard `web`. · Issue #590 · spatie/laravel-permission · GitHub Have a question about this project?

What version of Laravel is used in spatie?

Sign in to your account I am using Laravel 5.6 with spatie/laravel-permission version 2.9 also using Laravel Passport as auth driver with $guard = 'api'.

Is there a [permission] with ID 100 for guard API?

There is no [permission] with id 100 for guard api. any idea? Sorry, something went wrong. In my case, i work in a laravel project and I create a new permissions and roles, but artisan don´t recognize new user, then you need use php artisan cache:clear


5 Answers

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
like image 82
Qasim Ali Avatar answered Oct 20 '22 01:10

Qasim Ali


Clear your cache php artisan cache:clear

if this does not work use sudo php artisan cache:clear it worked for me once i use sudo

like image 36
beatusfk Avatar answered Oct 20 '22 02:10

beatusfk


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

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"

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)

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.

like image 5
joelrosenthal Avatar answered Oct 20 '22 01:10

joelrosenthal


Move the web and api places from

'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

like image 4
Barakzai Avatar answered Oct 20 '22 02:10

Barakzai


It might be a permissions issue. run below command.

sudo php artisan permission:cache-reset
like image 2
Dharmesh Rakholia Avatar answered Oct 20 '22 01:10

Dharmesh Rakholia