Debian Codegraph @

install/freeipa-webui/src/pages/PasswordPolicies/PasswordPoliciesSettings.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Flex,
5 FlexItem,
6 Form,
7 FormGroup,
8 Sidebar,
9 SidebarContent,
10 SidebarPanel,
11 } from "@patternfly/react-core";
12 // Data types
13 import { PwPolicy, Metadata } from "src/utils/datatypes/globalDataTypes";
14 // Redux
15 import { useAppDispatch } from "src/store/hooks";
16 // Hooks
17 import useUpdateRoute from "src/hooks/useUpdateRoute";
18 import { addAlert } from "src/store/Global/alerts-slice";
19 // Utils
20 import { asRecord } from "src/utils/subIdUtils";
21 // Icons
22 import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons";
23 // RPC
24 import {
25 PwPolicyModPayload,
26 usePwPolicyModMutation,
27 } from "src/services/rpcPwdPolicies";
28 // Components
29 import IpaTextInput from "src/components/Form/IpaTextInput";
30 import TabLayout from "src/components/layouts/TabLayout";
31 import SecondaryButton from "src/components/layouts/SecondaryButton";
32 import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout";
33 import IpaTextContent from "src/components/Form/IpaTextContent";
34
35 interface PropsToPwPolicySettings {
36 pwPolicy: Partial<PwPolicy>;
37 originalPwPolicy: Partial<PwPolicy>;
38 metadata: Metadata;
39 onPwPolicyChange: (pwPolicy: Partial<PwPolicy>) => void;
40 onRefresh: () => void;
41 isModified: boolean;
42 isDataLoading?: boolean;
43 modifiedValues: () => Partial<PwPolicy>;
44 onResetValues: () => void;
45 pathname: string;
46 }
47
48 const PasswordPolicySettings = (props: PropsToPwPolicySettings) => {
49 const dispatch = useAppDispatch();
50
51 // Update current route data to Redux and highlight the current page in the Nav bar
52 useUpdateRoute({ pathname: props.pathname });
53
54 // States
55 const [isDataLoading, setIsDataLoading] = React.useState(false);
56
57 // Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput'
58 const { ipaObject, recordOnChange } = asRecord(
59 props.pwPolicy,
60 props.onPwPolicyChange
61 );
62
63 // API calls
64 const [savePwPolicy] = usePwPolicyModMutation();
65
66 // 'Revert' handler method
67 const onRevert = () => {
68 props.onPwPolicyChange(props.originalPwPolicy);
69 props.onRefresh();
70 dispatch(
71 addAlert({
72 name: "revert-success",
73 title: "Password policy data reverted",
74 variant: "success",
75 })
76 );
77 };
78
79 // Helper method to build the payload based on values
80 const buildPayload = (
81 modifiedValues: Partial<PwPolicy>,
82 keyArray: string[]
83 ): PwPolicyModPayload => {
84 const payload: PwPolicyModPayload = {
85 pwPolicyId: props.pwPolicy.cn as string,
86 };
87
88 keyArray.forEach((key) => {
89 // Modified values are either the value (in string) or an array ([]) when set to empty string
90 if (modifiedValues[key] !== undefined) {
91 if (modifiedValues[key] === "") {
92 payload[key] = [];
93 } else {
94 payload[key] = modifiedValues[key];
95 }
96 }
97 });
98 return payload;
99 };
100
101 // on Save handler method
102 const onSave = () => {
103 setIsDataLoading(true);
104 const modifiedValues = props.modifiedValues();
105
106 // Generate payload
107 const payload = buildPayload(modifiedValues, [
108 "krbmaxpwdlife",
109 "krbminpwdlife",
110 "krbpwdhistorylength",
111 "krbpwdmindiffchars",
112 "krbpwdminlength",
113 "krbpwdmaxfailure",
114 "krbpwdfailurecountinterval",
115 "krbpwdlockoutduration",
116 "cospriority",
117 "passwordgracelimit",
118 ]);
119
120 savePwPolicy(payload).then((response) => {
121 if ("data" in response) {
122 const data = response.data;
123 if (data?.error) {
124 dispatch(
125 addAlert({
126 name: "error",
127 title: (data.error as Error).message,
128 variant: "danger",
129 })
130 );
131 }
132 if (data?.result) {
133 props.onPwPolicyChange(data.result.result);
134 dispatch(
135 addAlert({
136 name: "success",
137 title: "Password policy '" + props.pwPolicy.cn + "' updated",
138 variant: "success",
139 })
140 );
141 // Reset values. Disable 'revert' and 'save' buttons
142 props.onResetValues();
143 }
144 }
145 setIsDataLoading(false);
146 });
147 };
148
149 // Toolbar
150 const toolbarFields = [
151 {
152 key: 0,
153 element: (
154 <SecondaryButton
155 onClickHandler={props.onRefresh}
156 dataCy="password-policies-button-refresh"
157 >
158 Refresh
159 </SecondaryButton>
160 ),
161 },
162 {
163 key: 1,
164 element: (
165 <SecondaryButton
166 isDisabled={!props.isModified || isDataLoading}
167 onClickHandler={onRevert}
168 dataCy="password-policies-button-revert"
169 >
170 Revert
171 </SecondaryButton>
172 ),
173 },
174 {
175 key: 2,
176 element: (
177 <SecondaryButton
178 isDisabled={!props.isModified || isDataLoading}
179 onClickHandler={onSave}
180 dataCy="password-policies-button-save"
181 >
182 Save
183 </SecondaryButton>
184 ),
185 },
186 ];
187
188 // Render component
189 return (
190 <>
191 <TabLayout id="settings-page" toolbarItems={toolbarFields}>
192 <Sidebar isPanelRight>
193 <SidebarPanel variant="sticky">
194 <HelpTextWithIconLayout
195 textContent="Help"
196 icon={
197 <OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" />
198 }
199 />
200 </SidebarPanel>
201 <SidebarContent className="pf-v6-u-mr-xl">
202 <Flex direction={{ default: "column", lg: "row" }}>
203 <FlexItem flex={{ default: "flex_1" }}>
204 <Form className="pf-v6-u-mb-lg">
205 <FormGroup label="Group" fieldId="group" role="group">
206 <IpaTextContent
207 dataCy="password-policies-text-group"
208 name={"cn"}
209 ariaLabel={"Group"}
210 ipaObject={ipaObject}
211 objectName="pwpolicy"
212 metadata={props.metadata}
213 linkTo={
214 "/password-policies/" + props.pwPolicy.cn?.toString()
215 }
216 />
217 </FormGroup>
218 <FormGroup
219 label="Max lifetime (days)"
220 fieldId="krbmaxpwdlife"
221 >
222 <IpaTextInput
223 dataCy="password-policies-tab-settings-textbox-max-lifetime"
224 name={"krbmaxpwdlife"}
225 ariaLabel={"Max lifetime in days"}
226 ipaObject={ipaObject}
227 onChange={recordOnChange}
228 objectName="pwpolicy"
229 metadata={props.metadata}
230 />
231 </FormGroup>
232 <FormGroup
233 label="Min lifetime (hours)"
234 fieldId="krbminpwdlife"
235 >
236 <IpaTextInput
237 dataCy="password-policies-tab-settings-textbox-min-lifetime"
238 name={"krbminpwdlife"}
239 ariaLabel={"Min lifetime in hours"}
240 ipaObject={ipaObject}
241 onChange={recordOnChange}
242 objectName="pwpolicy"
243 metadata={props.metadata}
244 />
245 </FormGroup>
246 <FormGroup
247 label="History size (number of passwords)"
248 fieldId="krbpwdhistorylength"
249 >
250 <IpaTextInput
251 dataCy="password-policies-tab-settings-textbox-history-size"
252 name={"krbpwdhistorylength"}
253 ariaLabel={"History size by number of passwords"}
254 ipaObject={ipaObject}
255 onChange={recordOnChange}
256 objectName="pwpolicy"
257 metadata={props.metadata}
258 />
259 </FormGroup>
260 <FormGroup
261 label="Character classes"
262 fieldId="krbpwdmindiffchars"
263 >
264 <IpaTextInput
265 dataCy="password-policies-tab-settings-textbox-character-classes"
266 name={"krbpwdmindiffchars"}
267 ariaLabel={"Character classes"}
268 ipaObject={ipaObject}
269 onChange={recordOnChange}
270 objectName="pwpolicy"
271 metadata={props.metadata}
272 />
273 </FormGroup>
274 <FormGroup label="Min length" fieldId="krbpwdminlength">
275 <IpaTextInput
276 dataCy="password-policies-tab-settings-textbox-min-length"
277 name={"krbpwdminlength"}
278 ariaLabel={"Min length"}
279 ipaObject={ipaObject}
280 onChange={recordOnChange}
281 objectName="pwpolicy"
282 metadata={props.metadata}
283 />
284 </FormGroup>
285 <FormGroup label="Max failures" fieldId="krbpwdmaxfailure">
286 <IpaTextInput
287 dataCy="password-policies-tab-settings-textbox-max-failures"
288 name={"krbpwdmaxfailure"}
289 ariaLabel={"Max failures"}
290 ipaObject={ipaObject}
291 onChange={recordOnChange}
292 objectName="pwpolicy"
293 metadata={props.metadata}
294 />
295 </FormGroup>
296 <FormGroup
297 label="Failure reset interval (seconds)"
298 fieldId="krbpwdfailurecountinterval"
299 >
300 <IpaTextInput
301 dataCy="password-policies-tab-settings-textbox-failure-reset-interval"
302 name={"krbpwdfailurecountinterval"}
303 ariaLabel={"Failure reset interval in seconds"}
304 ipaObject={ipaObject}
305 onChange={recordOnChange}
306 objectName="pwpolicy"
307 metadata={props.metadata}
308 />
309 </FormGroup>
310 <FormGroup
311 label="Lockout duration (seconds)"
312 fieldId="krbpwdlockoutduration"
313 >
314 <IpaTextInput
315 dataCy="password-policies-tab-settings-textbox-lockout-duration"
316 name={"krbpwdlockoutduration"}
317 ariaLabel={"Lockout duration in seconds"}
318 ipaObject={ipaObject}
319 onChange={recordOnChange}
320 objectName="pwpolicy"
321 metadata={props.metadata}
322 />
323 </FormGroup>
324 <FormGroup label="Priority" fieldId="cospriority" isRequired>
325 <IpaTextInput
326 dataCy="password-policies-tab-settings-textbox-priority"
327 name={"cospriority"}
328 ariaLabel={"Priority"}
329 ipaObject={ipaObject}
330 onChange={recordOnChange}
331 objectName="pwpolicy"
332 metadata={props.metadata}
333 />
334 </FormGroup>
335 <FormGroup
336 label="Grace login limit"
337 fieldId="passwordgracelimit"
338 >
339 <IpaTextInput
340 dataCy="password-policies-tab-settings-textbox-grace-login-limit"
341 name={"passwordgracelimit"}
342 ariaLabel={"Grace login limit"}
343 ipaObject={ipaObject}
344 onChange={recordOnChange}
345 objectName="pwpolicy"
346 metadata={props.metadata}
347 />
348 </FormGroup>
349 </Form>
350 </FlexItem>
351 </Flex>
352 </SidebarContent>
353 </Sidebar>
354 </TabLayout>
355 </>
356 );
357 };
358
359 export default PasswordPolicySettings;