Skip to content

Commit f38d3d7

Browse files
committed
commit
1 parent 5a31291 commit f38d3d7

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All Notable changes to `laravel-permission` will be documented in this file
44

5+
## unreleased
6+
7+
- add `getDirectPermissions`, `getPermissionsViaRoles`, `getAllPermissions`
8+
59
## 1.10.0 - 2017-02-22
610

711
- add `hasAnyPermission`

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,19 @@ In above example a role is given permission to edit articles and this role is as
324324

325325
This method is useful if one has a form for setting permissions for roles and users in his application and want to restrict to change inherited permissions of roles of user, i.e. allowing to change only direct permissions of user.
326326

327-
You can also list all of theses permissions:
327+
You can list all of theses permissions:
328+
328329
```php
329330
//direct permissions
330-
$user->permissions
331+
$user->getDirectPermissions() // or $user->permissions;
331332

332333
//permissions inherited from user's roles
333-
$user->permissions_via_roles
334+
$user->getPermissionsViaRoles();
334335

335336
//all permissions which apply on the user
336-
$user->all_permissions
337+
$user->getAllPermissions();
337338
```
339+
338340
All theses responses are collections of `Spatie\Permission\Models\Permission`-objects.
339341

340342
If we follow the previous example, the first response will be a collection with the 'delete article' permission, the second will be a collection with the 'edit article' permission and the third will contain both.

src/Traits/HasRoles.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,21 @@ protected function getStoredRole($role)
279279
}
280280

281281
/**
282-
* Return all the permissions the user have via his roles.
282+
* Return all permissions the directory coupled to the user.
283283
*
284-
* @return Collection
284+
* @return \Illuminate\Support\Collection
285285
*/
286-
protected function getPermissionsViaRolesAttribute()
286+
public function getDirectPermissions()
287+
{
288+
return $this->permissions;
289+
}
290+
291+
/**
292+
* Return all the permissions the user has via roles.
293+
*
294+
* @return \Illuminate\Support\Collection
295+
*/
296+
public function getPermissionsViaRoles()
287297
{
288298
return $this->load('roles', 'roles.permissions')
289299
->roles->flatMap(function ($role) {
@@ -292,12 +302,12 @@ protected function getPermissionsViaRolesAttribute()
292302
}
293303

294304
/**
295-
* Return all the permissions the user have (directly and via his roles).
305+
* Return all the permissions the user has, both directly and via roles.
296306
*
297-
* @return Collection
307+
* @return \Illuminate\Support\Collection
298308
*/
299-
protected function getAllPermissionsAttribute()
309+
public function getAllPermissions()
300310
{
301-
return $this->permissions->merge($this->permissions_via_roles)->sort()->values();
311+
return $this->permissions->merge($this->getPermissionsViaRoles())->sort()->values();
302312
}
303313
}

tests/HasRolesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ public function it_can_determine_that_user_has_direct_permission()
394394
}
395395

396396
/** @test */
397-
public function it_can_determine_that_user_can_list_all_the_permissions_he_has_via_his_roles()
397+
public function it_can_list_all_the_permissions_via_his_roles()
398398
{
399399
$roleModel = app(Role::class);
400400
$roleModel->findByName('testRole2')->givePermissionTo('edit-news');
@@ -404,12 +404,12 @@ public function it_can_determine_that_user_can_list_all_the_permissions_he_has_v
404404

405405
$this->assertEquals(
406406
collect(['edit-articles', 'edit-news']),
407-
$this->testUser->permissions_via_roles->pluck('name')
407+
$this->testUser->getPermissionsViaRoles()->pluck('name')
408408
);
409409
}
410410

411411
/** @test */
412-
public function it_can_determine_that_user_can_list_all_his_permissions()
412+
public function it_can_list_all_the_coupled_permissions_both_directly_and_via_roles()
413413
{
414414
$this->testUser->givePermissionTo('edit-news');
415415

@@ -418,7 +418,7 @@ public function it_can_determine_that_user_can_list_all_his_permissions()
418418

419419
$this->assertEquals(
420420
collect(['edit-articles', 'edit-news']),
421-
$this->testUser->all_permissions->pluck('name')
421+
$this->testUser->getAllPermissions()->pluck('name')
422422
);
423423
}
424424

0 commit comments

Comments
 (0)