import React from "react"; // PatternFly import { Button, Checkbox, Flex, FormGroup, Radio, TextInput, } from "@patternfly/react-core"; // Components import ModalWithFormLayout, { Field, } from "src/components/layouts/ModalWithFormLayout"; import CustomTooltip from "src/components/layouts/CustomTooltip"; import InputRequiredText from "src/components/layouts/InputRequiredText"; import NumberSelector from "src/components/Form/NumberInput"; import PasswordInput from "src/components/layouts/PasswordInput"; // Redux import { useAppDispatch } from "src/store/hooks"; // RPC import { addAlert } from "src/store/Global/alerts-slice"; import { TrustAddPayload, useAddTrustMutation } from "src/services/rpcTrusts"; // Errors import { SerializedError } from "@reduxjs/toolkit"; // Icons import { InfoCircleIcon } from "@patternfly/react-icons"; // Data types import { RangeType } from "src/utils/datatypes/globalDataTypes"; interface PropsToAddTrustModal { isOpen: boolean; onClose: () => void; title: string; onRefresh: () => void; } // Tooltip messages const twoWayCheckboxMessage = "Establish bi-directional trust. By default trust is inbound one-way only."; const externalTrustCheckboxMessage = "Establish external trust to a domain in another forest. The trust is not transitive beyond the domain."; const AddTrustModal = (props: PropsToAddTrustModal) => { const dispatch = useAppDispatch(); // API calls const [addTrust] = useAddTrustMutation(); // States const [isAddButtonSpinning, setIsAddButtonSpinning] = React.useState(false); const [domainName, setDomainName] = React.useState(""); const [isTwoWayCheckboxChecked, setIsTwoWayCheckboxChecked] = React.useState(false); const [isExternalTrustCheckboxChecked, setIsExternalTrustCheckboxChecked] = React.useState(false); // Radio options const [authMethod, setAuthMethod] = React.useState< "admin" | "pre-shared-pwd" >("admin"); // Fields from radio buttons const [adminAccount, setAdminAccount] = React.useState(""); const [adminAccounPwd, setAdminAccounPwd] = React.useState(""); const [preSharedPwd, setPreSharedPwd] = React.useState(""); const [preSharedPwdVerify, setPreSharedPwdVerify] = React.useState(""); // Range type radio buttons const [rangeType, setRangeType] = React.useState("detect"); // Other fields const [baseId, setBaseId] = React.useState(0); const [rangeSize, setRangeSize] = React.useState(0); // Password hidden states const [adminAccounPwdHidden, setAdminAccounPwdHidden] = React.useState(true); const [preSharedPwdHidden, setPreSharedPwdHidden] = React.useState(true); const [preSharedPwdVerifyHidden, setPreSharedPwdVerifyHidden] = React.useState(true); const validateFields = () => { if (authMethod === "pre-shared-pwd") { if (preSharedPwd === "" || preSharedPwdVerify === "") return false; if (preSharedPwd !== preSharedPwdVerify) return false; } if (authMethod === "admin") { if (adminAccount === "" || adminAccounPwd === "") return false; } return domainName !== ""; }; const clearFields = () => { setDomainName(""); setIsTwoWayCheckboxChecked(false); setIsExternalTrustCheckboxChecked(false); setAuthMethod("admin"); setAdminAccount(""); setAdminAccounPwd(""); setPreSharedPwd(""); setPreSharedPwdVerify(""); setRangeType("detect"); setBaseId(0); setRangeSize(0); }; // 'Add' button handler const onAddTrust = () => { setIsAddButtonSpinning(true); const payload: TrustAddPayload = { domainName: domainName, }; if (isTwoWayCheckboxChecked) { payload.bidirectional = true; } if (isExternalTrustCheckboxChecked) { payload.external = true; } if (authMethod === "admin") { payload.realm_admin = adminAccount; payload.realm_passwd = adminAccounPwd; } if (authMethod === "pre-shared-pwd") { payload.trust_secret = preSharedPwd; } if (rangeType === "detect") { payload.range_type = "ipa-ad-trust"; } if (baseId) { payload.base_id = baseId; } if (rangeSize) { payload.range_size = rangeSize; } const validation = validateFields(); if (validation) { addTrust(payload) .then((response) => { if ("data" in response) { const data = response.data?.result; const error = response.data?.error as SerializedError; if (error) { dispatch( addAlert({ name: "add-trust-error", title: error.message!, variant: "danger", }) ); } if (data) { dispatch( addAlert({ name: "add-trust-success", title: data.summary, variant: "success", }) ); // Reset selected item clearFields(); // Update data props.onRefresh(); // Close modal props.onClose(); } } }) .finally(() => { setIsAddButtonSpinning(false); }); } }; // Clean and close modal const cleanAndCloseModal = () => { clearFields(); props.onClose(); }; const fields: Field[] = [ { id: "domain-name", name: "Domain name", pfComponent: ( setDomainName(value)} /> ), }, { id: `two-way-checkbox-${isTwoWayCheckboxChecked}`, pfComponent: ( setIsTwoWayCheckboxChecked(checked) } /> ), }, { id: `external-trust-checkbox-${isExternalTrustCheckboxChecked}`, pfComponent: ( setIsExternalTrustCheckboxChecked(checked) } /> ), }, { id: "admin-account-radio", pfComponent: ( <> { if (checked) setAuthMethod("admin"); }} label="Administrative account" aria-label="Administrative account radio button" />
setAdminAccount(value)} aria-label="Account text input" isDisabled={authMethod !== "admin"} /> setAdminAccounPwd(value)} aria-label="Administrative account password text input" isDisabled={authMethod !== "admin"} onRevealHandler={() => setAdminAccounPwdHidden(!adminAccounPwdHidden) } passwordHidden={adminAccounPwdHidden} />
), }, { id: "pre-shared-pwd-radio", pfComponent: ( <> { if (checked) setAuthMethod("pre-shared-pwd"); }} label="Pre-shared password" aria-label="Pre-shared password radio button" />
setPreSharedPwd(value)} aria-label="Pre-shared password text input" isDisabled={authMethod !== "pre-shared-pwd"} onRevealHandler={() => setPreSharedPwdHidden(!preSharedPwdHidden) } passwordHidden={preSharedPwdHidden} /> <> setPreSharedPwdVerify(value)} aria-label="Pre-shared password verify text input" isDisabled={authMethod !== "pre-shared-pwd"} onRevealHandler={() => setPreSharedPwdVerifyHidden(!preSharedPwdVerifyHidden) } passwordHidden={preSharedPwdVerifyHidden} rules={[ { id: "verify-match", message: "Passwords must match", validate: (v: string) => v === preSharedPwd, }, ]} />
), }, { id: "detect-radio", name: "Detect", pfComponent: ( <> { if (checked) setRangeType("detect"); }} label="Detect" aria-label="Detect radio button" /> { if (checked) setRangeType("ad-domain"); }} label="AD domain" aria-label="AD domain radio button" /> { if (checked) setRangeType("ad-domain-posix"); }} label="AD domain POSIX" aria-label="AD domain POSIX radio button" /> ), }, { id: "base-id-field", name: "Base ID", pfComponent: ( setBaseId(Number(value))} aria-label="Base ID number input" numCharsShown={6} maxValue={2147483647} /> ), }, { id: "range-size-field", name: "Range size", pfComponent: ( setRangeSize(Number(value))} aria-label="Range size number input" numCharsShown={6} maxValue={2147483647} /> ), }, ]; // Buttons are disabled until the user fills the required fields const isAdminAccountDisabled = authMethod === "admin" && (adminAccount.length === 0 || adminAccounPwd.length === 0); const isPreSharedPwdDisabled = (authMethod === "pre-shared-pwd" && preSharedPwd.length === 0 && preSharedPwdVerify.length === 0) || preSharedPwd !== preSharedPwdVerify; const isButtonDisabled = isAddButtonSpinning || domainName.length === 0 || isAdminAccountDisabled || isPreSharedPwdDisabled; // Actions const modalActions: JSX.Element[] = [ , , ]; return ( <> ); }; export default AddTrustModal;