Skip to content

Commit

Permalink
Merge pull request #442 from drbyte/add-getRoleNames
Browse files Browse the repository at this point in the history
Add getRoleNames() method to give a collection of assigned roles
  • Loading branch information
drbyte authored Sep 2, 2017
2 parents f89cf02 + 03e9456 commit f81348e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `laravel-permission` will be documented in this file

## 2.5.1 - 2017-09-02
- add getRoleNames() method to return a collection of assigned roles

## 2.5.0 - 2017-08-30
- add compatiblity with Laravel 5.5

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ $permissions = $user->permissions;
$permissions = $user->getAllPermissions();

// get a collection of all defined roles
$roles = $user->roles->pluck('name'); // Returns a collection
$roles = $user->getRoleNames(); // Returns a collection
```

The `HasRoles` trait also adds a scope to your models to scope the query to certain roles:
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/HasRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ public function getAllPermissions(): Collection
->values();
}

public function getRoleNames(): Collection
{
return $this->roles->pluck('name');
}

protected function getStoredRole($role): Role
{
if (is_string($role)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,15 @@ public function it_can_list_all_the_coupled_permissions_both_directly_and_via_ro
$this->testUser->getAllPermissions()->pluck('name')
);
}

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

$this->assertEquals(
collect(['testRole', 'testRole2']),
$this->testUser->getRoleNames()
);
}
}

0 comments on commit f81348e

Please sign in to comment.