Skip to content

Commit e21486c

Browse files
authoredMay 30, 2023
Merge pull request #8955 from mmateja/refetch-permissions
Add ability to refetch permissions on demand
2 parents 66f77ca + 3349c8c commit e21486c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎docs/usePermissions.md

+28
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ const MyPage = () => {
6161
}
6262
```
6363

64+
## Refreshing permissions
65+
66+
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.
67+
68+
{% raw %}
69+
```jsx
70+
const GrantAdminPermissionsButton = () => {
71+
const record = useRecordContext();
72+
const [ update ] = useUpdate();
73+
const { refetch } = usePermissions();
74+
75+
const handleClick = () => {
76+
update(
77+
"users",
78+
{ id: record.id, data: { admin: true }, previousData: record },
79+
{ onSuccess: refetch },
80+
);
81+
}
82+
83+
return (
84+
<Button onClick={handleClick}>
85+
Make user an admin
86+
</Button>
87+
)
88+
}
89+
```
90+
{% endraw %}
91+
6492
## RBAC
6593

6694
[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.

‎packages/ra-core/src/auth/usePermissions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const emptyParams = {};
1919
*
2020
* @param {Object} params Any params you want to pass to the authProvider
2121
*
22-
* @returns The current auth check state. Destructure as { permissions, error, isLoading }.
22+
* @returns The current auth check state. Destructure as { permissions, error, isLoading, refetch }.
2323
*
2424
* @example
2525
* import { usePermissions } from 'react-admin';
@@ -54,6 +54,7 @@ const usePermissions = <Permissions = any, Error = any>(
5454
permissions: result.data,
5555
isLoading: result.isLoading,
5656
error: result.error,
57+
refetch: result.refetch,
5758
}),
5859
[result]
5960
);

0 commit comments

Comments
 (0)
Please sign in to comment.