Debian Codegraph @

install/freeipa-webui/src/pages/Trusts/AddTrustModal.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Button,
5 Checkbox,
6 Flex,
7 FormGroup,
8 Radio,
9 TextInput,
10 } from "@patternfly/react-core";
11 // Components
12 import ModalWithFormLayout, {
13 Field,
14 } from "src/components/layouts/ModalWithFormLayout";
15 import CustomTooltip from "src/components/layouts/CustomTooltip";
16 import InputRequiredText from "src/components/layouts/InputRequiredText";
17 import NumberSelector from "src/components/Form/NumberInput";
18 import PasswordInput from "src/components/layouts/PasswordInput";
19 // Redux
20 import { useAppDispatch } from "src/store/hooks";
21 // RPC
22 import { addAlert } from "src/store/Global/alerts-slice";
23 import { TrustAddPayload, useAddTrustMutation } from "src/services/rpcTrusts";
24 // Errors
25 import { SerializedError } from "@reduxjs/toolkit";
26 // Icons
27 import { InfoCircleIcon } from "@patternfly/react-icons";
28 // Data types
29 import { RangeType } from "src/utils/datatypes/globalDataTypes";
30
31 interface PropsToAddTrustModal {
32 isOpen: boolean;
33 onClose: () => void;
34 title: string;
35 onRefresh: () => void;
36 }
37
38 // Tooltip messages
39 const twoWayCheckboxMessage =
40 "Establish bi-directional trust. By default trust is inbound one-way only.";
41 const externalTrustCheckboxMessage =
42 "Establish external trust to a domain in another forest. The trust is not transitive beyond the domain.";
43
44 const AddTrustModal = (props: PropsToAddTrustModal) => {
45 const dispatch = useAppDispatch();
46
47 // API calls
48 const [addTrust] = useAddTrustMutation();
49
50 // States
51 const [isAddButtonSpinning, setIsAddButtonSpinning] =
52 React.useState<boolean>(false);
53 const [domainName, setDomainName] = React.useState<string>("");
54 const [isTwoWayCheckboxChecked, setIsTwoWayCheckboxChecked] =
55 React.useState<boolean>(false);
56 const [isExternalTrustCheckboxChecked, setIsExternalTrustCheckboxChecked] =
57 React.useState<boolean>(false);
58 // Radio options
59 const [authMethod, setAuthMethod] = React.useState<
60 "admin" | "pre-shared-pwd"
61 >("admin");
62 // Fields from radio buttons
63 const [adminAccount, setAdminAccount] = React.useState<string>("");
64 const [adminAccounPwd, setAdminAccounPwd] = React.useState<string>("");
65 const [preSharedPwd, setPreSharedPwd] = React.useState<string>("");
66 const [preSharedPwdVerify, setPreSharedPwdVerify] =
67 React.useState<string>("");
68 // Range type radio buttons
69 const [rangeType, setRangeType] = React.useState<RangeType>("detect");
70 // Other fields
71 const [baseId, setBaseId] = React.useState<number>(0);
72 const [rangeSize, setRangeSize] = React.useState<number>(0);
73 // Password hidden states
74 const [adminAccounPwdHidden, setAdminAccounPwdHidden] =
75 React.useState<boolean>(true);
76 const [preSharedPwdHidden, setPreSharedPwdHidden] =
77 React.useState<boolean>(true);
78 const [preSharedPwdVerifyHidden, setPreSharedPwdVerifyHidden] =
79 React.useState<boolean>(true);
80
81 const validateFields = () => {
82 if (authMethod === "pre-shared-pwd") {
83 if (preSharedPwd === "" || preSharedPwdVerify === "") return false;
84 if (preSharedPwd !== preSharedPwdVerify) return false;
85 }
86 if (authMethod === "admin") {
87 if (adminAccount === "" || adminAccounPwd === "") return false;
88 }
89 return domainName !== "";
90 };
91
92 const clearFields = () => {
93 setDomainName("");
94 setIsTwoWayCheckboxChecked(false);
95 setIsExternalTrustCheckboxChecked(false);
96 setAuthMethod("admin");
97 setAdminAccount("");
98 setAdminAccounPwd("");
99 setPreSharedPwd("");
100 setPreSharedPwdVerify("");
101 setRangeType("detect");
102 setBaseId(0);
103 setRangeSize(0);
104 };
105
106 // 'Add' button handler
107 const onAddTrust = () => {
108 setIsAddButtonSpinning(true);
109
110 const payload: TrustAddPayload = {
111 domainName: domainName,
112 };
113
114 if (isTwoWayCheckboxChecked) {
115 payload.bidirectional = true;
116 }
117 if (isExternalTrustCheckboxChecked) {
118 payload.external = true;
119 }
120 if (authMethod === "admin") {
121 payload.realm_admin = adminAccount;
122 payload.realm_passwd = adminAccounPwd;
123 }
124 if (authMethod === "pre-shared-pwd") {
125 payload.trust_secret = preSharedPwd;
126 }
127 if (rangeType === "detect") {
128 payload.range_type = "ipa-ad-trust";
129 }
130 if (baseId) {
131 payload.base_id = baseId;
132 }
133 if (rangeSize) {
134 payload.range_size = rangeSize;
135 }
136
137 const validation = validateFields();
138
139 if (validation) {
140 addTrust(payload)
141 .then((response) => {
142 if ("data" in response) {
143 const data = response.data?.result;
144 const error = response.data?.error as SerializedError;
145
146 if (error) {
147 dispatch(
148 addAlert({
149 name: "add-trust-error",
150 title: error.message!,
151 variant: "danger",
152 })
153 );
154 }
155
156 if (data) {
157 dispatch(
158 addAlert({
159 name: "add-trust-success",
160 title: data.summary,
161 variant: "success",
162 })
163 );
164 // Reset selected item
165 clearFields();
166 // Update data
167 props.onRefresh();
168 // Close modal
169 props.onClose();
170 }
171 }
172 })
173 .finally(() => {
174 setIsAddButtonSpinning(false);
175 });
176 }
177 };
178
179 // Clean and close modal
180 const cleanAndCloseModal = () => {
181 clearFields();
182 props.onClose();
183 };
184
185 const fields: Field[] = [
186 {
187 id: "domain-name",
188 name: "Domain name",
189 pfComponent: (
190 <TextInput
191 data-cy={"modal-textbox-domain-name"}
192 id="realm_server"
193 name="realm_server"
194 value={domainName}
195 aria-label="Domain name text input"
196 onChange={(_event, value) => setDomainName(value)}
197 />
198 ),
199 },
200 {
201 id: `two-way-checkbox-${isTwoWayCheckboxChecked}`,
202 pfComponent: (
203 <Flex>
204 <Checkbox
205 data-cy={"modal-checkbox-two-way-trust"}
206 id="bidirectional"
207 name="bidirectional"
208 label="Two-way trust"
209 isChecked={isTwoWayCheckboxChecked}
210 aria-label="Two-way trust checkbox"
211 onChange={(_event, checked: boolean) =>
212 setIsTwoWayCheckboxChecked(checked)
213 }
214 />
215 <CustomTooltip
216 id="two-way-trust-tooltip"
217 message={twoWayCheckboxMessage}
218 ariaLabel="Two-way trust tooltip with message"
219 >
220 <InfoCircleIcon />
221 </CustomTooltip>
222 </Flex>
223 ),
224 },
225 {
226 id: `external-trust-checkbox-${isExternalTrustCheckboxChecked}`,
227 pfComponent: (
228 <Flex>
229 <Checkbox
230 data-cy={"modal-checkbox-external-trust"}
231 id="external-trust-checkbox"
232 name="external"
233 label="External trust"
234 isChecked={isExternalTrustCheckboxChecked}
235 aria-label="External trust checkbox"
236 onChange={(_event, checked: boolean) =>
237 setIsExternalTrustCheckboxChecked(checked)
238 }
239 />
240 <CustomTooltip
241 id="external-trust-tooltip"
242 message={externalTrustCheckboxMessage}
243 ariaLabel="External trust tooltip with message"
244 >
245 <InfoCircleIcon />
246 </CustomTooltip>
247 </Flex>
248 ),
249 },
250 {
251 id: "admin-account-radio",
252 pfComponent: (
253 <>
254 <Radio
255 data-cy={"modal-radio-admin-account"}
256 id="admin-account-radio"
257 name="auth-method-group"
258 checked={authMethod === "admin"}
259 onChange={(_event, checked: boolean) => {
260 if (checked) setAuthMethod("admin");
261 }}
262 label="Administrative account"
263 aria-label="Administrative account radio button"
264 />
265 <div className="pf-v6-u-ml-lg" id="admin-account-radio">
266 <FormGroup
267 label="Account"
268 fieldId="admin-account-field"
269 name="admin-account-formgroup"
270 isRequired={authMethod === "admin"}
271 >
272 <InputRequiredText
273 dataCy={"modal-textbox-admin-account"}
274 id="admin-account-field"
275 name="realm_admin"
276 value={adminAccount}
277 onChange={(value: string) => setAdminAccount(value)}
278 aria-label="Account text input"
279 isDisabled={authMethod !== "admin"}
280 />
281 </FormGroup>
282 <FormGroup
283 label="Password"
284 fieldId="admin-account-pwd-field"
285 name="admin-account-pwd-formgroup"
286 isRequired={authMethod === "admin"}
287 >
288 <PasswordInput
289 dataCy={"modal-textbox-admin-account-pwd"}
290 id="admin-account-pwd-field"
291 name="realm_passwd"
292 value={adminAccounPwd}
293 onChange={(value: string) => setAdminAccounPwd(value)}
294 aria-label="Administrative account password text input"
295 isDisabled={authMethod !== "admin"}
296 onRevealHandler={() =>
297 setAdminAccounPwdHidden(!adminAccounPwdHidden)
298 }
299 passwordHidden={adminAccounPwdHidden}
300 />
301 </FormGroup>
302 </div>
303 </>
304 ),
305 },
306 {
307 id: "pre-shared-pwd-radio",
308 pfComponent: (
309 <>
310 <Radio
311 data-cy={"modal-radio-pre-shared-pwd"}
312 id="pre-shared-pwd-radio"
313 name="auth-method-group"
314 checked={authMethod === "pre-shared-pwd"}
315 onChange={(_event, checked: boolean) => {
316 if (checked) setAuthMethod("pre-shared-pwd");
317 }}
318 label="Pre-shared password"
319 aria-label="Pre-shared password radio button"
320 />
321 <div className="pf-v6-u-ml-lg" id="pre-shared-pwd-radio">
322 <FormGroup
323 label="Password"
324 fieldId="pre-shared-pwd-field"
325 name="pre-shared-pwd-formgroup"
326 isRequired={authMethod === "pre-shared-pwd"}
327 >
328 <PasswordInput
329 dataCy={"modal-textbox-pre-shared-pwd"}
330 id="pre-shared-pwd-field"
331 name="realm_pre_shared_pwd"
332 value={preSharedPwd}
333 onChange={(value: string) => setPreSharedPwd(value)}
334 aria-label="Pre-shared password text input"
335 isDisabled={authMethod !== "pre-shared-pwd"}
336 onRevealHandler={() =>
337 setPreSharedPwdHidden(!preSharedPwdHidden)
338 }
339 passwordHidden={preSharedPwdHidden}
340 />
341 </FormGroup>
342 <FormGroup
343 label="Password verify"
344 fieldId="pre-shared-pwd-verify-field"
345 name="pre-shared-pwd-verify-formgroup"
346 isRequired={authMethod === "pre-shared-pwd"}
347 >
348 <>
349 <PasswordInput
350 dataCy={"modal-textbox-pre-shared-pwd-verify"}
351 id="pre-shared-pwd-verify-field"
352 name="realm_pre_shared_pwd_verify"
353 value={preSharedPwdVerify}
354 onChange={(value: string) => setPreSharedPwdVerify(value)}
355 aria-label="Pre-shared password verify text input"
356 isDisabled={authMethod !== "pre-shared-pwd"}
357 onRevealHandler={() =>
358 setPreSharedPwdVerifyHidden(!preSharedPwdVerifyHidden)
359 }
360 passwordHidden={preSharedPwdVerifyHidden}
361 rules={[
362 {
363 id: "verify-match",
364 message: "Passwords must match",
365 validate: (v: string) => v === preSharedPwd,
366 },
367 ]}
368 />
369 </>
370 </FormGroup>
371 </div>
372 </>
373 ),
374 },
375 {
376 id: "detect-radio",
377 name: "Detect",
378 pfComponent: (
379 <>
380 <Radio
381 data-cy={"modal-radio-detect"}
382 id="detect-radio"
383 name="range-type-group"
384 checked={rangeType === "detect"}
385 onChange={(_event, checked: boolean) => {
386 if (checked) setRangeType("detect");
387 }}
388 label="Detect"
389 aria-label="Detect radio button"
390 />
391 <Radio
392 data-cy={"modal-radio-ad-domain"}
393 id="ad-domain-radio"
394 name="range-type-group"
395 checked={rangeType === "ad-domain"}
396 onChange={(_event, checked: boolean) => {
397 if (checked) setRangeType("ad-domain");
398 }}
399 label="AD domain"
400 aria-label="AD domain radio button"
401 />
402 <Radio
403 data-cy={"modal-radio-ad-domain-posix"}
404 id="ad-domain-posix-radio"
405 name="range-type-group"
406 checked={rangeType === "ad-domain-posix"}
407 onChange={(_event, checked: boolean) => {
408 if (checked) setRangeType("ad-domain-posix");
409 }}
410 label="AD domain POSIX"
411 aria-label="AD domain POSIX radio button"
412 />
413 </>
414 ),
415 },
416 {
417 id: "base-id-field",
418 name: "Base ID",
419 pfComponent: (
420 <NumberSelector
421 dataCy={"modal-number-selector-base-id"}
422 id="base-id-field"
423 name="base_id"
424 value={baseId}
425 setValue={(value: number | "") => setBaseId(Number(value))}
426 aria-label="Base ID number input"
427 numCharsShown={6}
428 maxValue={2147483647}
429 />
430 ),
431 },
432 {
433 id: "range-size-field",
434 name: "Range size",
435 pfComponent: (
436 <NumberSelector
437 dataCy={"modal-number-selector-range-size"}
438 id="range-size-field"
439 name="range_size"
440 value={rangeSize}
441 setValue={(value: number | "") => setRangeSize(Number(value))}
442 aria-label="Range size number input"
443 numCharsShown={6}
444 maxValue={2147483647}
445 />
446 ),
447 },
448 ];
449
450 // Buttons are disabled until the user fills the required fields
451 const isAdminAccountDisabled =
452 authMethod === "admin" &&
453 (adminAccount.length === 0 || adminAccounPwd.length === 0);
454
455 const isPreSharedPwdDisabled =
456 (authMethod === "pre-shared-pwd" &&
457 preSharedPwd.length === 0 &&
458 preSharedPwdVerify.length === 0) ||
459 preSharedPwd !== preSharedPwdVerify;
460
461 const isButtonDisabled =
462 isAddButtonSpinning ||
463 domainName.length === 0 ||
464 isAdminAccountDisabled ||
465 isPreSharedPwdDisabled;
466
467 // Actions
468 const modalActions: JSX.Element[] = [
469 <Button
470 data-cy={"modal-button-add"}
471 key="add-new"
472 variant="secondary"
473 isDisabled={isButtonDisabled}
474 isLoading={isAddButtonSpinning}
475 type="submit"
476 onClick={onAddTrust}
477 >
478 Add
479 </Button>,
480 <Button
481 data-cy={"modal-button-cancel"}
482 key="cancel-new"
483 variant="link"
484 onClick={cleanAndCloseModal}
485 >
486 Cancel
487 </Button>,
488 ];
489
490 return (
491 <>
492 <ModalWithFormLayout
493 dataCy={"add-trust-modal"}
494 variantType={"small"}
495 modalPosition={"top"}
496 offPosition={"76px"}
497 title={props.title}
498 formId="add-trust-modal"
499 fields={fields}
500 show={props.isOpen}
501 onSubmit={onAddTrust}
502 onClose={cleanAndCloseModal}
503 actions={modalActions}
504 />
505 </>
506 );
507 };
508
509 export default AddTrustModal;