Skip to content

Commit

Permalink
Merge pull request #1528 from christianvogt/groups-rbac
Browse files Browse the repository at this point in the history
when fetching groups, capture error 403 as rbac failure and cache result
  • Loading branch information
openshift-merge-robot authored Jul 18, 2023
2 parents d48157a + 14f4db3 commit fffdd02
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions frontend/src/pages/projects/projectSharing/useGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ import * as React from 'react';
import useFetchState, { FetchState } from '~/utilities/useFetchState';
import { listGroups } from '~/api';
import { GroupKind } from '~/k8sTypes';
import { useUser } from '~/redux/selectors';

const useGroups = (): FetchState<GroupKind[]> => {
const { isAdmin } = useUser();
const is403 = React.useRef(false);

const getGroup = React.useCallback(() => {
if (!isAdmin) {
return Promise.resolve([]);
}
return listGroups().catch((e) => {
if (e.statusObject?.code === 404) {
throw new Error('No groups found.');
}
throw e;
});
}, [isAdmin]);
const getGroup = React.useCallback(
async () =>
is403.current
? []
: listGroups().catch((e) => {
if (e.statusObject?.code === 403) {
is403.current = true;
return [];
}
if (e.statusObject?.code === 404) {
throw new Error('No groups found.');
}
throw e;
}),
[],
);

return useFetchState<GroupKind[]>(getGroup, []);
};
Expand Down

0 comments on commit fffdd02

Please sign in to comment.