Debian Codegraph @

pkg/users/rename-group-dialog.jsx

raw
1 /*
2 * Copyright (C) 2023 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 { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js";
9 import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js";
10 import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js";
11
12 import { apply_modal_dialog, show_modal_dialog } from "cockpit-components-dialog.jsx";
13 import { FormHelper } from "cockpit-components-form-helper";
14 import { has_errors, is_valid_char_name } from "./dialog-utils.js";
15
16 const _ = cockpit.gettext;
17
18 function RenameGroupDialogBody({ state, errors, change }) {
19 const { name } = state;
20
21 return (
22 <Form isHorizontal onSubmit={apply_modal_dialog}>
23 <FormGroup fieldId="group-name" label={_("New name")}>
24 <TextInput id="group-name"
25 validated={(errors?.name) ? "error" : "default"}
26 onChange={(_event, val) => change("name", val)} value={name} />
27 <FormHelper fieldId="group-name" helperTextInvalid={errors?.name} />
28 </FormGroup>
29 <HelperText>
30 <HelperTextItem variant="warning">{_("Renaming a group may affect sudo and similar rules")}</HelperTextItem>
31 </HelperText>
32 </Form>
33 );
34 }
35
36 function validate_name(name) {
37 if (!name)
38 return _("Group name cannot be empty");
39
40 for (let i = 0; i < name.length; i++) {
41 if (!is_valid_char_name(name[i]))
42 return _("Group name can only consist of letters, digits, dots, dashes, and underscores");
43 }
44
45 return null;
46 }
47
48 export function rename_group_dialog(group) {
49 let dlg = null;
50
51 const state = {
52 name: group
53 };
54 let errors = { };
55
56 function change(field, value) {
57 state[field] = value;
58 errors = { };
59 update();
60 }
61
62 function update() {
63 const props = {
64 id: "group-confirm-rename-dialog",
65 title: cockpit.format(_("Rename group $0"), group),
66 body: <RenameGroupDialogBody state={state} errors={errors} change={change} />,
67 variant: 'small',
68 };
69
70 const footer = {
71 actions: [
72 {
73 caption: _("Rename"),
74 style: "primary",
75 clicked: () => {
76 errors.name = validate_name(state.name);
77 if (has_errors(errors)) {
78 update();
79 return Promise.reject();
80 }
81 return cockpit.spawn(["groupmod", group, "--new-name", state.name], { superuser: "require", err: "message" }).then(dlg.close);
82 }
83 }
84 ]
85 };
86
87 if (!dlg)
88 dlg = show_modal_dialog(props, footer);
89 else {
90 dlg.setProps(props);
91 dlg.setFooterProps(footer);
92 }
93 }
94
95 update();
96 }