import * as React from 'react'; import { Alert, AlertVariant, Button, Flex, FlexItem, List, ListItem, MenuToggle, Modal, ModalFooter, ModalVariant, Select, SelectList, SelectOption, Spinner, Content, ModalBody, ModalHeader, } from '@patternfly/react-core'; import textStyles from '@patternfly/react-styles/css/utilities/Text/text.mjs'; import { useRoleMap } from '~/hooks/useRole'; import type { RoleBinding } from '~/types'; import { ButtonWithAccessTooltip } from '../ButtonWithAccessTooltip'; import { splitRowKey } from './userAccessTableRows'; import './UserAccess.scss'; const getRoleRefWeight = ( roleRefName: string | undefined, roleRefWeights: Record, ): number | undefined => { if (!roleRefName) { return undefined; } const weight = roleRefWeights[roleRefName]; return weight === undefined ? undefined : weight; }; const saveErrorMessage = (err: unknown): string => { if (err instanceof Error && err.message) { return err.message; } return 'Unable to change roles. Please try again.'; }; export type UserAccessChangeRoleModalProps = { isOpen: boolean; onClose: () => void; selectedRowKeys: Set; allAffectedRoleBindings: RoleBinding[]; onSave: (newRoleRef: string) => Promise; }; export const UserAccessChangeRoleModal: React.FC = ({ isOpen, onClose, selectedRowKeys, allAffectedRoleBindings, onSave, }) => { const [roleMap, roleMapLoaded] = useRoleMap(); const [isRoleSelectOpen, setRoleSelectOpen] = React.useState(false); const [modalSelectedRoleRef, setModalSelectedRoleRef] = React.useState(); const [isSaving, setIsSaving] = React.useState(false); const [saveError, setSaveError] = React.useState(null); const currentRoleMap = React.useMemo( (): Record => roleMap?.roleMap ?? {}, [roleMap], ); const roleRefWeights = React.useMemo( (): Record => roleMap?.roleRefWeights ?? {}, [roleMap], ); const roleSelectOptions = React.useMemo(() => Object.entries(currentRoleMap), [currentRoleMap]); const unrankedRoleRefs = React.useMemo(() => { const refs = new Set(); for (const rb of allAffectedRoleBindings) { const roleRef = rb.roleRef?.name; if (roleRef && getRoleRefWeight(roleRef, roleRefWeights) === undefined) { refs.add(roleRef); } } if ( modalSelectedRoleRef && getRoleRefWeight(modalSelectedRoleRef, roleRefWeights) === undefined ) { refs.add(modalSelectedRoleRef); } return [...refs]; }, [allAffectedRoleBindings, modalSelectedRoleRef, roleRefWeights]); const hasUnrankedRoles = unrankedRoleRefs.length > 0; const resetModalFields = () => { setModalSelectedRoleRef(undefined); setRoleSelectOpen(false); setSaveError(null); }; const handleClose = () => { if (isSaving) { return; } onClose(); resetModalFields(); }; const selectedCount = selectedRowKeys.size; const highestAffectedRoleWeight = React.useMemo(() => { const weights = allAffectedRoleBindings .map((rb) => getRoleRefWeight(rb.roleRef?.name, roleRefWeights)) .filter((weight): weight is number => weight !== undefined); return weights.length === 0 ? undefined : Math.max(...weights); }, [allAffectedRoleBindings, roleRefWeights]); const getUserHighestRole = React.useMemo(() => { return (username: string): string => { const userRoleBindings = allAffectedRoleBindings.filter((rb) => rb.subjects?.some((subject) => subject.name === username), ); const allUserRoles = userRoleBindings.map((rb) => rb.roleRef.name); if (allUserRoles.length === 0) { return 'Unknown'; } const highestRole = allUserRoles.reduce((max, role) => { const roleWeight = getRoleRefWeight(role, roleRefWeights); const maxWeight = getRoleRefWeight(max, roleRefWeights); if (roleWeight === undefined) { return max; } if (maxWeight === undefined) { return role; } return roleWeight > maxWeight ? role : max; }, allUserRoles[0]); return currentRoleMap[highestRole] ?? highestRole; }; }, [allAffectedRoleBindings, currentRoleMap, roleRefWeights]); const isModalSaveDisabled = React.useMemo(() => { if (hasUnrankedRoles) { return true; } if (!modalSelectedRoleRef) { return true; } const selectedRoleWeight = getRoleRefWeight(modalSelectedRoleRef, roleRefWeights); if (selectedRoleWeight === undefined) { return true; } // One user selected -> allow downgrade if (selectedCount === 1) { return false; } if (highestAffectedRoleWeight === undefined) { return true; } // Downgrade exists? return highestAffectedRoleWeight > selectedRoleWeight; }, [ hasUnrankedRoles, modalSelectedRoleRef, selectedCount, highestAffectedRoleWeight, roleRefWeights, ]); const handleSave = async () => { if (!modalSelectedRoleRef || isModalSaveDisabled) { return; } setSaveError(null); setIsSaving(true); try { await onSave(modalSelectedRoleRef); onClose(); resetModalFields(); } catch (err: unknown) { setSaveError(saveErrorMessage(err)); } finally { setIsSaving(false); } }; return ( document.querySelector('#hacDev-modal-container') ?? document.body} > {hasUnrankedRoles ? ( Cannot evaluate role changes for: {unrankedRoleRefs.join(', ')}. These role bindings reference roles that are not listed in the cluster Konflux configuration, or the configuration order is invalid. Update konflux-public-info or remove affected users before continuing. ) : null} When changing access, the user will be removed from all role bindings in the current namespace and then added to the new role binding in the namespace. If any selected role binding contains multiple users, the role binding will be split and the users not selected will be added to the new role binding with the same role. {[...selectedRowKeys].map((rowKey) => { const { username, subjectKind } = splitRowKey(rowKey); return ( {username} ({subjectKind}) : {getUserHighestRole(username)} ); })} New role* {roleMapLoaded && roleMap ? ( ) : ( )} {saveError ? ( {saveError} ) : null} {isSaving ? ( ) : ( void handleSave()} isDisabled={isModalSaveDisabled} tooltip={ hasUnrankedRoles ? 'Cannot change roles: one or more affected role bindings use a role that is not configured in this cluster.' : !modalSelectedRoleRef ? 'No role selected. Select a role to save the changes.' : 'You cannot save the changes. The selected role is not allowed to downgrade the users.' } > Save )} ); };