Skip to content

Commit

Permalink
fix(RBAC): Correct type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
bastilian committed Aug 1, 2022
1 parent d6d69d9 commit 8ad0b19
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions packages/components/src/RBACProvider/RBACProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { Access } from '@redhat-cloud-services/rbac-client';
import { Bullseye, Spinner } from '@patternfly/react-core';
import { Access } from '@redhat-cloud-services/rbac-client';

import {
RBACContext,
Expand All @@ -11,7 +11,7 @@ import {
} from '@redhat-cloud-services/frontend-components-utilities/RBAC';

const hasAccessWithUserPermissions = (userPermissions: (Access | string)[]) => {
return (requiredPermissions: string[], checkAll = true): boolean => {
return (requiredPermissions: (Access | string)[], checkAll = true): boolean => {
return checkAll ? hasAllPermissions(userPermissions, requiredPermissions) : doesHavePermissions(userPermissions, requiredPermissions);
};
};
Expand All @@ -26,7 +26,7 @@ export const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appNa
const fetchPermissions = async () => {
const { isOrgAdmin, permissions: userPermissions } = await getRBAC(appName, true);

setPermissionState((currentPerms: any) => ({
setPermissionState((currentPerms) => ({
...currentPerms,
isLoading: false,
isOrgAdmin,
Expand Down
28 changes: 16 additions & 12 deletions packages/utils/src/RBAC/RBAC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function isAccessType(permission: Access | string): permission is Access {
return typeof permission === 'object';
}

export function doesHavePermissions(userPermissions: (Access | string)[], permissionList: string[]): boolean {
export function doesHavePermissions(userPermissions: (Access | string)[], permissionList: (Access | string)[]): boolean {
if (!userPermissions) {
return false;
}
Expand All @@ -39,15 +39,15 @@ export function doesHavePermissions(userPermissions: (Access | string)[], permis
});
}

export function hasAllPermissions(userPermissions: (Access | string)[], permissionList: string[]): boolean {
export function hasAllPermissions(userPermissions: (Access | string)[], permissionList: (Access | string)[]): boolean {
if (!userPermissions) {
return false;
}

return permissionList.every((permission) => {
return userPermissions.some((access) => {
const accessArray = (isAccessType(access) ? access?.permission : access)?.split(':') || [];
const permissionArray = permission.split(':');
const permissionArray = (permission as string).split(':');
const hasAccess = accessArray.slice(0).reduce((acc, curr, index, array) => {
if (acc === false) {
array.splice(index);
Expand All @@ -61,20 +61,24 @@ export function hasAllPermissions(userPermissions: (Access | string)[], permissi
});
}

export const initialPermissions = {
export interface UsePermissionsState extends RBAC {
isLoading: boolean;
hasAccess?: boolean;
}

export interface UsePermissionsContextState {
isLoading?: boolean;
isOrgAdmin: boolean;
permissions: (string | Access)[];
hasAccess?: (requiredPermissions: string[], checkAll?: boolean) => boolean;
}

export const initialPermissions: UsePermissionsContextState = {
isLoading: true,
isOrgAdmin: false,
permissions: [],
hasAccess: (_?: (Access | string)[]) => {
return;
},
};

export const RBACContext = createContext(initialPermissions);

export interface UsePermissionsState extends RBAC {
isLoading: boolean;
hasAccess?: boolean;
}

export default getRBAC;
2 changes: 1 addition & 1 deletion packages/utils/src/RBACHook/RBACHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const usePermissionsWithContext = (requiredPermissions: string[]) => {

return {
...permissionState,
hasAccess: hasAccess(requiredPermissions),
hasAccess: hasAccess?.(requiredPermissions) || false,
};
};

Expand Down

0 comments on commit 8ad0b19

Please sign in to comment.