| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
Form, |
| 6 |
FormGroup, |
| 7 |
JumpLinks, |
| 8 |
JumpLinksItem, |
| 9 |
Sidebar, |
| 10 |
SidebarPanel, |
| 11 |
SidebarContent, |
| 12 |
} from "@patternfly/react-core"; |
| 13 |
// Forms |
| 14 |
import IpaTextArea from "src/components/Form/IpaTextArea"; |
| 15 |
// Layouts |
| 16 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 17 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 18 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 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 { Automember, Metadata } from "../../utils/datatypes/globalDataTypes"; |
| 29 |
// Icons |
| 30 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 31 |
// RPC |
| 32 |
import { ErrorResult } from "src/services/rpc"; |
| 33 |
import { |
| 34 |
AutomemberModPayload, |
| 35 |
Condition, |
| 36 |
useSaveAutomemberMutation, |
| 37 |
} from "src/services/rpcAutomember"; |
| 38 |
import InclusiveExclusiveSection from "src/components/AutomemberSections/InclusiveExclusiveSection"; |
| 39 |
|
| 40 |
interface PropsToSettings { |
| 41 |
automemberRule: Partial<Automember>; |
| 42 |
originalAutomemberRule: Partial<Automember>; |
| 43 |
automemberType: string; |
| 44 |
metadata: Metadata; |
| 45 |
onAutomemberChange: (automember: Partial<Automember>) => void; |
| 46 |
onRefresh: () => void; |
| 47 |
isModified: boolean; |
| 48 |
isDataLoading?: boolean; |
| 49 |
modifiedValues: () => Partial<Automember>; |
| 50 |
onResetValues: () => void; |
| 51 |
} |
| 52 |
|
| 53 |
const AutoMemSettings = (props: PropsToSettings) => { |
| 54 |
const dispatch = useAppDispatch(); |
| 55 |
|
| 56 |
// RPC calls |
| 57 |
const [saveAutomember] = useSaveAutomemberMutation(); |
| 58 |
|
| 59 |
// Infer pathname from automemberType |
| 60 |
const pathname = |
| 61 |
props.automemberType === "group" ? "user-group-rules" : "host-group-rules"; |
| 62 |
|
| 63 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 64 |
useUpdateRoute({ pathname: pathname, noBreadcrumb: true }); |
| 65 |
|
| 66 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 67 |
const { ipaObject, recordOnChange } = asRecord( |
| 68 |
props.automemberRule, |
| 69 |
props.onAutomemberChange |
| 70 |
); |
| 71 |
|
| 72 |
// States |
| 73 |
const [inclusiveRules, setInclusiveRules] = React.useState<Condition[]>([]); |
| 74 |
const [exclusiveRules, setExclusiveRules] = React.useState<Condition[]>([]); |
| 75 |
const [isSaving, setSaving] = React.useState(false); |
| 76 |
|
| 77 |
// Keep the 'rule' state updated |
| 78 |
React.useEffect(() => { |
| 79 |
// Convert the automember inclusive/exclusive rules ('employeetype=SE') to Condition[] format |
| 80 |
if (props.automemberRule.automemberinclusiveregex !== undefined) { |
| 81 |
const inclusive = props.automemberRule.automemberinclusiveregex.map( |
| 82 |
(rule) => { |
| 83 |
const splittedRule = rule.split("="); |
| 84 |
const key = splittedRule[0]; |
| 85 |
const value = splittedRule[1]; |
| 86 |
|
| 87 |
return { |
| 88 |
key: key, |
| 89 |
automemberregex: value, |
| 90 |
} as Condition; |
| 91 |
} |
| 92 |
); |
| 93 |
|
| 94 |
setInclusiveRules(inclusive); |
| 95 |
} |
| 96 |
|
| 97 |
if (props.automemberRule.automemberexclusiveregex !== undefined) { |
| 98 |
const exclusive = props.automemberRule.automemberexclusiveregex.map( |
| 99 |
(rule) => { |
| 100 |
const splittedRule = rule.split("="); |
| 101 |
const key = splittedRule[0]; |
| 102 |
const value = splittedRule[1]; |
| 103 |
|
| 104 |
return { |
| 105 |
key: key, |
| 106 |
automemberregex: value, |
| 107 |
} as Condition; |
| 108 |
} |
| 109 |
); |
| 110 |
setExclusiveRules(exclusive); |
| 111 |
} |
| 112 |
}, [props.automemberRule]); |
| 113 |
|
| 114 |
// 'Save' handler method |
| 115 |
const onSave = () => { |
| 116 |
const modifiedValues = props.modifiedValues(); |
| 117 |
const payload = { |
| 118 |
automemberId: props.automemberRule.cn?.toString(), |
| 119 |
description: modifiedValues.description, |
| 120 |
type: props.automemberType, |
| 121 |
} as AutomemberModPayload; |
| 122 |
|
| 123 |
setSaving(true); |
| 124 |
|
| 125 |
saveAutomember(payload).then((response) => { |
| 126 |
if ("data" in response) { |
| 127 |
if (response.data?.result) { |
| 128 |
// Show toast notification: success |
| 129 |
dispatch( |
| 130 |
addAlert({ |
| 131 |
name: "save-success", |
| 132 |
title: "Entry updated", |
| 133 |
variant: "success", |
| 134 |
}) |
| 135 |
); |
| 136 |
// Refresh the page |
| 137 |
props.onRefresh(); |
| 138 |
} else if (response.data?.error) { |
| 139 |
// Show toast notification: error |
| 140 |
const errorMessage = response.data.error as ErrorResult; |
| 141 |
dispatch( |
| 142 |
addAlert({ |
| 143 |
name: "save-error", |
| 144 |
title: errorMessage.message, |
| 145 |
variant: "danger", |
| 146 |
}) |
| 147 |
); |
| 148 |
// Reset values. Disable 'revert' and 'save' buttons |
| 149 |
props.onResetValues(); |
| 150 |
} |
| 151 |
setSaving(false); |
| 152 |
} |
| 153 |
}); |
| 154 |
}; |
| 155 |
|
| 156 |
// 'Revert' handler method |
| 157 |
const onRevert = () => { |
| 158 |
props.onAutomemberChange(props.originalAutomemberRule); |
| 159 |
props.onRefresh(); |
| 160 |
dispatch( |
| 161 |
addAlert({ |
| 162 |
name: "revert-success", |
| 163 |
title: "Automember rule data reverted", |
| 164 |
variant: "success", |
| 165 |
}) |
| 166 |
); |
| 167 |
}; |
| 168 |
|
| 169 |
// Toolbar |
| 170 |
const toolbarFields = [ |
| 171 |
{ |
| 172 |
key: 0, |
| 173 |
element: ( |
| 174 |
<SecondaryButton |
| 175 |
dataCy="auto-member-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="auto-member-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="auto-member-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="automember-settings-page" toolbarItems={toolbarFields}> |
| 215 |
<Sidebar isPanelRight> |
| 216 |
<SidebarPanel variant="sticky"> |
| 217 |
<HelpTextWithIconLayout |
| 218 |
textContent="Help" |
| 219 |
icon={ |
| 220 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 221 |
} |
| 222 |
/> |
| 223 |
<JumpLinks |
| 224 |
isVertical |
| 225 |
label="Jump to section" |
| 226 |
scrollableSelector="#settings-page" |
| 227 |
offset={220} // for masthead |
| 228 |
expandable={{ default: "expandable", md: "nonExpandable" }} |
| 229 |
> |
| 230 |
<JumpLinksItem key={0} href="#rule-general"> |
| 231 |
General |
| 232 |
</JumpLinksItem> |
| 233 |
<JumpLinksItem key={1} href="#inclusive"> |
| 234 |
Inclusive |
| 235 |
</JumpLinksItem> |
| 236 |
<JumpLinksItem key={2} href="#exclusive"> |
| 237 |
Exclusive |
| 238 |
</JumpLinksItem> |
| 239 |
</JumpLinks> |
| 240 |
</SidebarPanel> |
| 241 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 242 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 243 |
<TitleLayout |
| 244 |
key={0} |
| 245 |
headingLevel="h1" |
| 246 |
id="rule-general" |
| 247 |
text="General" |
| 248 |
/> |
| 249 |
<Form className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md"> |
| 250 |
<FormGroup label="Description" fieldId="description" role="group"> |
| 251 |
<IpaTextArea |
| 252 |
dataCy="auto-member-tab-settings-textbox-description" |
| 253 |
name="description" |
| 254 |
ipaObject={ipaObject} |
| 255 |
onChange={recordOnChange} |
| 256 |
objectName="automember" |
| 257 |
metadata={props.metadata} |
| 258 |
/> |
| 259 |
</FormGroup> |
| 260 |
</Form> |
| 261 |
<Form className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md"> |
| 262 |
<TitleLayout |
| 263 |
key={1} |
| 264 |
headingLevel="h1" |
| 265 |
id="rule-inclusive" |
| 266 |
text="Inclusive" |
| 267 |
/> |
| 268 |
<InclusiveExclusiveSection |
| 269 |
entityId={props.automemberRule.cn?.toString() as string} |
| 270 |
automemberType={props.automemberType} |
| 271 |
conditionType={"inclusive"} |
| 272 |
tableElements={inclusiveRules} |
| 273 |
metadata={props.metadata} |
| 274 |
columnNames={["Attribute", "Expression"]} |
| 275 |
onRefresh={props.onRefresh} |
| 276 |
/> |
| 277 |
</Form> |
| 278 |
<Form className="pf-v6-u-mt-sm pf-v6-u-mb-lg pf-v6-u-mr-md"> |
| 279 |
<TitleLayout |
| 280 |
key={1} |
| 281 |
headingLevel="h1" |
| 282 |
id="rule-exclusive" |
| 283 |
text="Exclusive" |
| 284 |
/> |
| 285 |
<InclusiveExclusiveSection |
| 286 |
entityId={props.automemberRule.cn?.toString() as string} |
| 287 |
automemberType={props.automemberType} |
| 288 |
conditionType={"exclusive"} |
| 289 |
tableElements={exclusiveRules} |
| 290 |
metadata={props.metadata} |
| 291 |
columnNames={["Attribute", "Expression"]} |
| 292 |
onRefresh={props.onRefresh} |
| 293 |
/> |
| 294 |
</Form> |
| 295 |
</Flex> |
| 296 |
</SidebarContent> |
| 297 |
</Sidebar> |
| 298 |
</TabLayout> |
| 299 |
); |
| 300 |
}; |
| 301 |
|
| 302 |
export default AutoMemSettings; |