Skip to content
Open
50 changes: 28 additions & 22 deletions frontend/src/components/common/Resource/AuthVisible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,54 +56,60 @@ export interface AuthVisibleProps extends React.PropsWithChildren<{}> {
export default function AuthVisible(props: AuthVisibleProps) {
const { item, authVerb, subresource, namespace, onError, onAuthResult, children } = props;

if (!VALID_AUTH_VERBS.includes(authVerb)) {
console.warn(`Invalid authVerb provided: "${authVerb}". Skipping authorization check.`);
return null;
}
const isValidVerb = VALID_AUTH_VERBS.includes(authVerb);

Comment thread
kashish00208 marked this conversation as resolved.
const itemClass: KubeObjectClass | null = (item as KubeObject)?._class?.() ?? item;
const itemName = (item as KubeObject)?.getName?.();
const cluster = (item as any)?.cluster;

// eslint-disable-next-line react-hooks/rules-of-hooks
const { data } = useQuery<any>({
enabled: !!item,
enabled: !!itemClass && isValidVerb && !!item,

queryKey: [
Comment thread
kashish00208 marked this conversation as resolved.
'authVisible',
cluster,
itemName,
itemClass.apiName,
itemClass.apiVersion,
itemClass?.apiName,
itemClass?.apiVersion,
Comment thread
kashish00208 marked this conversation as resolved.
Comment thread
kashish00208 marked this conversation as resolved.
Outdated
authVerb,
subresource,
namespace,
],
queryFn: async () => {
try {
const res = await item!.getAuthorization(
authVerb,
{ subresource, namespace },
(item as any).cluster
);
return res;
if (!item) {
return null;
}

Comment thread
kashish00208 marked this conversation as resolved.
return await item.getAuthorization(authVerb, { subresource, namespace }, cluster);
} catch (e: any) {
onError?.(e);
return null;
}
},
});

const visible = data?.status?.allowed ?? false;
const visible = isValidVerb && (data?.status?.allowed ?? false);

// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (data) {
onAuthResult?.({
allowed: visible,
reason: data.status?.reason ?? '',
});
if (!isValidVerb || !data) {
return;
}
onAuthResult?.({
allowed: visible,
reason: data.status?.reason ?? '',
});

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data]);
Comment thread
kashish00208 marked this conversation as resolved.

if (!visible) {
useEffect(() => {
if (!isValidVerb) {
console.warn(`Invalid authVerb provided: "${authVerb}". Skipping authorization check.`);
}
}, [isValidVerb, authVerb]);

if (!isValidVerb || !visible) {
return null;
}

Expand Down
Loading