| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
Form, |
| 6 |
FormGroup, |
| 7 |
Label, |
| 8 |
Tab, |
| 9 |
Tabs, |
| 10 |
TabTitleText, |
| 11 |
} from "@patternfly/react-core"; |
| 12 |
// Forms |
| 13 |
import IpaTextArea from "src/components/Form/IpaTextArea"; |
| 14 |
import IpaTextInput from "src/components/Form/IpaTextInput"; |
| 15 |
import IpaCheckbox from "src/components/Form/IpaCheckbox"; |
| 16 |
// Layouts |
| 17 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 18 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 19 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 20 |
// Utils |
| 21 |
import { asRecord } from "../../utils/hostUtils"; |
| 22 |
// Redux |
| 23 |
import { useAppDispatch } from "src/store/hooks"; |
| 24 |
// Hooks |
| 25 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 26 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 27 |
// Data types |
| 28 |
import { Netgroup, Metadata } from "../../utils/datatypes/globalDataTypes"; |
| 29 |
import NetgroupsMemberTable from "./NetgroupsMemberTable"; |
| 30 |
// RPC |
| 31 |
import { ErrorResult } from "src/services/rpc"; |
| 32 |
import { |
| 33 |
useSaveAndCleanNetgroupMutation, |
| 34 |
AllowAllPayload, |
| 35 |
} from "src/services/rpcNetgroups"; |
| 36 |
|
| 37 |
interface PropsToGroupsSettings { |
| 38 |
netgroup: Partial<Netgroup>; |
| 39 |
originalGroup: Partial<Netgroup>; |
| 40 |
metadata: Metadata; |
| 41 |
onGroupChange: (netgroup: Partial<Netgroup>) => void; |
| 42 |
onRefresh: () => void; |
| 43 |
isModified: boolean; |
| 44 |
isDataLoading?: boolean; |
| 45 |
modifiedValues: () => Partial<Netgroup>; |
| 46 |
onResetValues: () => void; |
| 47 |
} |
| 48 |
|
| 49 |
const NetgroupsSettings = (props: PropsToGroupsSettings) => { |
| 50 |
const dispatch = useAppDispatch(); |
| 51 |
|
| 52 |
// API |
| 53 |
const [saveGroup] = useSaveAndCleanNetgroupMutation(); |
| 54 |
|
| 55 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 56 |
useUpdateRoute({ pathname: "netgroups", noBreadcrumb: true }); |
| 57 |
|
| 58 |
const [cn, setNetgroupName] = useState<string>(""); |
| 59 |
const [memberUsers, setMemberUsers] = useState<string[]>([]); |
| 60 |
const [memberGroups, setMemberGroups] = useState<string[]>([]); |
| 61 |
const [memberHosts, setMemberHosts] = useState<string[]>([]); |
| 62 |
const [memberHostGroups, setMemberHostGroups] = useState<string[]>([]); |
| 63 |
const [memberExternalHosts, setMemberExternalHosts] = useState<string[]>([]); |
| 64 |
|
| 65 |
React.useEffect(() => { |
| 66 |
// Process the member users and hosts |
| 67 |
let externalHosts: string[] = []; |
| 68 |
|
| 69 |
setNetgroupName(props.netgroup.cn ? props.netgroup.cn : ""); |
| 70 |
if (props.netgroup.memberuser_user) { |
| 71 |
setMemberUsers(props.netgroup.memberuser_user); |
| 72 |
} |
| 73 |
if (props.netgroup.memberuser_group) { |
| 74 |
setMemberGroups(props.netgroup.memberuser_group); |
| 75 |
} |
| 76 |
if (props.netgroup.memberhost_host) { |
| 77 |
setMemberHosts(props.netgroup.memberhost_host); |
| 78 |
} |
| 79 |
if (props.netgroup.memberhost_hostgroup) { |
| 80 |
setMemberHostGroups(props.netgroup.memberhost_hostgroup); |
| 81 |
} |
| 82 |
externalHosts = props.netgroup.externalhost |
| 83 |
? [...props.netgroup.externalhost] |
| 84 |
: []; |
| 85 |
setMemberExternalHosts(externalHosts); |
| 86 |
}, [props.netgroup]); |
| 87 |
|
| 88 |
// Tabs |
| 89 |
const [userTabKey, setUserTabKey] = useState(0); |
| 90 |
const [hostTabKey, setHostTabKey] = useState(0); |
| 91 |
const handleUserTabClick = ( |
| 92 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 93 |
tabIndex: number | string |
| 94 |
) => { |
| 95 |
setUserTabKey(tabIndex as number); |
| 96 |
}; |
| 97 |
const handleHostTabClick = ( |
| 98 |
_event: React.MouseEvent<HTMLElement, MouseEvent>, |
| 99 |
tabIndex: number | string |
| 100 |
) => { |
| 101 |
setHostTabKey(tabIndex as number); |
| 102 |
}; |
| 103 |
|
| 104 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 105 |
const { ipaObject, recordOnChange } = asRecord( |
| 106 |
props.netgroup, |
| 107 |
props.onGroupChange |
| 108 |
); |
| 109 |
|
| 110 |
const [isSaving, setSaving] = useState(false); |
| 111 |
|
| 112 |
// 'Save' handler method |
| 113 |
const onSave = () => { |
| 114 |
const modifiedValues = props.modifiedValues(); |
| 115 |
const payload = { |
| 116 |
groupName: cn, |
| 117 |
users: memberUsers, |
| 118 |
groups: memberGroups, |
| 119 |
hosts: memberHosts, |
| 120 |
hostgroups: memberHostGroups, |
| 121 |
external: memberExternalHosts, |
| 122 |
modifiedValues, |
| 123 |
} as AllowAllPayload; |
| 124 |
|
| 125 |
setSaving(true); |
| 126 |
saveGroup(payload).then((response) => { |
| 127 |
if ("data" in response) { |
| 128 |
if (response.data?.result) { |
| 129 |
// Show toast notification: success |
| 130 |
dispatch( |
| 131 |
addAlert({ |
| 132 |
name: "save-success", |
| 133 |
title: "Netgroup modified", |
| 134 |
variant: "success", |
| 135 |
}) |
| 136 |
); |
| 137 |
props.onRefresh(); |
| 138 |
setHostTabKey(0); |
| 139 |
setUserTabKey(0); |
| 140 |
} else if (response.data?.error) { |
| 141 |
// Show toast notification: error |
| 142 |
const errorMessage = response.data.error as ErrorResult; |
| 143 |
dispatch( |
| 144 |
addAlert({ |
| 145 |
name: "save-error", |
| 146 |
title: errorMessage.message, |
| 147 |
variant: "danger", |
| 148 |
}) |
| 149 |
); |
| 150 |
props.onResetValues(); |
| 151 |
} |
| 152 |
} |
| 153 |
setSaving(false); |
| 154 |
}); |
| 155 |
}; |
| 156 |
|
| 157 |
// 'Revert' handler method |
| 158 |
const onRevert = () => { |
| 159 |
props.onGroupChange(props.originalGroup); |
| 160 |
dispatch( |
| 161 |
addAlert({ |
| 162 |
name: "revert-success", |
| 163 |
title: "Netgroup data reverted", |
| 164 |
variant: "success", |
| 165 |
}) |
| 166 |
); |
| 167 |
}; |
| 168 |
|
| 169 |
// Toolbar |
| 170 |
const toolbarFields = [ |
| 171 |
{ |
| 172 |
key: 0, |
| 173 |
element: ( |
| 174 |
<SecondaryButton |
| 175 |
dataCy="netgroups-tab-settings-button-refresh" |
| 176 |
onClickHandler={props.onRefresh} |
| 177 |
> |
| 178 |
Refresh |
| 179 |
</SecondaryButton> |
| 180 |
), |
| 181 |
}, |
| 182 |
{ |
| 183 |
key: 1, |
| 184 |
element: ( |
| 185 |
<SecondaryButton |
| 186 |
dataCy="netgroups-tab-settings-button-revert" |
| 187 |
isDisabled={!props.isModified} |
| 188 |
onClickHandler={onRevert} |
| 189 |
> |
| 190 |
Revert |
| 191 |
</SecondaryButton> |
| 192 |
), |
| 193 |
}, |
| 194 |
{ |
| 195 |
key: 2, |
| 196 |
element: ( |
| 197 |
<SecondaryButton |
| 198 |
dataCy="netgroups-tab-settings-button-save" |
| 199 |
isDisabled={!props.isModified || isSaving} |
| 200 |
onClickHandler={onSave} |
| 201 |
isLoading={isSaving} |
| 202 |
spinnerAriaValueText="Saving" |
| 203 |
spinnerAriaLabelledBy="Saving" |
| 204 |
spinnerAriaLabel="Saving" |
| 205 |
> |
| 206 |
{isSaving ? "Saving" : "Save"} |
| 207 |
</SecondaryButton> |
| 208 |
), |
| 209 |
}, |
| 210 |
]; |
| 211 |
|
| 212 |
// Render component |
| 213 |
return ( |
| 214 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 215 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 216 |
<TitleLayout |
| 217 |
key={0} |
| 218 |
headingLevel="h1" |
| 219 |
id="group-settings" |
| 220 |
text="Netgroup settings" |
| 221 |
/> |
| 222 |
<Form |
| 223 |
className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md" |
| 224 |
isHorizontal |
| 225 |
> |
| 226 |
<FormGroup label="Description" fieldId="description"> |
| 227 |
<IpaTextArea |
| 228 |
dataCy="netgroups-tab-settings-textbox-description" |
| 229 |
name="description" |
| 230 |
ipaObject={ipaObject} |
| 231 |
onChange={recordOnChange} |
| 232 |
objectName="netgroup" |
| 233 |
metadata={props.metadata} |
| 234 |
/> |
| 235 |
</FormGroup> |
| 236 |
<FormGroup label="NIS domain name" fieldId="nisdomainname"> |
| 237 |
<IpaTextInput |
| 238 |
dataCy="netgroups-tab-settings-textbox-nisdomainname" |
| 239 |
name="nisdomainname" |
| 240 |
ariaLabel={"NIS domain name"} |
| 241 |
ipaObject={ipaObject} |
| 242 |
onChange={recordOnChange} |
| 243 |
objectName="netgroup" |
| 244 |
metadata={props.metadata} |
| 245 |
/> |
| 246 |
</FormGroup> |
| 247 |
</Form> |
| 248 |
<TitleLayout |
| 249 |
key={1} |
| 250 |
headingLevel="h2" |
| 251 |
id="user-category-tabs" |
| 252 |
text="User category" |
| 253 |
className="pf-v6-u-mt-lg" |
| 254 |
/> |
| 255 |
<IpaCheckbox |
| 256 |
dataCy="netgroups-tab-settings-checkbox-usercategory" |
| 257 |
name="usercategory" |
| 258 |
value="usercategory" |
| 259 |
text="Allow anyone" |
| 260 |
className="pf-v6-u-ml-lg" |
| 261 |
ipaObject={ipaObject} |
| 262 |
onChange={recordOnChange} |
| 263 |
objectName="netgroup" |
| 264 |
metadata={props.metadata} |
| 265 |
altTrue="all" |
| 266 |
altFalse={""} |
| 267 |
/> |
| 268 |
{ipaObject.usercategory === "" && ( |
| 269 |
<Tabs |
| 270 |
activeKey={userTabKey} |
| 271 |
onSelect={handleUserTabClick} |
| 272 |
className="pf-v6-u-ml-md pf-v6-u-mr-md" |
| 273 |
> |
| 274 |
<Tab |
| 275 |
eventKey={0} |
| 276 |
name="users" |
| 277 |
data-cy="netgroups-tab-settings-tab-users" |
| 278 |
title={ |
| 279 |
<TabTitleText> |
| 280 |
Users <Label isCompact>{memberUsers.length}</Label> |
| 281 |
</TabTitleText> |
| 282 |
} |
| 283 |
> |
| 284 |
<NetgroupsMemberTable |
| 285 |
from="user" |
| 286 |
id={cn} |
| 287 |
members={memberUsers} |
| 288 |
onRefresh={props.onRefresh} |
| 289 |
unsetCategory={ |
| 290 |
ipaObject.usercategory !== props.originalGroup.usercategory |
| 291 |
} |
| 292 |
/> |
| 293 |
</Tab> |
| 294 |
<Tab |
| 295 |
eventKey={1} |
| 296 |
name="groups" |
| 297 |
data-cy="netgroups-tab-settings-tab-groups" |
| 298 |
title={ |
| 299 |
<TabTitleText> |
| 300 |
Groups <Label isCompact>{memberGroups.length}</Label> |
| 301 |
</TabTitleText> |
| 302 |
} |
| 303 |
> |
| 304 |
<NetgroupsMemberTable |
| 305 |
from="group" |
| 306 |
id={cn} |
| 307 |
members={memberGroups} |
| 308 |
onRefresh={props.onRefresh} |
| 309 |
unsetCategory={ |
| 310 |
ipaObject.usercategory !== props.originalGroup.usercategory |
| 311 |
} |
| 312 |
/> |
| 313 |
</Tab> |
| 314 |
</Tabs> |
| 315 |
)} |
| 316 |
<TitleLayout |
| 317 |
key={2} |
| 318 |
headingLevel="h2" |
| 319 |
id="host-category-tabs" |
| 320 |
text="Host category" |
| 321 |
className="pf-v6-u-mt-xl" |
| 322 |
/> |
| 323 |
<IpaCheckbox |
| 324 |
dataCy="netgroups-tab-settings-checkbox-hostcategory" |
| 325 |
name="hostcategory" |
| 326 |
value="hostcategory" |
| 327 |
text="Allow any host" |
| 328 |
className="pf-v6-u-ml-lg" |
| 329 |
ipaObject={ipaObject} |
| 330 |
onChange={recordOnChange} |
| 331 |
objectName="netgroup" |
| 332 |
metadata={props.metadata} |
| 333 |
altTrue="all" |
| 334 |
altFalse={""} |
| 335 |
/> |
| 336 |
{ipaObject.hostcategory === "" && ( |
| 337 |
<Tabs |
| 338 |
activeKey={hostTabKey} |
| 339 |
onSelect={handleHostTabClick} |
| 340 |
className="pf-v6-u-ml-md pf-v6-u-mr-md" |
| 341 |
> |
| 342 |
<Tab |
| 343 |
eventKey={0} |
| 344 |
name="memberHosts" |
| 345 |
data-cy="netgroups-tab-settings-tab-hosts" |
| 346 |
title={ |
| 347 |
<TabTitleText> |
| 348 |
Hosts <Label isCompact>{memberHosts.length}</Label> |
| 349 |
</TabTitleText> |
| 350 |
} |
| 351 |
> |
| 352 |
<NetgroupsMemberTable |
| 353 |
from="host" |
| 354 |
id={cn} |
| 355 |
members={memberHosts} |
| 356 |
onRefresh={props.onRefresh} |
| 357 |
unsetCategory={ |
| 358 |
ipaObject.hostcategory !== props.originalGroup.hostcategory |
| 359 |
} |
| 360 |
/> |
| 361 |
</Tab> |
| 362 |
<Tab |
| 363 |
eventKey={1} |
| 364 |
name="memberHostGroups" |
| 365 |
data-cy="netgroups-tab-settings-tab-hostgroups" |
| 366 |
title={ |
| 367 |
<TabTitleText> |
| 368 |
Host groups <Label isCompact>{memberHostGroups.length}</Label> |
| 369 |
</TabTitleText> |
| 370 |
} |
| 371 |
> |
| 372 |
<NetgroupsMemberTable |
| 373 |
from="hostgroup" |
| 374 |
id={cn} |
| 375 |
members={memberHostGroups} |
| 376 |
onRefresh={props.onRefresh} |
| 377 |
fromLabel={"Host group"} |
| 378 |
unsetCategory={ |
| 379 |
ipaObject.hostcategory !== props.originalGroup.hostcategory |
| 380 |
} |
| 381 |
/> |
| 382 |
</Tab> |
| 383 |
<Tab |
| 384 |
eventKey={2} |
| 385 |
name="memberExternalHosts" |
| 386 |
data-cy="netgroups-tab-settings-tab-externalhosts" |
| 387 |
title={ |
| 388 |
<TabTitleText> |
| 389 |
External hosts{" "} |
| 390 |
<Label isCompact>{memberExternalHosts.length}</Label> |
| 391 |
</TabTitleText> |
| 392 |
} |
| 393 |
> |
| 394 |
<NetgroupsMemberTable |
| 395 |
from="externalHost" |
| 396 |
id={cn} |
| 397 |
members={memberExternalHosts} |
| 398 |
onRefresh={props.onRefresh} |
| 399 |
fromLabel={"External host"} |
| 400 |
unsetCategory={ |
| 401 |
ipaObject.hostcategory !== props.originalGroup.hostcategory |
| 402 |
} |
| 403 |
/> |
| 404 |
</Tab> |
| 405 |
</Tabs> |
| 406 |
)} |
| 407 |
</Flex> |
| 408 |
</TabLayout> |
| 409 |
); |
| 410 |
}; |
| 411 |
|
| 412 |
export default NetgroupsSettings; |