Skip to content

Commit

Permalink
Remove multiple roles
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafamaklad committed Jan 25, 2018
1 parent d75a0c5 commit 2b625e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,23 @@ public function assignRole(...$roles)
/**
* Revoke the given role from the model.
*
* @param string|Role $role
* @param array|string|Role ...$roles
*
* @return HasRoles
*/
public function removeRole($role)
public function removeRole(...$roles)
{
$this->roles()->detach($this->getStoredRole($role));
\collect($roles)
->flatten()
->map(function ($role) {
$role = $this->getStoredRole($role);
$this->roles()->detach($this->getStoredRole($role));
return $role;
});

$this->forgetCachedPermissions();

return $this;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,17 @@ public function it_does_not_detach_roles_when_soft_deleting()
$user = SoftDeletingUser::withTrashed()->find($user->id);
$this->assertTrue($user->hasRole('testRole'));
}

/** @test */
public function it_can_give_and_revoke_multiple_roles()
{
$this->testUser->assignRole('testRole');
$this->testUser->assignRole('testRole2');

$this->testUser->removeRole('testRole', 'testRole2');

$this->assertFalse($this->testUser->hasRole('testRole'));

$this->assertFalse($this->testUser->hasRole('testRole2'));
}
}

0 comments on commit 2b625e0

Please sign in to comment.