From cf06665bd294fd59d8390e0cd6940d9e5b368322 Mon Sep 17 00:00:00 2001 From: Georgy Karataev Date: Sun, 13 Aug 2023 22:59:42 +0200 Subject: [PATCH] fix: Fix usage of null values in RBAC RDs Make sure null values are not left out when RBAC RD value is an array. --- packages/components/src/RBACProvider/RBACProvider.tsx | 6 +++--- packages/utils/src/RBAC/RBAC.ts | 5 ++--- packages/utils/src/RBACHook/RBACHook.ts | 8 ++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/components/src/RBACProvider/RBACProvider.tsx b/packages/components/src/RBACProvider/RBACProvider.tsx index 711a76d2c2..4a6d78d1fc 100644 --- a/packages/components/src/RBACProvider/RBACProvider.tsx +++ b/packages/components/src/RBACProvider/RBACProvider.tsx @@ -11,10 +11,10 @@ import { } from '@redhat-cloud-services/frontend-components-utilities/RBAC'; const hasAccessWithUserPermissions = (userPermissions: (Access | string)[], checkResourceDefinitions: boolean) => { - return (requiredPermissions: (Access | string)[], checkAll?: boolean): boolean => { + return (requiredPermissions: (Access | string)[], checkAll?: boolean, checkResourceDefinitionsOverride?: boolean): boolean => { return checkAll - ? hasAllPermissions(userPermissions, requiredPermissions, checkResourceDefinitions) - : doesHavePermissions(userPermissions, requiredPermissions, checkResourceDefinitions); + ? hasAllPermissions(userPermissions, requiredPermissions, checkResourceDefinitionsOverride ?? checkResourceDefinitions) + : doesHavePermissions(userPermissions, requiredPermissions, checkResourceDefinitionsOverride ?? checkResourceDefinitions); }; }; diff --git a/packages/utils/src/RBAC/RBAC.ts b/packages/utils/src/RBAC/RBAC.ts index 15fa0e1df7..89644ae426 100644 --- a/packages/utils/src/RBAC/RBAC.ts +++ b/packages/utils/src/RBAC/RBAC.ts @@ -36,8 +36,7 @@ function extractResourceDefinitionValues(rds: ResourceDefinition[]) { if (operation === ResourceDefinitionFilterOperationEnum.In) { return [ ...acc, - ...value - .toString() + ...(Array.isArray(value) ? value.map((value) => (value === null ? 'null' : value)).toString() : value) .split(',') .map((value) => `${key}:${value}`), ]; @@ -135,7 +134,7 @@ export interface UsePermissionsContextState { isLoading?: boolean; isOrgAdmin: boolean; permissions: (string | Access)[]; - hasAccess?: (requiredPermissions: (Access | string)[], checkAll?: boolean) => boolean; + hasAccess?: (requiredPermissions: (Access | string)[], checkAll?: boolean, checkResourceDefinitionsOverride?: boolean) => boolean; } export const initialPermissions: UsePermissionsContextState = { diff --git a/packages/utils/src/RBACHook/RBACHook.ts b/packages/utils/src/RBACHook/RBACHook.ts index de90ba9f48..eff73716da 100644 --- a/packages/utils/src/RBACHook/RBACHook.ts +++ b/packages/utils/src/RBACHook/RBACHook.ts @@ -39,12 +39,16 @@ export function usePermissions( return permissions; } -export const usePermissionsWithContext = (requiredPermissions: (Access | string)[], checkAll?: boolean) => { +export const usePermissionsWithContext = ( + requiredPermissions: (Access | string)[], + checkAll?: boolean, + checkResourceDefinitionsOverride?: boolean +) => { const { hasAccess, ...permissionState } = useContext(RBACContext); return { ...permissionState, - hasAccess: hasAccess?.(requiredPermissions, checkAll) || false, + hasAccess: hasAccess?.(requiredPermissions, checkAll, checkResourceDefinitionsOverride) || false, }; };