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
This component, part of [the ra-rbac module](https://marmelab.com/ra-enterprise/modules/ra-rbac#ifcanaccess)<imgclass="icon"src="./img/premium.svg" />, relies on the `authProvider` to render its child only if the user has the right permissions. It accepts the following props:
8
+
This component, part of [the ra-rbac module](https://marmelab.com/ra-enterprise/modules/ra-rbac#ifcanaccess)<imgclass="icon"src="./img/premium.svg" />, renders its child only if the user has the right permissions.
9
9
10
-
-`action` (`string`, required): the action to check, e.g. 'read', 'list', 'export', 'delete', etc.
11
-
-`resource` (`string`, optional): the resource to check, e.g. 'users', 'comments', 'posts', etc. Falls back to the current resource context if absent.
12
-
-`record` (`object`, optional): 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" }`
13
-
-`fallback` (`ReactNode`, optional): The element to render when the user does not have the permission. Defaults to `null`.
10
+
## Usage
14
11
15
-
Additional props are passed down to the child element.
12
+
Wrap the components that you want to add access control to with the `<IfCanAccess>` component.
13
+
14
+
For example, to display action buttons for a company record only if the user has the right permissions:
## Showing An Access Denied Message Instead Of A Not Found Page
32
+
With this code and the following user permissions:
37
33
38
-
`ra-rbac` shows a Not Found page when users try to access a page they don't have the permissions for. It is considered good security practice not to disclose to a potentially malicious user that a page exists if they are not allowed to see it.
However, should you prefer to show an Access Denied screen in those cases, you can do so by using the `Resource` component from `react-admin` instead of the one from `ra-rbac` and leveraging the `IfCanAccess` component in your views:
42
+
The `CompanyRecordToolbar` component will render the `<EditButton>` but not the `<DeleteButton>`.
|`action`| Required |`string`|| The action to check, e.g. 'read', 'list', 'export', 'delete', etc. |
49
+
|`resource`| Optional |`string`|| The resource to check, e.g. 'users', 'comments', 'posts', etc. Falls back to the current resource context if absent. |
50
+
|`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" }`|
51
+
|`fallback`| Optional |`ReactNode`|`null`| The element to render when the user does not have the permission. Defaults to `null`. |
54
52
55
-
// in src/AccessDenied.tsx
56
-
exportconst AccessDenied = () => (
57
-
<Typography>You don't have the required permissions to access this page.</Typography>
58
-
);
53
+
Additional props are passed down to the child element.
54
+
55
+
## `action`
59
56
57
+
The `action` prop allows you to restrict a component to users who have a permission to use the specified action on the current resource.
58
+
59
+
For instance, if the user has the following permissions:
To display the `ExportButton` in a `CompanyList` component, you would use:
70
+
71
+
```jsx
72
+
<IfCanAccess action="export">
73
+
<ExportButton />
74
+
</IfCanAccess>
75
+
```
76
+
77
+
## `resource`
78
+
79
+
By default, `<UseCanAccess>` uses the current resource (from the `ResourceContext`) to check permissions. You can override this behavior by passing the `resource` prop:
RBAC allows to specify [record-level permissions](./AuthRBAC.md#record-level-permissions). These permissions are triggered when you specify the `record` prop.
90
+
91
+
For example, let's say a user has the permission to edit a company only if the company is in the same group as the user:
To display the `EditButton` in a `CompanyShow` component, you would use:
101
+
102
+
```jsx
103
+
constEditCompanyButton= () => {
104
+
constrecord=useRecordContext();
105
+
return (
106
+
<IfCanAccess action="edit" record={record}>
107
+
<EditButton />
108
+
</IfCanAccess>
109
+
);
110
+
};
111
+
```
112
+
113
+
## `fallback`
114
+
115
+
`ra-rbac` shows a Not Found page when users try to access a page they don't have the permissions for. It is considered good security practice not to disclose to a potentially malicious user that a page exists if they are not allowed to see it.
116
+
117
+
However, should you prefer to show an Access Denied screen in those cases, you can do so by specifying a `fallback` component in `<IfCanAccess>`:
You can also choose to redirect users to a [custom route](https://marmelab.com/react-admin/CustomRoutes.html):
136
+
**Tip**: This example uses a `Navigate` component to redirect to a custom page because you cannot use a `Redirect` component in this context. The `IfCanAccess` component uses a render prop, and `Redirect` only works in the render method of a component.
137
+
138
+
Note that you if you use the `fallback` prop for a CRUD page (Create, Edit, List, Show) as above, you must use the `<Resource>` component from `react-admin` rather than the one from `ra-rbac`. This is because `ra-rbac` already does the access control check, and would redirect to the Not Found page before the fallback component is rendered.
0 commit comments