Skip to content

Commit

Permalink
feat: Override resourceDefinitions within context
Browse files Browse the repository at this point in the history
Relates to https://issues.redhat.com/browse/ESSNTL-5239.

Complement hasAccess function with another parameter that allows to
override resource definitions check. The parameter ignores the value set
in the RBACContext which provides more flexibility for some edge cases.
  • Loading branch information
gkarat authored and Hyperkid123 committed Aug 15, 2023
1 parent 43411bb commit c87f70c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/components/src/RBACProvider/RBACProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/RBAC/RBAC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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 = {
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/RBACHook/RBACHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};

Expand Down

0 comments on commit c87f70c

Please sign in to comment.