Skip to content

Commit

Permalink
feat(HMS-2721): add a hook for the permissions
Browse files Browse the repository at this point in the history
This change add the useIdmPermissions which is used to
check the required permissions on the UI.

Signed-off-by: Alejandro Visiedo <[email protected]>
  • Loading branch information
avisiedo committed May 20, 2024
1 parent 732edb8 commit ebf668e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Hooks/useIdmPermissions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { usePermissions } from '@redhat-cloud-services/frontend-components-utilities/RBACHook';

const APP = 'idmsvc';

export interface IdmPermissions {
isLoading: boolean;
permissions: {
hasTokenCreate: boolean | undefined;
hasDomainsRead: boolean | undefined;
hasDomainsList: boolean | undefined;
hasDomainsUpdate: boolean | undefined;
hasDomainsDelete: boolean | undefined;
};
}

/**
* @returns useIdmPermissions encapsulate the permissions for
* domain integration service.
*/
const useIdmPermissions = (): IdmPermissions => {
const { hasAccess: hasTokensCreate, isLoading: isLoadingTokensCreate } = usePermissions(APP, [APP + ':token:create'], false, true);
const { hasAccess: hasDomainsRead, isLoading: isLoadingDomainsRead } = usePermissions(APP, [APP + ':domains:read'], false, true);
const { hasAccess: hasDomainsUpdate, isLoading: isLoadingDomainsUpdate } = usePermissions(APP, [APP + ':domains:update'], false, true);
const { hasAccess: hasDomainsDelete, isLoading: isLoadingDomainsDelete } = usePermissions(APP, [APP + ':domains:delete'], false, true);
const { hasAccess: hasDomainsList, isLoading: isLoadingDomainsList } = usePermissions(APP, [APP + ':domains:list'], false, true);

const isLoading: boolean =
isLoadingTokensCreate || isLoadingDomainsRead || isLoadingDomainsUpdate || isLoadingDomainsDelete || isLoadingDomainsList;

return {
isLoading: isLoading,
permissions: {
hasTokenCreate: hasTokensCreate,
hasDomainsRead: hasDomainsRead,
hasDomainsList: hasDomainsList,
hasDomainsUpdate: hasDomainsUpdate,
hasDomainsDelete: hasDomainsDelete,
},
};
};

export default useIdmPermissions;

0 comments on commit ebf668e

Please sign in to comment.