Skip to content

Commit

Permalink
feat(RBACHook): Extend RBAC hook with context
Browse files Browse the repository at this point in the history
  • Loading branch information
bastilian committed Jul 27, 2022
1 parent b92c638 commit 0de30e4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/utils/src/RBACHook/RBACHook.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { useEffect, useState } from 'react';
import { RBAC, doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';

export interface UsePermissionsState extends RBAC {
hasAccess: boolean;
isLoading: boolean;
}
import { useContext, useEffect, useState } from 'react';
import { RBACContext, UsePermissionsState } from './constants';
import { doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';

export function usePermissions(appName: string, permissionsList: string[], disableCache?: boolean, checkAll?: boolean): UsePermissionsState {
const [permissions, setPermissions] = useState<UsePermissionsState>({
Expand Down Expand Up @@ -35,4 +31,13 @@ export function usePermissions(appName: string, permissionsList: string[], disab
return permissions;
}

export const usePermissionsWithContext = (requiredPermissions: string[]) => {
const { hasAccess, ...permissionState } = useContext(RBACContext);

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

export default usePermissions;
52 changes: 52 additions & 0 deletions packages/utils/src/RBACHook/RBACProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useEffect, useState } from 'react';
import { Access } from '@redhat-cloud-services/rbac-client';
import { doesHavePermissions, getRBAC, hasAllPermissions } from '../RBAC';
import { RBACContext, initialPermissions } from './constants';

const hasAccessWithUserPermissions = (userPermissions: Access[]) => {
return (requiredPermissions: any, checkAll = true) => {
return checkAll ? hasAllPermissions(userPermissions, requiredPermissions) : doesHavePermissions(userPermissions, requiredPermissions);
};
};

export interface RBACProviderProps {
appName: string;
}

const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, children }) => {
const [permissionState, setPermissionState] = useState(initialPermissions);
let loading = false;

const fetchPermissions = async () => {
loading = true;
const { isOrgAdmin, permissions: userPermissions } = await getRBAC(appName, true);

setPermissionState((currentPerms: any) => ({
...currentPerms,
isLoading: false,
isOrgAdmin,
permissions: userPermissions,
}));
loading = false;
};

useEffect(() => {
console.log('SADASD', appName, permissionState, loading);
if (!loading && permissionState.permissions.length == 0) {
fetchPermissions();
}
}, [appName]);

return (
<RBACContext.Provider
value={{
...permissionState,
hasAccess: hasAccessWithUserPermissions(permissionState?.permissions),
}}
>
{!permissionState.isLoading && children}
</RBACContext.Provider>
);
};

export default RBACProvider;
19 changes: 19 additions & 0 deletions packages/utils/src/RBACHook/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createContext } from 'react';
import { RBAC } from '../RBAC';

export const initialPermissions = {
isLoading: true,
loaded: false,
isOrgAdmin: false,
permissions: [],
hasAccess: (_?: any) => {
return;
},
};

export const RBACContext = createContext(initialPermissions);

export interface UsePermissionsState extends RBAC {
isLoading: boolean;
hasAccess?: boolean;
}
1 change: 1 addition & 0 deletions packages/utils/src/RBACHook/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as RBACProvider } from './RBACProvider';
export { default } from './RBACHook';
export * from './RBACHook';

0 comments on commit 0de30e4

Please sign in to comment.