Debian Codegraph @

pkg/users/expiration-dialogs.js

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from 'cockpit';
7 import React from 'react';
8 import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
9 import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js";
10 import { Radio } from "@patternfly/react-core/dist/esm/components/Radio/index.js";
11 import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
12 import { DatePicker } from "@patternfly/react-core/dist/esm/components/DatePicker/index.js";
13
14 import { has_errors } from "./dialog-utils.js";
15 import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx";
16 import * as timeformat from "timeformat";
17 import { FormHelper } from "cockpit-components-form-helper";
18
19 const _ = cockpit.gettext;
20
21 function AccountExpirationDialogBody({ state, errors, change, validate, update }) {
22 const { mode, before, date } = state;
23
24 return (
25 <Form className="expiration-modal" onSubmit={apply_modal_dialog}>
26 <FormGroup validated={errors?.date ? "error" : "default"}>
27 <Radio id="account-expiration-never" name="mode" value="never"
28 label={_("Never expire account")}
29 isChecked={mode == "never"} onChange={() => change("mode", "never")} />
30 <Radio id="account-expiration-expires" name="mode" value="expires"
31 label={
32 <Flex>
33 <span>{before}</span>
34 <DatePicker aria-label={_("Pick date")}
35 buttonAriaLabel={_("Toggle date picker")}
36 locale={timeformat.dateFormatLang()}
37 weekStart={timeformat.firstDayOfWeek()}
38 onChange={(_, str) => change("date", str)}
39 onBlur={() => { validate(); update() }}
40 invalidFormatText=""
41 id="account-expiration-input"
42 value={date}
43 appendTo={() => document.body}
44 isDisabled={mode !== "expires"} />
45 </Flex>
46 }
47 isChecked={mode == "expires"} onChange={() => change("mode", "expires")} />
48 <FormHelper helperTextInvalid={errors?.date} />
49 </FormGroup>
50 </Form>
51 );
52 }
53
54 export function account_expiration_dialog(account, expire_date) {
55 let dlg = null;
56
57 const parts = _("Expire account on");
58
59 const state = {
60 mode: expire_date ? "expires" : "never",
61 before: parts,
62 date: expire_date?.toISOString().substring(0, 10) ?? ""
63 };
64
65 let errors = { };
66
67 function change(field, value) {
68 state[field] = value;
69 errors = { };
70 update();
71 }
72
73 // Datepicker does not provide information about the validity of the date so we need to do it here
74 // https://github.com/patternfly/patternfly-react/issues/5564
75 function validate() {
76 errors = { };
77
78 if (state.mode == "expires") {
79 if (!state.date)
80 errors.date = _("Please specify an expiration date");
81 else {
82 const date = new Date(state.date + "T12:00:00Z");
83 if (isNaN(date.getTime()) || date.getTime() < 0)
84 errors.date = _("Invalid expiration date");
85 }
86 }
87
88 return !has_errors(errors);
89 }
90
91 function update() {
92 const props = {
93 id: "account-expiration",
94 title: _("Account expiration"),
95 body: <AccountExpirationDialogBody state={state} errors={errors} change={change} validate={validate} update={update} />
96 };
97
98 const footer = {
99 actions: [
100 {
101 caption: _("Change"),
102 style: "primary",
103 clicked: () => {
104 if (validate()) {
105 const prog = ["/usr/sbin/usermod", "-e"];
106 if (state.mode == "expires") {
107 const date = new Date(state.date + "T12:00:00Z");
108 prog.push(date.toISOString().substring(0, 10));
109 } else
110 prog.push("");
111 prog.push(account.name);
112 return cockpit.spawn(prog, { superuser: "require", err: "message" });
113 } else {
114 update();
115 return Promise.reject();
116 }
117 }
118 }
119 ]
120 };
121
122 if (!dlg)
123 dlg = show_modal_dialog(props, footer);
124 else {
125 dlg.setProps(props);
126 dlg.setFooterProps(footer);
127 }
128 }
129
130 update();
131 }
132
133 function PasswordExpirationDialogBody({ state, errors, change }) {
134 const { mode, before, after, days } = state;
135
136 return (
137 <Form className="expiration-modal" onSubmit={apply_modal_dialog}>
138 <FormGroup>
139 <Radio id="password-expiration-never" name="mode" value="never"
140 label={_("Never expire password")}
141 isChecked={mode == "never"} onChange={() => change("mode", "never")} />
142 <Radio id="password-expiration-expires" name="mode" value="expires"
143 label={<>
144 <span id="password-expiration-before">{before}</span>
145 <TextInput className="size-text-ct" id="password-expiration-input"
146 validated={(errors?.days) ? "error" : "default"}
147 value={days} onChange={(_event, value) => change("days", value)} isDisabled={mode != "expires"} />
148 <span id="password-expiration-after">{after}</span>
149 </>}
150 isChecked={mode == "expires"} onChange={() => change("mode", "expires")} />
151 <FormHelper helperTextInvalid={errors?.days} />
152 </FormGroup>
153 </Form>
154 );
155 }
156
157 export function password_expiration_dialog(account, expire_days) {
158 let dlg = null;
159
160 /* TRANSLATORS: This is split up and therefore cannot use ngettext plurals */
161 const parts = _("Require password change every $0 days").split("$0");
162
163 const state = {
164 mode: expire_days ? "expires" : "never",
165 before: parts[0],
166 after: parts[1],
167 days: expire_days || ""
168 };
169
170 let errors = { };
171
172 function change(field, value) {
173 state[field] = value;
174 update();
175 }
176
177 function validate() {
178 errors = { };
179
180 if (state.mode == "expires") {
181 const days = parseInt(state.days);
182 if (isNaN(days) || days < 0)
183 errors.days = _("Invalid number of days");
184 }
185
186 return !has_errors(errors);
187 }
188
189 function update() {
190 const props = {
191 id: "password-expiration",
192 title: _("Password expiration"),
193 body: <PasswordExpirationDialogBody state={state} errors={errors} change={change} />,
194 variant: "small"
195 };
196
197 const footer = {
198 actions: [
199 {
200 caption: _("Change"),
201 style: "primary",
202 clicked: () => {
203 if (validate()) {
204 const days = state.mode == "expires" ? parseInt(state.days) : -1;
205 return cockpit.spawn(["passwd", "-x", String(days), account.name],
206 { superuser: "require", err: "message" });
207 } else {
208 update();
209 return Promise.reject();
210 }
211 }
212 }
213 ]
214 };
215
216 if (!dlg)
217 dlg = show_modal_dialog(props, footer);
218 else {
219 dlg.setProps(props);
220 dlg.setFooterProps(footer);
221 }
222 }
223
224 update();
225 }