You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/useCanAccess.md
+15-2Lines changed: 15 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,8 @@ title: "useCanAccess"
7
7
8
8
This hook, part of [the ra-rbac module](https://marmelab.com/ra-enterprise/modules/ra-rbac)<imgclass="icon"src="./img/premium.svg" />, calls the `authProvider.getPermissions()` to get the role definitions, then checks whether the requested action and resource are allowed for the current user.
9
9
10
+
## Usage
11
+
10
12
`useCanAccess` takes an object `{ action, resource, record }` as argument. It returns an object describing the state of the RBAC request. As calls to the `authProvider` are asynchronous, the hook returns a `loading` state in addition to the `canAccess` key.
**Tip**: The *order* of permissions as returned by the `authProvider` isn't significant. As soon as at least one permission grants access to an action on a resource, the user will be able to perform it.
58
60
59
61
**Tip**: `useCanAccess` is asynchronous, because it calls `usePermissions` internally. If you have to use `useCanAccess` several times in a component, the rendered result will "blink" as the multiple calls to `authProvider.getPermissions()` resolve. To avoid that behavior, you can use the `usePermissions` hook once, then call [the `canAccess` helper](./canAccess.md).
62
+
63
+
## Parameters
64
+
65
+
`useCanAccess` expects a single parameter object with the following properties:
66
+
67
+
| Name | Required | Type | Default | Description |
68
+
| --- | --- | --- | --- | --- |
69
+
|`resource`| Required |`string`| - | The resource to check, e.g. 'users', 'comments', 'posts', etc. |
70
+
|`action`| Required |`string`| - | The action to check, e.g. 'read', 'list', 'export', 'delete', etc. |
71
+
|`record`| Optional |`object`| - | The record to check. If passed, the child only renders if the user has permissions for that record, e.g. `{ id: 123, firstName: "John", lastName: "Doe" }`|
0 commit comments