Merge pull request #17333 from strapi/fix/use-rbac-recalc

This commit is contained in:
Josh 2023-07-17 14:58:13 +01:00 committed by GitHub
commit e70c18d5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 1 deletions

View File

@ -137,6 +137,95 @@ describe('useRBAC', () => {
}
`);
});
/**
* There may be cases where permissionsToCheck and the passed Permissions update at different times
* so therefore we can't just depend on permissionsToCheck to recalculate the permissions but we also
* need to recalculate the permissions when the permissions argument changes.
*/
it('should recalculate the permissions the permissions argument changes', async () => {
const { result, rerender } = renderHook(
({ permissions, permissionsToCheck }) => {
return useRBAC(permissionsToCheck, permissions);
},
{
initialProps: {
permissionsToCheck: {
create: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::about.about',
},
],
},
permissions: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::about.about',
},
],
},
}
);
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.allowedActions).toMatchInlineSnapshot(`
{
"canCreate": true,
}
`);
rerender({
permissionsToCheck: {
create: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::term.term',
},
],
},
permissions: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::about.about',
},
],
});
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.allowedActions).toMatchInlineSnapshot(`
{
"canCreate": false,
}
`);
rerender({
permissionsToCheck: {
create: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::term.term',
},
],
},
permissions: [
{
action: 'plugin::content-manager.explorer.update',
subject: 'api::term.term',
},
],
});
await waitFor(() => expect(result.current.isLoading).toBe(false));
expect(result.current.allowedActions).toMatchInlineSnapshot(`
{
"canCreate": true,
}
`);
});
});
describe('checking against the server if there are conditions in the permissions', () => {

View File

@ -40,7 +40,7 @@ export const useRBAC = (permissionsToCheck, passedPermissions) => {
const queryResults = useQueries(
permissionsToCheckEntries.map(([name, permissions]) => ({
queryKey: ['useRBAC', name, permissions.map(({ action, subject }) => ({ action, subject }))],
queryKey: ['useRBAC', name, permissions, userPermissions],
async queryFn() {
if (!permissions || !permissions.length) {
return { name, hasPermission: true };