Skip to content
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

Add ability to refetch permissions on demand #8955

Merged
merged 5 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/usePermissions.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ const MyPage = () => {
}
```

## Refreshing permissions

Permissions are loaded when the app loads and then cached. If your application requires permissions to be refreshed, for example after a change modifying user permissions, you can use `refetch` function to trigger reload.

{% raw %}
```jsx
const GrantAdminPermissionsButton = () => {
const record = useRecordContext();
const [ update ] = useUpdate();
const { refetch } = usePermissions();

const handleClick = () => {
update(
"users",
{ id: record.id, data: { admin: true }, previousData: record },
{ onSuccess: refetch },
);
}

return (
<Button onClick={handleClick}>
Make user an admin
</Button>
)
}
```
{% endraw %}

## RBAC

[The ra-rbac module](https://marmelab.com/ra-enterprise/modules/ra-rbac)<img class="icon" src="./img/premium.svg" /> provides an alternative implementation of the `usePermissions` hook. It returns an array of permissions, resulting in the merge of the user permissions and the permissions from the user roles.
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-core/src/auth/usePermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const emptyParams = {};
*
* @param {Object} params Any params you want to pass to the authProvider
*
* @returns The current auth check state. Destructure as { permissions, error, isLoading }.
* @returns The current auth check state. Destructure as { permissions, error, isLoading, refetch }.
*
* @example
* import { usePermissions } from 'react-admin';
Expand Down Expand Up @@ -54,6 +54,7 @@ const usePermissions = <Permissions = any, Error = any>(
permissions: result.data,
isLoading: result.isLoading,
error: result.error,
refetch: result.refetch,
}),
[result]
);
Expand Down