Skip to content

Commit a18c911

Browse files
committed
docs: Update documentation for including hierarchical permission system
1 parent 6e087d0 commit a18c911

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

docs/references/authorization.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public string $defaultGroup = 'user';
3636
## Defining Available Permissions
3737

3838
All permissions must be added to the `AuthGroups` config file, also. A permission is simply a string consisting of
39-
a scope and action, like `users.create`. The scope would be `users` and the action would be `create`. Each permission
40-
can have a description for display within UIs if needed.
39+
a scope and action, like `users.create`. The scope would be `users` and the action would be `create`. A scope can
40+
have sub-scopes, as in `forum.posts.delete`, where the scope is `forum`, the sub-scope is `posts`, and the action
41+
associated with this permission is `delete`. Each permission can have a description for display within UIs if needed.
4142

4243
```php
4344
public array $permissions = [
@@ -47,7 +48,10 @@ public array $permissions = [
4748
'users.create' => 'Can create new non-admin users',
4849
'users.edit' => 'Can edit existing non-admin users',
4950
'users.delete' => 'Can delete existing non-admin users',
50-
'beta.access' => 'Can access beta-level features'
51+
'beta.access' => 'Can access beta-level features',
52+
'forum.posts.create' => 'Can create posts in the forum',
53+
'forum.posts.edit' => 'Can edit posts in the forum',
54+
'forum.posts.delete' => 'Can delete posts in the forum',
5155
];
5256
```
5357

@@ -68,16 +72,17 @@ public array $matrix = [
6872
'admin' => [
6973
'admin.access',
7074
'users.create', 'users.edit', 'users.delete',
71-
'beta.access'
75+
'beta.access',
76+
'forum.posts.create', 'forum.posts.edit', 'forum.posts.delete',
7277
],
7378
];
7479
```
7580

76-
You can use a wildcard within a scope to allow all actions within that scope, by using a `*` in place of the action.
81+
You can use a wildcard within a scope or sub-scope to allow all actions within that scope or sub-scope, by using a `*` in place of the action.
7782

7883
```php
7984
public array $matrix = [
80-
'superadmin' => ['admin.*', 'users.*', 'beta.*'],
85+
'superadmin' => ['admin.*', 'users.*', 'beta.*', 'forum.posts.*'],
8186
];
8287
```
8388

@@ -87,9 +92,11 @@ The `Authorizable` trait on the `User` entity provides the following methods to
8792

8893
#### can()
8994

90-
Allows you to check if a user is permitted to do a specific action or group or actions. The permission string(s) should be passed as the argument(s). Returns
95+
Allows you to check if a user is permitted to do a specific action or group of actions. The permission string(s) should be passed as the argument(s). Returns
9196
boolean `true`/`false`. Will check the user's direct permissions (**user-level permissions**) first, and then check against all of the user's groups
92-
permissions (**group-level permissions**) to determine if they are allowed.
97+
permissions (**group-level permissions**) to determine if they are allowed. When checking against group-level permissions, this includes evaluating
98+
hierarchical wildcard permissions. For example, if a user's group has the permission `forum.posts.*`, a check for `$user->can('forum.posts.create')`
99+
would return `true`.
93100

94101
```php
95102
if ($user->can('users.create')) {
@@ -100,8 +107,25 @@ if ($user->can('users.create')) {
100107
if ($user->can('users.create', 'users.edit')) {
101108
//
102109
}
110+
111+
// Example with hierarchical wildcard check.
112+
// Assuming the $user is in a group with 'forum.posts.*' permission.
113+
if ($user->can('forum.posts.create')) {
114+
// This will return true
115+
}
103116
```
104117

118+
When checking group-level permissions, Shield automatically creates a hierarchy check by examining parent permissions:
119+
120+
- For permission `forum.posts.create`, it checks: `forum.posts.create`, `forum.posts.*`, and `forum.*`
121+
- For permission `admin.settings`, it checks: `admin.settings` and `admin.*`
122+
123+
This allows for flexible permission management where broader permissions automatically grant access to more specific actions.
124+
125+
!!! warning
126+
127+
Be cautious when granting wildcard permissions, especially at high levels like `admin.*`, as they will grant access to any future permissions added under that scope.
128+
105129
#### inGroup()
106130

107131
Checks if the user is in one of the groups passed in. Returns boolean `true`/`false`.

0 commit comments

Comments
 (0)