Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions docs/authorization.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
- [can()](#can)
- [inGroup()](#ingroup)
- [hasPermission()](#haspermission)
- [Authorizing via Filters](#authorizing-via-filters)
- [Authorizing via Routes](#authorizing-via-routes)
- [Managing User Permissions](#managing-user-permissions)
- [addPermission()](#addpermission)
Expand Down Expand Up @@ -130,28 +129,34 @@ if (! $user->hasPermission('users.create')) {
}
```

#### Authorizing via Filters
#### Authorizing via Routes

You can restrict access to multiple routes through a [Controller Filter](https://codeigniter.com/user_guide/incoming/filters.html). One is provided for both restricting via groups the user belongs to, as well as which permission they need. The filters are automatically registered with the system under the `group` and `permission` aliases, respectively. You can define the protections within **app/Config/Filters.php**:
You can restrict access to a route or route group through a
[Controller Filter](https://codeigniter.com/user_guide/incoming/filters.html).

```php
public $filters = [
'group:admin,superadmin' => ['before' => ['admin/*']],
'permission:users.manage' => ['before' => ['admin/users/*']],
];
```
One is provided for restricting via groups the user belongs to, the other
is for permission they need. The filters are automatically registered with the
system under the `group` and `permission` aliases, respectively.

#### Authorizing via Routes

The filters can also be used on a route or route group level:
You can set the filters within **app/Config/Routes.php**:

```php
$routes->group('admin', ['filter' => 'group:admin,superadmin'], static function ($routes) {
$routes->resource('users');
$routes->group(
'',
['filter' => ['group:admin,superadmin', 'permission:users.manage']],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
['filter' => ['group:admin,superadmin', 'permission:users.manage']],
['filter' => ['group:admin,superadmin', 'permission:users.manage-admins']],

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permission:users.manage is not listed in https://github.com/codeigniter4/shield/blob/develop/docs/authorization.md#defining-available-permissions
But in this case, I feel permission:users.manage-admins is a bit weird.
The URI seems the page to manage users, not admin-users.

static function ($routes) {
$routes->resource('users');
}
);
});

```

Note that the options (`filter`) passed to the outer `group()` are not merged with the inner `group()` options.

> **Note** If you set more than one filter to a route, you need to enable
> [Multiple Filters](https://codeigniter.com/user_guide/incoming/routing.html#multiple-filters).

## Managing User Permissions

Permissions can be granted on a user level as well as on a group level. Any user-level permissions granted will
Expand Down