| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Divider, |
| 6 |
DropdownItem, |
| 7 |
Flex, |
| 8 |
Form, |
| 9 |
FormGroup, |
| 10 |
Label, |
| 11 |
TextInput, |
| 12 |
} from "@patternfly/react-core"; |
| 13 |
// Modals |
| 14 |
import ConfirmationModal from "../../components/modals/ConfirmationModal"; |
| 15 |
// Forms |
| 16 |
import IpaTextArea from "../../components/Form/IpaTextArea"; |
| 17 |
import { useNavigate } from "react-router"; |
| 18 |
// Layouts |
| 19 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 20 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 21 |
import KebabLayout from "src/components/layouts/KebabLayout"; |
| 22 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 23 |
// Utils |
| 24 |
import { asRecord } from "../../utils/hostUtils"; |
| 25 |
// Redux |
| 26 |
import { useAppDispatch } from "src/store/hooks"; |
| 27 |
// Hooks |
| 28 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 29 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 30 |
// Data types |
| 31 |
import { |
| 32 |
UserGroup, |
| 33 |
Metadata, |
| 34 |
PwPolicy, |
| 35 |
} from "../../utils/datatypes/globalDataTypes"; |
| 36 |
// RPC |
| 37 |
import { ErrorResult } from "src/services/rpc"; |
| 38 |
import { |
| 39 |
useRemoveGroupMutation, |
| 40 |
useSaveGroupMutation, |
| 41 |
useConvertGroupExternalMutation, |
| 42 |
useConvertGroupPOSIXMutation, |
| 43 |
} from "src/services/rpcUserGroups"; |
| 44 |
|
| 45 |
interface PropsToGroupsSettings { |
| 46 |
userGroup: Partial<UserGroup>; |
| 47 |
originalGroup: Partial<UserGroup>; |
| 48 |
metadata: Metadata; |
| 49 |
onGroupChange: (userGroup: Partial<UserGroup>) => void; |
| 50 |
onRefresh: () => void; |
| 51 |
isModified: boolean; |
| 52 |
isDataLoading?: boolean; |
| 53 |
modifiedValues: () => Partial<UserGroup>; |
| 54 |
onResetValues: () => void; |
| 55 |
pwPolicyData: Partial<PwPolicy>; |
| 56 |
} |
| 57 |
|
| 58 |
const UserGroupsSettings = (props: PropsToGroupsSettings) => { |
| 59 |
const navigate = useNavigate(); |
| 60 |
|
| 61 |
const dispatch = useAppDispatch(); |
| 62 |
|
| 63 |
// API |
| 64 |
const [saveGroup] = useSaveGroupMutation(); |
| 65 |
const [deleteGroup] = useRemoveGroupMutation(); |
| 66 |
const [convertGroupExternal] = useConvertGroupExternalMutation(); |
| 67 |
const [convertGroupPOSIX] = useConvertGroupPOSIXMutation(); |
| 68 |
|
| 69 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 70 |
useUpdateRoute({ pathname: "user-groups", noBreadcrumb: true }); |
| 71 |
|
| 72 |
let cn = ""; |
| 73 |
if (props.userGroup.cn !== undefined) { |
| 74 |
cn = props.userGroup.cn; |
| 75 |
} |
| 76 |
|
| 77 |
let groupType = ""; |
| 78 |
if (props.userGroup.objectclass) { |
| 79 |
if (props.userGroup.objectclass.includes("posixgroup")) { |
| 80 |
groupType = "POSIX"; |
| 81 |
} else if (props.userGroup.objectclass.includes("ipaexternalgroup")) { |
| 82 |
groupType = "External"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
const [isSaving, setSaving] = useState(false); |
| 87 |
|
| 88 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 89 |
const { ipaObject, recordOnChange } = asRecord( |
| 90 |
props.userGroup, |
| 91 |
props.onGroupChange |
| 92 |
); |
| 93 |
|
| 94 |
// Kebab |
| 95 |
const [isKebabOpen, setIsKebabOpen] = useState(false); |
| 96 |
|
| 97 |
const dropdownItems = [ |
| 98 |
<DropdownItem |
| 99 |
key="delete-user-group" |
| 100 |
onClick={() => onOpenDeleteModal()} |
| 101 |
data-cy="user-groups-tab-settings-kebab-delete" |
| 102 |
> |
| 103 |
Delete group |
| 104 |
</DropdownItem>, |
| 105 |
]; |
| 106 |
if (groupType === "") { |
| 107 |
dropdownItems.push(<Divider key="separator" />); |
| 108 |
dropdownItems.push( |
| 109 |
<DropdownItem |
| 110 |
key="change-to-posix" |
| 111 |
onClick={() => onOpenPOSIXModal()} |
| 112 |
data-cy="user-groups-tab-settings-kebab-change-to-posix" |
| 113 |
> |
| 114 |
Change to POSIX group |
| 115 |
</DropdownItem> |
| 116 |
); |
| 117 |
dropdownItems.push( |
| 118 |
<DropdownItem |
| 119 |
key="change-to-external" |
| 120 |
onClick={() => onOpenExternalModal()} |
| 121 |
data-cy="user-groups-tab-settings-kebab-change-to-external" |
| 122 |
> |
| 123 |
Change to external group |
| 124 |
</DropdownItem> |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
const onKebabToggle = () => { |
| 129 |
setIsKebabOpen(!isKebabOpen); |
| 130 |
}; |
| 131 |
|
| 132 |
const onKebabSelect = ( |
| 133 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 134 |
_event: React.MouseEvent<Element, MouseEvent> | undefined |
| 135 |
) => { |
| 136 |
setIsKebabOpen(!isKebabOpen); |
| 137 |
}; |
| 138 |
|
| 139 |
// Confirmation modals |
| 140 |
const [modalSpinning, setModalSpinning] = React.useState(false); |
| 141 |
const [isDeleteModalOpen, setIsDeleteModalOpen] = React.useState(false); |
| 142 |
const [isExternalModalOpen, setIsExternalModalOpen] = React.useState(false); |
| 143 |
const [isPOSIXModalOpen, setIsPOSIXModalOpen] = React.useState(false); |
| 144 |
const onOpenDeleteModal = () => { |
| 145 |
setIsDeleteModalOpen(true); |
| 146 |
}; |
| 147 |
const onCloseDeleteModal = () => { |
| 148 |
setIsDeleteModalOpen(false); |
| 149 |
}; |
| 150 |
const onOpenExternalModal = () => { |
| 151 |
setIsExternalModalOpen(true); |
| 152 |
}; |
| 153 |
const onCloseExternalModal = () => { |
| 154 |
setIsExternalModalOpen(false); |
| 155 |
}; |
| 156 |
const onOpenPOSIXModal = () => { |
| 157 |
setIsPOSIXModalOpen(true); |
| 158 |
}; |
| 159 |
const onClosePOSIXModal = () => { |
| 160 |
setIsPOSIXModalOpen(false); |
| 161 |
}; |
| 162 |
|
| 163 |
// Change group to POSIX group |
| 164 |
const doChangeToPosix = () => { |
| 165 |
setModalSpinning(true); |
| 166 |
convertGroupPOSIX(cn).then((response) => { |
| 167 |
if ("data" in response) { |
| 168 |
if (response.data?.result) { |
| 169 |
dispatch( |
| 170 |
addAlert({ |
| 171 |
name: "posix-usergroup-success", |
| 172 |
title: "User group changed to POSIX group", |
| 173 |
variant: "success", |
| 174 |
}) |
| 175 |
); |
| 176 |
} else if (response.data?.error) { |
| 177 |
dispatch( |
| 178 |
addAlert({ |
| 179 |
name: "posix-error", |
| 180 |
title: "Failed to convert group: " + response.data.error, |
| 181 |
variant: "danger", |
| 182 |
}) |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
props.onRefresh(); |
| 187 |
setIsPOSIXModalOpen(false); |
| 188 |
setModalSpinning(false); |
| 189 |
}); |
| 190 |
}; |
| 191 |
|
| 192 |
// Change group to external |
| 193 |
const doChangeToExternal = () => { |
| 194 |
setModalSpinning(true); |
| 195 |
convertGroupExternal(cn).then((response) => { |
| 196 |
if ("data" in response) { |
| 197 |
if (response.data?.result) { |
| 198 |
dispatch( |
| 199 |
addAlert({ |
| 200 |
name: "external-usergroup-success", |
| 201 |
title: "User group changed to external group", |
| 202 |
variant: "success", |
| 203 |
}) |
| 204 |
); |
| 205 |
} else if (response.data?.error) { |
| 206 |
dispatch( |
| 207 |
addAlert({ |
| 208 |
name: "external-error", |
| 209 |
title: "Failed to convert group: " + response.data.error, |
| 210 |
variant: "danger", |
| 211 |
}) |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
props.onRefresh(); |
| 216 |
setIsExternalModalOpen(false); |
| 217 |
setModalSpinning(false); |
| 218 |
}); |
| 219 |
}; |
| 220 |
|
| 221 |
// Delete group |
| 222 |
const doDelete = () => { |
| 223 |
setModalSpinning(true); |
| 224 |
deleteGroup(cn).then((response) => { |
| 225 |
if ("data" in response) { |
| 226 |
if (response.data?.result) { |
| 227 |
dispatch( |
| 228 |
addAlert({ |
| 229 |
name: "remove-usergroup-success", |
| 230 |
title: "User group removed", |
| 231 |
variant: "success", |
| 232 |
}) |
| 233 |
); |
| 234 |
// Redirect to the main page |
| 235 |
navigate("/user-groups"); |
| 236 |
} else if (response.data?.error) { |
| 237 |
dispatch( |
| 238 |
addAlert({ |
| 239 |
name: "delete-error", |
| 240 |
title: "Failed to delete group: " + response.data.error, |
| 241 |
variant: "danger", |
| 242 |
}) |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
setIsDeleteModalOpen(false); |
| 247 |
setModalSpinning(false); |
| 248 |
}); |
| 249 |
}; |
| 250 |
|
| 251 |
const deleteModalActions = [ |
| 252 |
<Button |
| 253 |
data-cy="modal-button-delete" |
| 254 |
key="delete-host" |
| 255 |
variant="danger" |
| 256 |
onClick={doDelete} |
| 257 |
isDisabled={modalSpinning} |
| 258 |
isLoading={modalSpinning} |
| 259 |
spinnerAriaValueText="Deleting" |
| 260 |
spinnerAriaLabelledBy="Deleting" |
| 261 |
spinnerAriaLabel="Deleting" |
| 262 |
> |
| 263 |
{modalSpinning ? "Deleting" : "Delete"} |
| 264 |
</Button>, |
| 265 |
<Button |
| 266 |
data-cy="modal-button-cancel" |
| 267 |
key="cancel" |
| 268 |
variant="link" |
| 269 |
onClick={onCloseDeleteModal} |
| 270 |
> |
| 271 |
Cancel |
| 272 |
</Button>, |
| 273 |
]; |
| 274 |
|
| 275 |
const posixModalActions = [ |
| 276 |
<Button |
| 277 |
data-cy="modal-button-change" |
| 278 |
key="change-posix" |
| 279 |
variant="primary" |
| 280 |
onClick={doChangeToPosix} |
| 281 |
isDisabled={modalSpinning} |
| 282 |
isLoading={modalSpinning} |
| 283 |
spinnerAriaValueText="Changing" |
| 284 |
spinnerAriaLabelledBy="Changing" |
| 285 |
spinnerAriaLabel="Changing" |
| 286 |
> |
| 287 |
{modalSpinning ? "Changing" : "Change Group"} |
| 288 |
</Button>, |
| 289 |
<Button |
| 290 |
data-cy="modal-button-cancel" |
| 291 |
key="cancel" |
| 292 |
variant="link" |
| 293 |
onClick={onClosePOSIXModal} |
| 294 |
> |
| 295 |
Cancel |
| 296 |
</Button>, |
| 297 |
]; |
| 298 |
|
| 299 |
const externalModalActions = [ |
| 300 |
<Button |
| 301 |
data-cy="modal-button-change" |
| 302 |
key="change-external" |
| 303 |
variant="primary" |
| 304 |
onClick={doChangeToExternal} |
| 305 |
isDisabled={modalSpinning} |
| 306 |
isLoading={modalSpinning} |
| 307 |
spinnerAriaValueText="Changing" |
| 308 |
spinnerAriaLabelledBy="Changing" |
| 309 |
spinnerAriaLabel="Changing" |
| 310 |
> |
| 311 |
{modalSpinning ? "Changing" : "Change Group"} |
| 312 |
</Button>, |
| 313 |
<Button |
| 314 |
data-cy="modal-button-cancel" |
| 315 |
key="cancel" |
| 316 |
variant="link" |
| 317 |
onClick={onCloseExternalModal} |
| 318 |
> |
| 319 |
Cancel |
| 320 |
</Button>, |
| 321 |
]; |
| 322 |
|
| 323 |
// 'Save' handler method |
| 324 |
const onSave = () => { |
| 325 |
const modifiedValues = props.modifiedValues(); |
| 326 |
modifiedValues.cn = props.userGroup.cn; |
| 327 |
setSaving(true); |
| 328 |
|
| 329 |
saveGroup(modifiedValues).then((response) => { |
| 330 |
if ("data" in response) { |
| 331 |
if (response.data?.result) { |
| 332 |
// Show toast notification: success |
| 333 |
dispatch( |
| 334 |
addAlert({ |
| 335 |
name: "save-success", |
| 336 |
title: "User group modified", |
| 337 |
variant: "success", |
| 338 |
}) |
| 339 |
); |
| 340 |
} else if (response.data?.error) { |
| 341 |
// Show toast notification: error |
| 342 |
const errorMessage = response.data.error as ErrorResult; |
| 343 |
dispatch( |
| 344 |
addAlert({ |
| 345 |
name: "save-error", |
| 346 |
title: errorMessage.message, |
| 347 |
variant: "danger", |
| 348 |
}) |
| 349 |
); |
| 350 |
} |
| 351 |
// Reset values. Disable 'revert' and 'save' buttons |
| 352 |
props.onResetValues(); |
| 353 |
setSaving(false); |
| 354 |
} |
| 355 |
}); |
| 356 |
}; |
| 357 |
|
| 358 |
// 'Revert' handler method |
| 359 |
const onRevert = () => { |
| 360 |
props.onGroupChange(props.originalGroup); |
| 361 |
dispatch( |
| 362 |
addAlert({ |
| 363 |
name: "revert-success", |
| 364 |
title: "User group data reverted", |
| 365 |
variant: "success", |
| 366 |
}) |
| 367 |
); |
| 368 |
}; |
| 369 |
|
| 370 |
// Toolbar |
| 371 |
const toolbarFields = [ |
| 372 |
{ |
| 373 |
key: 0, |
| 374 |
element: ( |
| 375 |
<SecondaryButton |
| 376 |
onClickHandler={props.onRefresh} |
| 377 |
dataCy="user-groups-tab-settings-button-refresh" |
| 378 |
> |
| 379 |
Refresh |
| 380 |
</SecondaryButton> |
| 381 |
), |
| 382 |
}, |
| 383 |
{ |
| 384 |
key: 1, |
| 385 |
element: ( |
| 386 |
<SecondaryButton |
| 387 |
isDisabled={!props.isModified} |
| 388 |
onClickHandler={onRevert} |
| 389 |
dataCy="user-groups-tab-settings-button-revert" |
| 390 |
> |
| 391 |
Revert |
| 392 |
</SecondaryButton> |
| 393 |
), |
| 394 |
}, |
| 395 |
{ |
| 396 |
key: 2, |
| 397 |
element: ( |
| 398 |
<SecondaryButton |
| 399 |
isDisabled={!props.isModified || isSaving} |
| 400 |
onClickHandler={onSave} |
| 401 |
dataCy="user-groups-tab-settings-button-save" |
| 402 |
isLoading={isSaving} |
| 403 |
spinnerAriaValueText="Saving" |
| 404 |
spinnerAriaLabelledBy="Saving" |
| 405 |
spinnerAriaLabel="Saving" |
| 406 |
> |
| 407 |
{isSaving ? "Saving" : "Save"} |
| 408 |
</SecondaryButton> |
| 409 |
), |
| 410 |
}, |
| 411 |
{ |
| 412 |
key: 3, |
| 413 |
element: ( |
| 414 |
<KebabLayout |
| 415 |
dataCy="user-groups-tab-settings-kebab" |
| 416 |
direction={"up"} |
| 417 |
onDropdownSelect={onKebabSelect} |
| 418 |
onKebabToggle={onKebabToggle} |
| 419 |
idKebab="toggle-action-buttons" |
| 420 |
isKebabOpen={isKebabOpen} |
| 421 |
dropdownItems={dropdownItems} |
| 422 |
isDisabled={isSaving} |
| 423 |
/> |
| 424 |
), |
| 425 |
}, |
| 426 |
]; |
| 427 |
|
| 428 |
// Render component |
| 429 |
return ( |
| 430 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 431 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 432 |
<TitleLayout |
| 433 |
key={0} |
| 434 |
headingLevel="h2" |
| 435 |
id="group-settings" |
| 436 |
text="User group settings" |
| 437 |
/> |
| 438 |
<Form |
| 439 |
className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md" |
| 440 |
isHorizontal |
| 441 |
> |
| 442 |
<FormGroup label="Description" fieldId="description"> |
| 443 |
<IpaTextArea |
| 444 |
dataCy="user-groups-tab-settings-textbox-description" |
| 445 |
name="description" |
| 446 |
ipaObject={ipaObject} |
| 447 |
onChange={recordOnChange} |
| 448 |
objectName="group" |
| 449 |
metadata={props.metadata} |
| 450 |
/> |
| 451 |
</FormGroup> |
| 452 |
<FormGroup label="Group type" fieldId="group-type"> |
| 453 |
<TextInput |
| 454 |
data-cy="user-groups-tab-settings-textbox-group-type" |
| 455 |
id="group-type" |
| 456 |
name="grouptype" |
| 457 |
value={groupType} |
| 458 |
type="text" |
| 459 |
aria-label="group type" |
| 460 |
readOnlyVariant="plain" |
| 461 |
/> |
| 462 |
</FormGroup> |
| 463 |
<FormGroup label="GID" fieldId="gid"> |
| 464 |
<TextInput |
| 465 |
data-cy="user-groups-tab-settings-textbox-gid" |
| 466 |
id="gid" |
| 467 |
name="gid" |
| 468 |
value={props.userGroup.gidnumber} |
| 469 |
type="text" |
| 470 |
aria-label="GID" |
| 471 |
readOnlyVariant="plain" |
| 472 |
/> |
| 473 |
</FormGroup> |
| 474 |
<FormGroup label="Password policy" fieldId="pwdpolicy" role="group"> |
| 475 |
<Label |
| 476 |
label="Temporary until pwdPolicy page is finished" |
| 477 |
color={props.pwPolicyData.cn ? "blue" : "grey"} |
| 478 |
> |
| 479 |
{props.pwPolicyData.cn || "None"} |
| 480 |
</Label> |
| 481 |
</FormGroup> |
| 482 |
</Form> |
| 483 |
</Flex> |
| 484 |
<ConfirmationModal |
| 485 |
dataCy="delete-user-group-modal" |
| 486 |
title={"Delete user group"} |
| 487 |
isOpen={isDeleteModalOpen} |
| 488 |
onClose={onCloseDeleteModal} |
| 489 |
actions={deleteModalActions} |
| 490 |
messageText={"Are you sure you want to delete this user group?"} |
| 491 |
messageObj={cn} |
| 492 |
/> |
| 493 |
<ConfirmationModal |
| 494 |
dataCy="change-group-type-posix-modal" |
| 495 |
title={"Change group type"} |
| 496 |
isOpen={isPOSIXModalOpen} |
| 497 |
onClose={onClosePOSIXModal} |
| 498 |
actions={posixModalActions} |
| 499 |
messageText={ |
| 500 |
"Are you sure you want to change this group to a POSIX group?" |
| 501 |
} |
| 502 |
messageObj={cn} |
| 503 |
/> |
| 504 |
<ConfirmationModal |
| 505 |
dataCy="change-group-type-external-modal" |
| 506 |
title={"Change group type"} |
| 507 |
isOpen={isExternalModalOpen} |
| 508 |
onClose={onCloseExternalModal} |
| 509 |
actions={externalModalActions} |
| 510 |
messageText={ |
| 511 |
"Are you sure you want to change this group to an external group?" |
| 512 |
} |
| 513 |
messageObj={cn} |
| 514 |
/> |
| 515 |
</TabLayout> |
| 516 |
); |
| 517 |
}; |
| 518 |
|
| 519 |
export default UserGroupsSettings; |