-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Doc] Improve Access Control for Custom Pages #10357
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,30 +253,59 @@ const CommentsToolbar = ({ record }) => ( | |
|
||
### Custom Routes | ||
|
||
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>`: | ||
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>`: | ||
|
||
```tsx | ||
import { Admin, CustomRoutes, Authenticated, CanAccess } from 'react-admin'; | ||
import { Authenticated, CanAccess, AccessDenied } from 'react-admin'; | ||
|
||
export const LogsPage = () => ( | ||
<Authenticated> | ||
<CanAccess resource="logs" action="read" accessDenied={<AccessDenied />}> | ||
... | ||
</CanAccess> | ||
</Authenticated> | ||
); | ||
``` | ||
|
||
Use the [`<CustomRoutes>`](./CustomRoutes.md) component to add custom routes to your admin. | ||
|
||
```tsx | ||
import { Admin, CustomRoutes, Authenticated, CanAccess, AccessDenied, Layout } from 'react-admin'; | ||
import { Route } from 'react-router-dom'; | ||
|
||
import { LogsPage } from './LogsPage'; | ||
import { MyMenu } from './MyMenu'; | ||
|
||
const MyLayout = (props) => <Layout {...props} menu={MyMenu} />; | ||
|
||
const App = () => ( | ||
<Admin authProvider={authProvider}> | ||
<Admin authProvider={authProvider} layout={MyLayout}> | ||
<CustomRoutes> | ||
<Route path="/restricted" element={ | ||
<Authenticated> | ||
<CanAccess action="read" resource="restricted"> | ||
<RestrictedPage /> | ||
</CanAccess> | ||
</Authenticated> | ||
} /> | ||
<Route path="/logs" element={<LogsPage />} /> | ||
</CustomRoutes> | ||
</Admin> | ||
); | ||
``` | ||
|
||
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. | ||
|
||
```tsx | ||
import { Menu, CanAccess } from "react-admin"; | ||
import SsidChartIcon from "@mui/icons-material/SsidChart"; | ||
|
||
export const MyMenu = () => ( | ||
<Menu> | ||
<Menu.ResourceItems /> | ||
<CanAccess resource="logs" action="read"> | ||
<Menu.Item primaryText="Logs" to="/logs" leftIcon={<SsidChartIcon />} /> | ||
</CanAccess> | ||
</Menu> | ||
); | ||
``` | ||
|
||
**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. | ||
|
||
**Note**: You don't need to use `<CanAccess>` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth). | ||
**Note**: You don't need to use `<Authenticated>` on custom pages if your admin uses [`requireAuth`](./Admin.md#requireauth). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move that tip below the example that uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that, too, but I feel that it interrupts the flow of the explanation for a particular case. So I'm -1 for this change. |
||
|
||
## Permissions | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The important part is the
accessDenied={<AccessDenied />}
part, which was missing.