| 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 |
|
| 9 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 10 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 11 |
import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx"; |
| 12 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 13 |
|
| 14 |
import { has_errors, is_valid_char_name } from "./dialog-utils.js"; |
| 15 |
|
| 16 |
const _ = cockpit.gettext; |
| 17 |
|
| 18 |
function GroupCreateBody({ state, errors, change }) { |
| 19 |
const { |
| 20 |
name, id, |
| 21 |
} = state; |
| 22 |
|
| 23 |
return ( |
| 24 |
<Form isHorizontal onSubmit={apply_modal_dialog}> |
| 25 |
<FormGroup label={_("Name")} |
| 26 |
fieldId="groups-create-name"> |
| 27 |
<TextInput id="groups-create-name" |
| 28 |
validated={(errors?.name) ? "error" : "default"} |
| 29 |
value={name} onChange={(_event, value) => change("name", value)} /> |
| 30 |
<FormHelper fieldId="groups-create-name" helperTextInvalid={errors?.name} /> |
| 31 |
</FormGroup> |
| 32 |
|
| 33 |
<FormGroup label={_("ID")} |
| 34 |
hasNoPaddingTop |
| 35 |
isStack |
| 36 |
fieldId="groups-create-id"> |
| 37 |
<TextInput id="groups-create-id" |
| 38 |
validated={(errors?.id) ? "error" : "default"} |
| 39 |
value={id} onChange={(_event, value) => change("id", value)} /> |
| 40 |
<FormHelper fieldId="groups-create-id" helperTextInvalid={errors?.id} /> |
| 41 |
</FormGroup> |
| 42 |
</Form> |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
function validate_name(name, groups) { |
| 47 |
if (!name) |
| 48 |
return _("No group name specified"); |
| 49 |
|
| 50 |
for (let i = 0; i < name.length; i++) { |
| 51 |
if (!is_valid_char_name(name[i])) |
| 52 |
return _("Group name can only consist of letters, digits, dots, dashes, and underscores"); |
| 53 |
} |
| 54 |
|
| 55 |
for (let k = 0; k < groups.length; k++) { |
| 56 |
if (groups[k].name == name) |
| 57 |
return _("A group with this name already exists"); |
| 58 |
} |
| 59 |
|
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
function validate_group(id, groups) { |
| 64 |
if (!id) |
| 65 |
return _("No ID specified"); |
| 66 |
|
| 67 |
const id_num = parseInt(id); |
| 68 |
if (id_num.toString() !== id || id_num < 0) |
| 69 |
return _("Group ID must be positive integer"); |
| 70 |
|
| 71 |
return null; |
| 72 |
} |
| 73 |
|
| 74 |
export function group_create_dialog(groups, setGroupsCardExpanded, min_gid, max_gid) { |
| 75 |
let dlg = null; |
| 76 |
const state = { |
| 77 |
name: "", |
| 78 |
id: "", |
| 79 |
}; |
| 80 |
let errors = { }; |
| 81 |
|
| 82 |
const gids = groups |
| 83 |
.filter(g => g.name !== 'nobody') |
| 84 |
.map(group => group.gid); |
| 85 |
|
| 86 |
change("id", (Math.max(min_gid, Math.max(...gids.filter(id => id < max_gid)) + 1) + 1).toString()); |
| 87 |
|
| 88 |
function change(field, value) { |
| 89 |
state[field] = value; |
| 90 |
errors = { }; |
| 91 |
|
| 92 |
update(); |
| 93 |
} |
| 94 |
|
| 95 |
function validate(name, id) { |
| 96 |
const errs = { }; |
| 97 |
|
| 98 |
errs.name = validate_name(name, groups); |
| 99 |
errs.id = validate_group(id, groups); |
| 100 |
errors = errs; |
| 101 |
|
| 102 |
return !has_errors(errs); |
| 103 |
} |
| 104 |
|
| 105 |
function create(name, id) { |
| 106 |
const valid = validate(name, id); |
| 107 |
if (valid) { |
| 108 |
const group_add_cmd = ["groupadd", name, "-g", id]; |
| 109 |
|
| 110 |
return cockpit.spawn(group_add_cmd, { superuser: "require", err: "message" }); |
| 111 |
} else { |
| 112 |
update(); |
| 113 |
return Promise.reject(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
function update() { |
| 118 |
const props = { |
| 119 |
id: "groups-create-dialog", |
| 120 |
title: _("Create new group"), |
| 121 |
body: <GroupCreateBody state={state} errors={errors} change={change} /> |
| 122 |
}; |
| 123 |
|
| 124 |
const footer = { |
| 125 |
actions: [ |
| 126 |
{ |
| 127 |
caption: _("Create"), |
| 128 |
style: "primary", |
| 129 |
clicked: () => create(state.name, state.id).then(() => setGroupsCardExpanded(true)) |
| 130 |
} |
| 131 |
] |
| 132 |
}; |
| 133 |
|
| 134 |
if (!dlg) |
| 135 |
dlg = show_modal_dialog(props, footer); |
| 136 |
else { |
| 137 |
dlg.setProps(props); |
| 138 |
dlg.setFooterProps(footer); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
update(); |
| 143 |
} |