Skip to content

Commit f69c7e2

Browse files
authored
Merge pull request #9396 from marmelab/doc-rbac-can-access-example-uses-resource-which-is-misleading
[Doc] Update canAccess doc
2 parents 0e22525 + f2a07fa commit f69c7e2

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

docs/canAccess.md

+37-21
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,23 @@ The `PostEditButton` component will render the `<EditButton>`.
5252

5353
## `action`
5454

55-
Specify the action you want to check.
55+
If you want to check a specific action, for instance if the user can `delete` a `post`, you can specify this action in the dedicated prop:
5656

5757
```tsx
5858
const permissions = [
59-
{ resource: 'posts', action: ['read', 'edit', 'create'] },
59+
{ resource: 'posts', action: ['read', 'edit', 'create', 'delete'] },
6060
];
6161
canAccess({ permissions, resource: 'posts', action: 'read' }); // true
6262
canAccess({ permissions, resource: 'posts', action: 'edit' }); // true
6363
canAccess({ permissions, resource: 'posts', action: 'create' }); // true
64-
canAccess({ permissions, resource: 'posts', action: 'delete' }); // false
64+
canAccess({ permissions, resource: 'posts', action: 'delete' }); // true
6565
canAccess({ permissions, resource: 'posts', action: 'export' }); // false
6666
```
6767

68-
You don't have to provide an `action` if you just want to know whether users can access the CRUD pages of a resource. This is useful to leverage `canAccess` in an `<Admin>` component children function:
68+
If you just want to know whether users can access any of the resources, you don't have to provide an `action`. For instance, here's how you may display different components depending on resources access rights in the dashboard:
6969

7070
```tsx
71-
import { Admin, Resource, ListGuesser, EditGuesser } from 'react-admin';
71+
import { Admin, usePermissions } from 'react-admin';
7272
import { canAccess } from '@react-admin/ra-rbac';
7373
import { dataProvider } from './dataProvider';
7474

@@ -84,28 +84,44 @@ const authProvider = {
8484
]),
8585
};
8686

87+
const AccessDashboard = () => {
88+
const { permissions } = usePermissions();
89+
return (
90+
<>
91+
{canAccess({
92+
permissions,
93+
resource: 'commands',
94+
}) ? (
95+
<>List of last orders...</> // no access to this component
96+
) : null}
97+
98+
{canAccess({
99+
permissions,
100+
resource: 'products',
101+
}) ? (
102+
<>List of last products...</>
103+
) : null}
104+
{canAccess({
105+
permissions,
106+
resource: 'categories',
107+
}) ? (
108+
<>List of last categories...</>
109+
) : null}
110+
</>
111+
);
112+
};
113+
87114
export const MyApp = () => (
88-
<Admin authProvider={authProvider} dataProvider={dataProvider}>
89-
{(permissions: Permissions) => (
90-
<>
91-
{canAccess({ permissions, resource: 'products' }) ? (
92-
<Resource name="products" list={ListGuesser} />
93-
) : null}
94-
{canAccess({ permissions, resource: 'categories' }) ? (
95-
<Resource name="categories" list={ListGuesser} edit={EditGuesser} />
96-
) : null}
97-
{canAccess({ permissions, resource: 'commands' }) ? (
98-
<Resource name="commands" list={ListGuesser} />
99-
) : null}
100-
</>
101-
)}
115+
<Admin authProvider={authProvider} dataProvider={dataProvider} dashboard={AccessDashboard}>
116+
{/*...*/}
102117
</Admin>
103118
);
104119
```
105120

106-
In this example, users will see the products list and will be able to click on its category link to edit the category. However, they won't see the categories list nor the commands list.
121+
In this example, users will see the list of last products and the list of last categories but they won't be able to see the list of last orders.
122+
107123

108-
Note that [ra-rbac's `<Resource>` component](./AuthRBAC.md#resource) does this check automatically, so you don't actually need to use `canAccess` in this case.
124+
**Note**: [ra-rbac's `<Resource>` component](./AuthRBAC.md#resource) automatically checks for the `list`, `show`, `create` and `edit` actions, so you don't actually need to use `canAccess` if you want to restrict a whole resource.
109125

110126
```tsx
111127
import { Admin, ListGuesser, EditGuesser } from 'react-admin'; // do not import Resource here

0 commit comments

Comments
 (0)