import React from "react"; // PatternFly import { Flex, FlexItem, Form, FormGroup, Sidebar, SidebarContent, SidebarPanel, } from "@patternfly/react-core"; // Data types import { PwPolicy, Metadata } from "src/utils/datatypes/globalDataTypes"; // Redux import { useAppDispatch } from "src/store/hooks"; // Hooks import useUpdateRoute from "src/hooks/useUpdateRoute"; import { addAlert } from "src/store/Global/alerts-slice"; // Utils import { asRecord } from "src/utils/subIdUtils"; // Icons import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; // RPC import { PwPolicyModPayload, usePwPolicyModMutation, } from "src/services/rpcPwdPolicies"; // Components import IpaTextInput from "src/components/Form/IpaTextInput"; import TabLayout from "src/components/layouts/TabLayout"; import SecondaryButton from "src/components/layouts/SecondaryButton"; import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; import IpaTextContent from "src/components/Form/IpaTextContent"; interface PropsToPwPolicySettings { pwPolicy: Partial; originalPwPolicy: Partial; metadata: Metadata; onPwPolicyChange: (pwPolicy: Partial) => void; onRefresh: () => void; isModified: boolean; isDataLoading?: boolean; modifiedValues: () => Partial; onResetValues: () => void; pathname: string; } const PasswordPolicySettings = (props: PropsToPwPolicySettings) => { const dispatch = useAppDispatch(); // Update current route data to Redux and highlight the current page in the Nav bar useUpdateRoute({ pathname: props.pathname }); // States const [isDataLoading, setIsDataLoading] = React.useState(false); // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' const { ipaObject, recordOnChange } = asRecord( props.pwPolicy, props.onPwPolicyChange ); // API calls const [savePwPolicy] = usePwPolicyModMutation(); // 'Revert' handler method const onRevert = () => { props.onPwPolicyChange(props.originalPwPolicy); props.onRefresh(); dispatch( addAlert({ name: "revert-success", title: "Password policy data reverted", variant: "success", }) ); }; // Helper method to build the payload based on values const buildPayload = ( modifiedValues: Partial, keyArray: string[] ): PwPolicyModPayload => { const payload: PwPolicyModPayload = { pwPolicyId: props.pwPolicy.cn as string, }; keyArray.forEach((key) => { // Modified values are either the value (in string) or an array ([]) when set to empty string if (modifiedValues[key] !== undefined) { if (modifiedValues[key] === "") { payload[key] = []; } else { payload[key] = modifiedValues[key]; } } }); return payload; }; // on Save handler method const onSave = () => { setIsDataLoading(true); const modifiedValues = props.modifiedValues(); // Generate payload const payload = buildPayload(modifiedValues, [ "krbmaxpwdlife", "krbminpwdlife", "krbpwdhistorylength", "krbpwdmindiffchars", "krbpwdminlength", "krbpwdmaxfailure", "krbpwdfailurecountinterval", "krbpwdlockoutduration", "cospriority", "passwordgracelimit", ]); savePwPolicy(payload).then((response) => { if ("data" in response) { const data = response.data; if (data?.error) { dispatch( addAlert({ name: "error", title: (data.error as Error).message, variant: "danger", }) ); } if (data?.result) { props.onPwPolicyChange(data.result.result); dispatch( addAlert({ name: "success", title: "Password policy '" + props.pwPolicy.cn + "' updated", variant: "success", }) ); // Reset values. Disable 'revert' and 'save' buttons props.onResetValues(); } } setIsDataLoading(false); }); }; // Toolbar const toolbarFields = [ { key: 0, element: ( Refresh ), }, { key: 1, element: ( Revert ), }, { key: 2, element: ( Save ), }, ]; // Render component return ( <> } />
); }; export default PasswordPolicySettings;