Skip to content

Commit 4357cc1

Browse files
authored
Merge pull request marmelab#10357 from marmelab/doc-access-control-custom-routes
[Doc] Improve Access Control for Custom Pages
2 parents c0012c7 + c6b1d50 commit 4357cc1

File tree

2 files changed

+95
-11
lines changed

2 files changed

+95
-11
lines changed

docs/CanAccess.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,59 @@ const UserEdit = () => (
4242
| `accessDenied` | | `ReactNode` | - | The element displayed when users are denied access to the resource |
4343
| `error` | | `ReactNode` | - | The element displayed when an error occurs while calling `authProvider.canAccess` |
4444

45+
## Securing Custom Routes
46+
47+
By default, there is no authentication or authorization control on custom routes. If you need to restrict access to a custom route, wrap the content with `<CanAccess>`. Remember to check the authentication status before with `<Authenticated>`:
48+
49+
```tsx
50+
import { Authenticated, CanAccess, AccessDenied } from 'react-admin';
51+
52+
export const LogsPage = () => (
53+
<Authenticated>
54+
<CanAccess resource="logs" action="read" accessDenied={<AccessDenied />}>
55+
...
56+
</CanAccess>
57+
</Authenticated>
58+
);
59+
```
60+
61+
Use the [`<CustomRoutes>`](./CustomRoutes.md) component to add custom routes to your admin.
62+
63+
```tsx
64+
import { Admin, CustomRoutes, Authenticated, CanAccess, AccessDenied, Layout } from 'react-admin';
65+
import { Route } from 'react-router-dom';
66+
67+
import { LogsPage } from './LogsPage';
68+
import { MyMenu } from './MyMenu';
69+
70+
const MyLayout = (props) => <Layout {...props} menu={MyMenu} />;
71+
72+
const App = () => (
73+
<Admin authProvider={authProvider} layout={MyLayout}>
74+
<CustomRoutes>
75+
<Route path="/logs" element={<LogsPage />} />
76+
</CustomRoutes>
77+
</Admin>
78+
);
79+
```
80+
81+
Remember to also wrap your [custom menu items](./Menu.md) with `<CanAccess>` to hide the menu items if the user doesn't have access to the resource.
82+
83+
```tsx
84+
import { Menu, CanAccess } from "react-admin";
85+
import SsidChartIcon from "@mui/icons-material/SsidChart";
86+
87+
export const MyMenu = () => (
88+
<Menu>
89+
<Menu.ResourceItems />
90+
<CanAccess resource="logs" action="read">
91+
<Menu.Item primaryText="Logs" to="/logs" leftIcon={<SsidChartIcon />} />
92+
</CanAccess>
93+
</Menu>
94+
);
95+
```
96+
97+
**Note**: You don't need to use `<CanAccess>` on the core react-admin page components (`<List>`, `<Create>`, `<Edit>`, `<Show>`) because they already have built-in access control.
98+
99+
**Note**: You don't need to use `<Authenticated>` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth).
45100

docs/Permissions.md

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,30 +253,59 @@ const CommentsToolbar = ({ record }) => (
253253

254254
### Custom Routes
255255

256-
By default, there is no authentication or authorization control on the custom routes. If you need to restrict access to a custom route, you can use the `<CanAccess>` component. Remember to check the authentication status before with `<Authenticated>`:
256+
By default, there is no authentication or authorization control on custom routes. If you need to restrict access to a custom route, you can use the `<CanAccess>` component. Remember to check the authentication status before with `<Authenticated>`:
257257

258258
```tsx
259-
import { Admin, CustomRoutes, Authenticated, CanAccess } from 'react-admin';
259+
import { Authenticated, CanAccess, AccessDenied } from 'react-admin';
260+
261+
export const LogsPage = () => (
262+
<Authenticated>
263+
<CanAccess resource="logs" action="read" accessDenied={<AccessDenied />}>
264+
...
265+
</CanAccess>
266+
</Authenticated>
267+
);
268+
```
269+
270+
Use the [`<CustomRoutes>`](./CustomRoutes.md) component to add custom routes to your admin.
271+
272+
```tsx
273+
import { Admin, CustomRoutes, Authenticated, CanAccess, AccessDenied, Layout } from 'react-admin';
260274
import { Route } from 'react-router-dom';
261275

276+
import { LogsPage } from './LogsPage';
277+
import { MyMenu } from './MyMenu';
278+
279+
const MyLayout = (props) => <Layout {...props} menu={MyMenu} />;
280+
262281
const App = () => (
263-
<Admin authProvider={authProvider}>
282+
<Admin authProvider={authProvider} layout={MyLayout}>
264283
<CustomRoutes>
265-
<Route path="/restricted" element={
266-
<Authenticated>
267-
<CanAccess action="read" resource="restricted">
268-
<RestrictedPage />
269-
</CanAccess>
270-
</Authenticated>
271-
} />
284+
<Route path="/logs" element={<LogsPage />} />
272285
</CustomRoutes>
273286
</Admin>
274287
);
275288
```
276289

290+
Remember to also wrap your [custom menu items](./Menu.md) with `<CanAccess>` to hide the menu items if the user doesn't have access to the resource.
291+
292+
```tsx
293+
import { Menu, CanAccess } from "react-admin";
294+
import SsidChartIcon from "@mui/icons-material/SsidChart";
295+
296+
export const MyMenu = () => (
297+
<Menu>
298+
<Menu.ResourceItems />
299+
<CanAccess resource="logs" action="read">
300+
<Menu.Item primaryText="Logs" to="/logs" leftIcon={<SsidChartIcon />} />
301+
</CanAccess>
302+
</Menu>
303+
);
304+
```
305+
277306
**Note**: You don't need to use `<CanAccess>` on the core react-admin page components (`<List>`, `<Create>`, `<Edit>`, `<Show>`) because they already have built-in access control.
278307

279-
**Note**: You don't need to use `<CanAccess>` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth).
308+
**Note**: You don't need to use `<Authenticated>` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth).
280309

281310
## Permissions
282311

0 commit comments

Comments
 (0)