spatie/laravel-permission

There is no permission named `list-users` for guard `web`.

fcno opened this issue · 2 comments

fcno commented

Describe the bug
There is no permission named list-users for guard web, when checking user permission via role like that:

$user->hasPermissionTo(Permission::USER_VIEW_ANY);

My User Policy

public function viewAny(User $user): bool
{
    return $user->hasPermissionTo(Permission::USER_VIEW_ANY);
}

My Permission enum

enum Permission: string
{
    case USER_VIEW_ANY = 'list-users';
    case USER_VIEW = 'view-user';
    case USER_UPDATE = 'edit-user';
}

My PermissionSeeder

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // create permissions
        $role = Role::create(['name' => EnumsRole::ADMINISTRATOR->value]);

        // create roles and assign created permissions
        Permission::create(['name' => EnumsPermission::USER_VIEW_ANY->value]);
        Permission::create(['name' => EnumsPermission::USER_VIEW->value]);
        Permission::create(['name' => EnumsPermission::USER_UPDATE->value]);

        $role->givePermissionTo(Permission::all());
    }
}

My User Class

class User extends AuthUser implements LdapAuthenticatable
{
    use AuthenticatesWithLdap, HasFactory, HasRoles;

    protected $guard_name = 'web';

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected function getDefaultGuardName(): string
    {
        return 'web';
    }
}

My Auth config

return [

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

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

    'providers' => [
        'users' => [
            'driver' => 'ldap',
            'model' => \LdapRecord\Models\ActiveDirectory\User::class,
            'rules' => [],
            'database' => [
                'model' => \App\Models\User::class,
            ],
        ],

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

];

Versions

  • spatie/laravel-permission package version: 10.34
  • illuminate/framework package: 6.1

PHP version: 8.1

Database version: mysql 8

To Reproduce
I'm trying to use $user->hasPermissionTo(Permission::USER_VIEW_ANY), but I always get the error on title.
Diving into the code, I found findByName which is used by hasPermissionTo and which does not find the permission.

Just to add more info

$user->getPermissionNames();// Returns empty, despite having permissions via roles.
$user->getRoleNames(); // Returns the expected roles

$user->getPermissionsViaRoles(); // Returns all expected permissions

Expected behavior
I hope to be able to check the permission linked to roles as follows:

$user->hasPermissionTo(Permission::USER_VIEW_ANY);

Environment (please complete the following information, because it helps us investigate better):

  • Linux Red Hat
  • Version 9.2

make an example app showing the problem

fcno commented

Hey @parallels999

I tried hard the last few days to recreate the bug, but I just couldn't.

In the example app I was creating, it just worked.

So, because it was a very small application, I recreated the application and, to my surprise, the bug didn't happen also.

I did a diff from the bugged project to the new one and the code was exactly the same.

Anyway, there was some junk in the application that was causing the bug, but unfortunately I couldn't identify it.

Of course, I did procedures like clearing the laravel cache, the package cache, deleting the vendor folder and recreating it, but all in vain. I couldn't identify the problem.

I'm closing this report, but if at any point I encounter the problem again, I'll come back and to report it with an example app.

Hugs and thanks!