spatie/laravel-permission

hasAnyRole method has problem

raSooolSh opened this issue · 4 comments

hasAnyRole() method dosent true work when array pass to this and return false always

The test suite shows that it does work with arrays:

public function it_can_determine_that_a_user_has_one_of_the_given_roles()
{
$roleModel = app(Role::class);
$roleModel->create(['name' => 'second role']);
$this->assertFalse($this->testUser->hasRole($roleModel->all()));
$this->testUser->assignRole($this->testUserRole);
$this->assertTrue($this->testUser->hasRole($roleModel->all()));
$this->assertTrue($this->testUser->hasAnyRole($roleModel->all()));
$this->assertTrue($this->testUser->hasAnyRole('testRole'));
$this->assertFalse($this->testUser->hasAnyRole('role does not exist'));
$this->assertTrue($this->testUser->hasAnyRole(['testRole']));
$this->assertTrue($this->testUser->hasAnyRole(['testRole', 'role does not exist']));
$this->assertFalse($this->testUser->hasAnyRole(['role does not exist']));
$this->assertTrue($this->testUser->hasAnyRole('testRole', 'role does not exist'));
}

The problem must be in your code somewhere. Please post your code.

I have the same problem. It only works by passing a string as a parameter

Actually, I always just use hasRole([array]) because hasAnyRole is essentially an alias to hasRole, but without processing guards.

@germanio7 wrote:

I have the same problem. It only works by passing a string as a parameter

What did you pass as an array? Was it just an array of strings? or a collection? or objects?
And how many guards do you have in your application? Did it fail because it checked against a different guard?