Debian Codegraph @

pkg/users/shell-dialog.js

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 { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js";
10
11 import { has_errors } from "./dialog-utils.js";
12 import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx";
13
14 const _ = cockpit.gettext;
15
16 function ShellDialogBody({ state, errors, change, shells }) {
17 const { shell } = state;
18 return (
19 <Form className="shell-modal" onSubmit={apply_modal_dialog}>
20 <FormGroup fieldId="edit-user-shell">
21 <FormSelect
22 data-selected={shell}
23 id="edit-user-shell"
24 onChange={(_, selection) => { change("shell", selection) }}
25 value={shell}>
26 { shells.map(shell_path => <FormSelectOption key={shell_path} value={shell_path} label={shell_path} />) }
27 </FormSelect>
28 </FormGroup>
29 </Form>
30 );
31 }
32
33 export function account_shell_dialog(account, shells) {
34 let dlg = null;
35
36 const state = {
37 shell: account.shell,
38 };
39
40 let errors = { };
41
42 function change(field, value) {
43 state[field] = value;
44 update();
45 }
46
47 function validate() {
48 errors = { };
49
50 return !has_errors(errors);
51 }
52
53 function update() {
54 const props = {
55 id: "shell-dialog",
56 title: _("Change shell"),
57 body: <ShellDialogBody state={state} errors={errors} change={change} shells={shells} />,
58 variant: "small"
59 };
60
61 const footer = {
62 actions: [
63 {
64 caption: _("Change"),
65 style: "primary",
66 clicked: () => {
67 if (validate()) {
68 return cockpit.spawn(["usermod", "--shell", state.shell, account.name],
69 { superuser: "require", err: "message" });
70 } else {
71 update();
72 return Promise.reject();
73 }
74 }
75 }
76 ]
77 };
78
79 if (!dlg)
80 dlg = show_modal_dialog(props, footer);
81 else {
82 dlg.setProps(props);
83 dlg.setFooterProps(footer);
84 }
85 }
86
87 update();
88 }