Laravel-permission: There is no role named `admin` ???

Created on 12 Mar 2018  路  13Comments  路  Source: spatie/laravel-permission

my code:
$user_id=1;
$user = User::find($user_id);
$roles = $user->getRoleNames();
$roles->map(function ($role) use ($user) {
$user->removeRole($role);
});

auth file

'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

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

roles table
qq20180312-212827 2x

model_has_roles table
qq20180312-213026 2x

support

Most helpful comment

Why I add to User Model

Protected $guard_name ='api';

Just fine

qq20180312-215818 2x

auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
], 锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵

All 13 comments

Why I add to User Model

Protected $guard_name ='api';

Just fine

qq20180312-215818 2x

auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
], 锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵锛燂紵

I'm not sure I understand your post.

Problem

OP means, that HasRoles doesn't use default guard from config/auth.php.

I have in config/auth.php:

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

and seeder:

$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());

The seeder will add "super-admin" role to api guard.

While using $user->assignRole('super-admin'); is throwed error:

There is no role named super-admin

and need add protected $guard_name = 'api'; to model, to fix it.

Reason

https://github.com/spatie/laravel-permission/blob/master/src/Guard.php#L49
static::getNames($class)->first() returns "web" despite default guard set on "api". Probably User model have guard defaults set on 'web'.

I had the same issue. Spent 1 hour wondering why it wouldn't find the role.

Like written, defining guard_name on model solves it. But this shouldn't be necessary.

@canvural wrote:

But this shouldn't be necessary.

I understand, and agree. We're rewriting a number of things for (what might be v3) and aim to improve this... particularly the way it handles guards. (Right now it looks to the model as the primary source of truth, and if not set there then falls back to other options based on configs.)

This is a bad idea:

$role = Role::create(['name' => 'super-admin']);
$role->givePermissionTo(Permission::all());

Because when new permissions are added, super-admin will not have perm.

It is much better to do this through Laravel Gate:

Gate::before(function (User $user) {
    if ($user->hasRole('super-admin')) {
        return true;
    }
});

@drbyte I am still experiencing this issue in v3 despite setting default guard in my config to api. I still need to add $guard_name to my User model.

Is this intended?

@danvim wrote:

@drbyte I am still experiencing this issue in v3 despite setting default guard in my config to api. I still need to add $guard_name to my User model.

Is this intended?

Yes. v3 didn't get those sweeping changes. They haven't been completed yet.

For compatibility with Laravel's semantic numbering change, and other dependencies, we released v3 as basically v2 but with modern dependencies. This also allowed us to remove code required to patch for much older Laravel and PHP versions.
Apologies for any confusion. Numbering is sometimes hard :)

@drbyte Right. I was just confused since I saw the issue was closed and thought it has been fixed and released. If the issue is tracked somewhere else, that's fine. It's working with $guard_name anyway.

@SzymonLisowiec
I had another guard alongsite api and web.
As you followed to add $guard_name = "web" property in User class, It work with web guard but what about another guard (here my guard name is *customer)?*
customer guard does not work.

@SzymonLisowiec
I had another guard alongsite api and web.
As you followed to add $guard_name = "web" property in User class, It work with web guard but what about another guard (here my guard name is *customer)?*
customer guard does not work.

Check it: https://docs.spatie.be/laravel-permission/v3/basic-usage/multiple-guards/

I got a solution for this.

  • Add public static $guard_name = "customer" property in User model.
  • Than before assignRole() method, Just rewrite $guard_name property like this
    User::$guard_name = 'guard name';
  • Than you can call assignRole() method.
    $user->assignRole("role name");
    Make sure the role has valid guard name..
    Hope it will help you.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bbdangar picture bbdangar  路  4Comments

enghelewa picture enghelewa  路  4Comments

neoreids picture neoreids  路  3Comments

bhulsman picture bhulsman  路  3Comments

dylangeorgeharbour picture dylangeorgeharbour  路  3Comments