Debian Codegraph @

install/freeipa-webui/src/pages/CertificateMapping/CertificateMappingSettings.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 DropdownItem,
5 Flex,
6 FlexItem,
7 Form,
8 FormGroup,
9 Sidebar,
10 SidebarContent,
11 SidebarPanel,
12 } from "@patternfly/react-core";
13 // Data types
14 import {
15 CertificateMapping,
16 Metadata,
17 } from "src/utils/datatypes/globalDataTypes";
18 // Redux
19 import { useAppDispatch } from "src/store/hooks";
20 // Hooks
21 import useUpdateRoute from "src/hooks/useUpdateRoute";
22 import { addAlert } from "src/store/Global/alerts-slice";
23 // Utils
24 import { certMapRuleAsRecord } from "src/utils/certMappingUtils";
25 // Icons
26 import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons";
27 // RPC
28 import {
29 CertModPayload,
30 useCertMapRuleModMutation,
31 } from "src/services/rpcCertMapping";
32 // Components
33 import IpaTextInput from "src/components/Form/IpaTextInput/IpaTextInput";
34 import TabLayout from "src/components/layouts/TabLayout";
35 import SecondaryButton from "src/components/layouts/SecondaryButton";
36 import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout";
37 import KebabLayout from "src/components/layouts/KebabLayout";
38 import IpaTextArea from "src/components/Form/IpaTextArea";
39 import PopoverWithIconLayout from "src/components/layouts/PopoverWithIconLayout";
40 import IpaTextboxList from "src/components/Form/IpaTextboxList";
41 import IpaNumberInput from "src/components/Form/IpaNumberInput";
42 import EnableDisableRuleModal from "src/components/modals/CertificateMapping/EnableDisableRuleModal";
43 import DeleteRuleModal from "src/components/modals/CertificateMapping/DeleteRuleModal";
44
45 interface CertificateMappingSettingsProps {
46 certMapping: Partial<CertificateMapping>;
47 originalCertMapping: Partial<CertificateMapping>;
48 metadata: Metadata;
49 onCertMappingChange: (idpRef: Partial<CertificateMapping>) => void;
50 onRefresh: () => void;
51 isModified: boolean;
52 isDataLoading?: boolean;
53 modifiedValues: () => Partial<CertificateMapping>;
54 onResetValues: () => void;
55 pathname: string;
56 }
57
58 const CertificateMappingSettings = (props: CertificateMappingSettingsProps) => {
59 const dispatch = useAppDispatch();
60
61 // Update current route data to Redux and highlight the current page in the Nav bar
62 useUpdateRoute({ pathname: props.pathname });
63
64 // States
65 const [isDataLoading, setIsDataLoading] = React.useState(false);
66 const [isRuleEnabled, setIsRuleEnabled] = React.useState(true);
67
68 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
69 const { ipaObject, recordOnChange } = certMapRuleAsRecord(
70 props.certMapping,
71 props.onCertMappingChange
72 );
73
74 // Determine the rule status
75 React.useEffect(() => {
76 if (props.certMapping) {
77 setIsRuleEnabled(
78 props.certMapping.ipaenabledflag?.toString() === "true" ? true : false
79 );
80 }
81 }, [props.certMapping]);
82
83 // API calls
84 const [saveCertMapping] = useCertMapRuleModMutation();
85
86 // 'Revert' handler method
87 const onRevert = () => {
88 props.onCertMappingChange(props.originalCertMapping);
89 props.onRefresh();
90 dispatch(
91 addAlert({
92 name: "revert-success",
93 title: "Certificate mapping rule data reverted",
94 variant: "success",
95 })
96 );
97 };
98
99 // Helper method to build the payload based on values
100 const buildPayload = (
101 modifiedValues: Partial<CertificateMapping>,
102 keyArray: string[]
103 ): CertModPayload => {
104 const payload: CertModPayload = {
105 ruleId: props.certMapping.cn?.toString() as string,
106 };
107
108 keyArray.forEach((key) => {
109 // Modified values are either the value (in string) or an array ([]) when set to empty string
110 if (modifiedValues[key] !== undefined) {
111 if (modifiedValues[key] === "") {
112 payload[key] = [];
113 } else {
114 payload[key] = modifiedValues[key];
115 }
116 }
117 });
118 return payload;
119 };
120
121 // on Save handler method
122 const onSave = () => {
123 setIsDataLoading(true);
124 const modifiedValues = props.modifiedValues();
125
126 // Generate payload
127 const payload = buildPayload(modifiedValues, [
128 "description",
129 "ipacertmapmaprule",
130 "ipacertmapmatchrule",
131 "associateddomain",
132 "ipacertmappriority",
133 ]);
134
135 saveCertMapping(payload).then((response) => {
136 if ("data" in response) {
137 const data = response.data;
138 if (data?.error) {
139 dispatch(
140 addAlert({
141 name: "error",
142 title: (data.error as Error).message,
143 variant: "danger",
144 })
145 );
146 }
147 if (data?.result) {
148 props.onCertMappingChange(data.result.result);
149 dispatch(
150 addAlert({
151 name: "success",
152 title: response.data.result.summary,
153 variant: "success",
154 })
155 );
156 // Disable 'revert' and 'save' buttons
157 props.onRefresh();
158 }
159 }
160 setIsDataLoading(false);
161 });
162 };
163
164 // Kebab
165 const [isKebabOpen, setIsKebabOpen] = React.useState(false);
166
167 const kebabItems = [
168 <DropdownItem
169 data-cy="certificate-mapping-tab-settings-kebab-enable"
170 key="enable"
171 isDisabled={isRuleEnabled}
172 onClick={() => setIsEnableDisableOpen(true)}
173 >
174 Enable
175 </DropdownItem>,
176 <DropdownItem
177 data-cy="certificate-mapping-tab-settings-kebab-disable"
178 key="disable"
179 isDisabled={!isRuleEnabled}
180 onClick={() => setIsEnableDisableOpen(true)}
181 >
182 Disable
183 </DropdownItem>,
184 <DropdownItem
185 data-cy="certificate-mapping-tab-settings-kebab-delete"
186 key="delete"
187 onClick={() => setIsDeleteOpen(true)}
188 >
189 Delete
190 </DropdownItem>,
191 ];
192
193 // Toolbar
194 const toolbarFields = [
195 {
196 key: 0,
197 element: (
198 <SecondaryButton
199 dataCy="certificate-mapping-tab-settings-button-refresh"
200 onClickHandler={props.onRefresh}
201 >
202 Refresh
203 </SecondaryButton>
204 ),
205 },
206 {
207 key: 1,
208 element: (
209 <SecondaryButton
210 dataCy="certificate-mapping-tab-settings-button-revert"
211 isDisabled={!props.isModified || isDataLoading}
212 onClickHandler={onRevert}
213 >
214 Revert
215 </SecondaryButton>
216 ),
217 },
218 {
219 key: 2,
220 element: (
221 <SecondaryButton
222 dataCy="certificate-mapping-tab-settings-button-save"
223 isDisabled={!props.isModified || isDataLoading}
224 onClickHandler={onSave}
225 >
226 Save
227 </SecondaryButton>
228 ),
229 },
230 {
231 key: 3,
232 element: (
233 <KebabLayout
234 dataCy="certificate-mapping-tab-settings-kebab"
235 direction={"up"}
236 onDropdownSelect={() => setIsKebabOpen(!isKebabOpen)}
237 onKebabToggle={() => setIsKebabOpen(!isKebabOpen)}
238 idKebab="toggle-action-buttons"
239 isKebabOpen={isKebabOpen}
240 dropdownItems={kebabItems}
241 isDisabled={isDataLoading}
242 />
243 ),
244 },
245 ];
246
247 // Tooltip messages
248 const mappingRuleMessage =
249 "Rule used to map the certificate with a user entry";
250
251 const matchingRuleMessage =
252 "Rule used to check if a certificate can be used for authentication";
253
254 const domainNameMessage = "Domain where the user entry will be searched";
255
256 const priorityMessage =
257 "Priority of the rule (higher number means lower priority)";
258
259 // Modals
260 const [isEnableDisableOpen, setIsEnableDisableOpen] = React.useState(false);
261 const [isDeleteOpen, setIsDeleteOpen] = React.useState(false);
262
263 // Render component
264 return (
265 <>
266 <TabLayout id="settings-page" toolbarItems={toolbarFields}>
267 <Sidebar isPanelRight>
268 <SidebarPanel variant="sticky">
269 <HelpTextWithIconLayout
270 textContent="Help"
271 icon={
272 <OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" />
273 }
274 />
275 </SidebarPanel>
276 <SidebarContent className="pf-v6-u-mr-xl">
277 <Flex direction={{ default: "column", lg: "row" }}>
278 <FlexItem flex={{ default: "flex_1" }}>
279 <Form className="pf-v6-u-mb-lg">
280 <FormGroup label="Rule name" role="group">
281 <IpaTextInput
282 dataCy="certificate-mapping-tab-settings-textbox-cn"
283 name={"cn"}
284 ariaLabel={"Rule name"}
285 ipaObject={ipaObject}
286 objectName="certmaprule"
287 metadata={props.metadata}
288 />
289 </FormGroup>
290 <FormGroup label="Description" fieldId="description">
291 <IpaTextArea
292 dataCy="certificate-mapping-tab-settings-textbox-description"
293 name={"description"}
294 ariaLabel={"Description"}
295 ipaObject={ipaObject}
296 onChange={recordOnChange}
297 objectName="certmaprule"
298 metadata={props.metadata}
299 />
300 </FormGroup>
301 <FormGroup
302 label="Mapping rule"
303 fieldId="ipacertmapmaprule"
304 labelHelp={
305 <PopoverWithIconLayout message={mappingRuleMessage} />
306 }
307 >
308 <IpaTextInput
309 dataCy="certificate-mapping-tab-settings-textbox-ipacertmapmaprule"
310 name={"ipacertmapmaprule"}
311 ariaLabel={"Mapping rule"}
312 ipaObject={ipaObject}
313 onChange={recordOnChange}
314 objectName="certmaprule"
315 metadata={props.metadata}
316 />
317 </FormGroup>
318 <FormGroup
319 label="Matching rule"
320 fieldId="ipacertmapmatchrule"
321 labelHelp={
322 <PopoverWithIconLayout message={matchingRuleMessage} />
323 }
324 >
325 <IpaTextInput
326 dataCy="certificate-mapping-tab-settings-textbox-ipacertmapmatchrule"
327 name={"ipacertmapmatchrule"}
328 ariaLabel={"Matching rule"}
329 ipaObject={ipaObject}
330 onChange={recordOnChange}
331 objectName="certmaprule"
332 metadata={props.metadata}
333 />
334 </FormGroup>
335 <FormGroup
336 label="Domain name"
337 fieldId="associateddomain"
338 labelHelp={
339 <PopoverWithIconLayout message={domainNameMessage} />
340 }
341 >
342 <IpaTextboxList
343 dataCy="certificate-mapping-tab-settings-textbox-associateddomain"
344 ipaObject={ipaObject}
345 setIpaObject={recordOnChange}
346 name={"associateddomain"}
347 ariaLabel={"Domain name list"}
348 />
349 </FormGroup>
350 <FormGroup
351 label="Priority"
352 fieldId="ipacertmappriority"
353 labelHelp={
354 <PopoverWithIconLayout message={priorityMessage} />
355 }
356 >
357 <IpaNumberInput
358 dataCy="certificate-mapping-tab-settings-textbox-ipacertmappriority"
359 id={"ipacertmappriority"}
360 name={"ipacertmappriority"}
361 aria-label={"Priority number"}
362 ipaObject={ipaObject}
363 onChange={recordOnChange}
364 objectName="certmaprule"
365 metadata={props.metadata}
366 numCharsShown={6}
367 minValue={0}
368 maxValue={2147483647}
369 />
370 </FormGroup>
371 </Form>
372 </FlexItem>
373 </Flex>
374 </SidebarContent>
375 </Sidebar>
376 </TabLayout>
377 <EnableDisableRuleModal
378 isOpen={isEnableDisableOpen}
379 onClose={() => setIsEnableDisableOpen(false)}
380 ruleId={props.certMapping.cn?.toString() as string}
381 operation={isRuleEnabled ? "disable" : "enable"}
382 setIsLoading={setIsDataLoading}
383 onRefresh={props.onRefresh}
384 />
385 <DeleteRuleModal
386 isOpen={isDeleteOpen}
387 onClose={() => setIsDeleteOpen(false)}
388 ruleId={props.certMapping.cn?.toString() as string}
389 setIsLoading={setIsDataLoading}
390 onRefresh={props.onRefresh}
391 pathToMainPage={props.pathname}
392 />
393 </>
394 );
395 };
396
397 export default CertificateMappingSettings;