| 1 |
import React, { useEffect, useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Flex, |
| 5 |
FlexItem, |
| 6 |
JumpLinks, |
| 7 |
JumpLinksItem, |
| 8 |
PageSection, |
| 9 |
Sidebar, |
| 10 |
SidebarPanel, |
| 11 |
SidebarContent, |
| 12 |
} from "@patternfly/react-core"; |
| 13 |
// Redux |
| 14 |
import { useAppDispatch, useAppSelector } from "src/store/hooks"; |
| 15 |
// Layouts |
| 16 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 17 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 18 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 19 |
// Components |
| 20 |
import ToolbarLayout from "src/components/layouts/ToolbarLayout"; |
| 21 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 22 |
// Hooks |
| 23 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 24 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 25 |
import { useConfigSettings } from "src/hooks/useConfigSettingsData"; |
| 26 |
// Utils |
| 27 |
import { API_VERSION_BACKUP } from "src/utils/utils"; |
| 28 |
import { asRecord } from "../../utils/hostUtils"; |
| 29 |
// Errors |
| 30 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 31 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 32 |
// Option components |
| 33 |
import ConfigSearchOptions from "./ConfigSearchOptions"; |
| 34 |
import ConfigServerOptions from "./ConfigServerOptions"; |
| 35 |
import ConfigUserOptions from "./ConfigUserOptions"; |
| 36 |
import ConfigGroupOptions from "./ConfigGroupOptions"; |
| 37 |
import ConfigSELinuxOptions from "./ConfigSELinuxOptions"; |
| 38 |
import ConfigServiceOptions from "./ConfigServiceOptions"; |
| 39 |
// RPC |
| 40 |
import { |
| 41 |
ErrorResult, |
| 42 |
GenericPayload, |
| 43 |
useSearchEntriesMutation, |
| 44 |
} from "src/services/rpc"; |
| 45 |
import { useSaveConfigMutation } from "src/services/rpcConfig"; |
| 46 |
import { useGettingGroupsQuery } from "src/services/rpcUserGroups"; |
| 47 |
|
| 48 |
const Configuration = () => { |
| 49 |
const dispatch = useAppDispatch(); |
| 50 |
|
| 51 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 52 |
const { browserTitle } = useUpdateRoute({ pathname: "configuration" }); |
| 53 |
|
| 54 |
// Set the page title to be shown in the browser tab |
| 55 |
React.useEffect(() => { |
| 56 |
document.title = browserTitle; |
| 57 |
}, [browserTitle]); |
| 58 |
|
| 59 |
// Retrieve API version from environment data |
| 60 |
const apiVersion = useAppSelector( |
| 61 |
(state) => state.global.environment.api_version |
| 62 |
) as string; |
| 63 |
|
| 64 |
// Data loaded from DB |
| 65 |
const configData = useConfigSettings(); |
| 66 |
|
| 67 |
const [groups, setGroups] = React.useState<string[]>([]); |
| 68 |
|
| 69 |
// Get User groups |
| 70 |
const groupDataResponse = useGettingGroupsQuery({ |
| 71 |
searchValue: "", |
| 72 |
sizeLimit: 1000, |
| 73 |
apiVersion: apiVersion || API_VERSION_BACKUP, |
| 74 |
startIdx: 0, |
| 75 |
stopIdx: 1000, |
| 76 |
} as GenericPayload); |
| 77 |
|
| 78 |
const { data: batchResponse } = groupDataResponse; |
| 79 |
|
| 80 |
// Handle data when the API call is finished |
| 81 |
useEffect(() => { |
| 82 |
if (groupDataResponse.isFetching) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
// API response: Success |
| 87 |
if ( |
| 88 |
groupDataResponse.isSuccess && |
| 89 |
groupDataResponse.data && |
| 90 |
batchResponse !== undefined |
| 91 |
) { |
| 92 |
const groupsListResult = batchResponse.result.results; |
| 93 |
const groupsListSize = batchResponse.result.count; |
| 94 |
const groupsList: string[] = []; |
| 95 |
|
| 96 |
for (let i = 0; i < groupsListSize; i++) { |
| 97 |
groupsList.push(groupsListResult[i].result.cn[0]); |
| 98 |
} |
| 99 |
setGroups(groupsList); |
| 100 |
} |
| 101 |
}, [groupDataResponse]); |
| 102 |
|
| 103 |
// Issue a search using a specific search value |
| 104 |
const [retrieveGroups] = useSearchEntriesMutation({}); |
| 105 |
|
| 106 |
const submitSearchValue = (value: string) => { |
| 107 |
retrieveGroups({ |
| 108 |
searchValue: value, |
| 109 |
sizeLimit: 1000, |
| 110 |
apiVersion: apiVersion || API_VERSION_BACKUP, |
| 111 |
startIdx: 0, |
| 112 |
stopIdx: 1000, |
| 113 |
entryType: "usergroup", |
| 114 |
} as GenericPayload).then((result) => { |
| 115 |
if ("data" in result) { |
| 116 |
const searchError = result.data?.error as |
| 117 |
| FetchBaseQueryError |
| 118 |
| SerializedError; |
| 119 |
|
| 120 |
if (searchError) { |
| 121 |
// Error |
| 122 |
let error: string | undefined = ""; |
| 123 |
if ("error" in searchError) { |
| 124 |
error = searchError.error; |
| 125 |
} else if ("message" in searchError) { |
| 126 |
error = searchError.message; |
| 127 |
} |
| 128 |
dispatch( |
| 129 |
addAlert({ |
| 130 |
name: "submit-search-value-error", |
| 131 |
title: error || "Error when searching for user groups", |
| 132 |
variant: "danger", |
| 133 |
}) |
| 134 |
); |
| 135 |
} else { |
| 136 |
// Success |
| 137 |
const groupsListResult = result.data?.result.results || []; |
| 138 |
const groupsListSize = result.data?.result.count || 0; |
| 139 |
const groupsList: string[] = []; |
| 140 |
for (let i = 0; i < groupsListSize; i++) { |
| 141 |
groupsList.push(groupsListResult[i].result.cn[0]); |
| 142 |
} |
| 143 |
setGroups(groupsList); |
| 144 |
} |
| 145 |
} |
| 146 |
}); |
| 147 |
}; |
| 148 |
|
| 149 |
const [isSaving, setSaving] = useState(false); |
| 150 |
|
| 151 |
// 'Save' handler method |
| 152 |
const [saveConfig] = useSaveConfigMutation(); |
| 153 |
const onSave = () => { |
| 154 |
const modifiedValues = configData.modifiedValues(); |
| 155 |
setSaving(true); |
| 156 |
saveConfig(modifiedValues).then((response) => { |
| 157 |
if ("data" in response) { |
| 158 |
if (response.data?.result) { |
| 159 |
// Show toast notification: success |
| 160 |
dispatch( |
| 161 |
addAlert({ |
| 162 |
name: "save-success", |
| 163 |
title: "Configuration updated", |
| 164 |
variant: "success", |
| 165 |
}) |
| 166 |
); |
| 167 |
} else if (response.data?.error) { |
| 168 |
// Show toast notification: error |
| 169 |
const errorMessage = response.data.error as ErrorResult; |
| 170 |
dispatch( |
| 171 |
addAlert({ |
| 172 |
name: "save-error", |
| 173 |
title: errorMessage.message, |
| 174 |
variant: "danger", |
| 175 |
}) |
| 176 |
); |
| 177 |
} |
| 178 |
onRefresh(); |
| 179 |
} |
| 180 |
setSaving(false); |
| 181 |
}); |
| 182 |
}; |
| 183 |
|
| 184 |
// 'Revert' handler method |
| 185 |
const onRevert = () => { |
| 186 |
configData.setConfig(configData.originalConfig); |
| 187 |
dispatch( |
| 188 |
addAlert({ |
| 189 |
name: "revert-success", |
| 190 |
title: "Configuration data reverted", |
| 191 |
variant: "success", |
| 192 |
}) |
| 193 |
); |
| 194 |
}; |
| 195 |
|
| 196 |
const onRefresh = () => { |
| 197 |
configData.refetch(); |
| 198 |
}; |
| 199 |
|
| 200 |
if (!configData || configData.isLoading) { |
| 201 |
return <DataSpinner />; |
| 202 |
} |
| 203 |
|
| 204 |
// Toolbar |
| 205 |
const toolbarFields = [ |
| 206 |
{ |
| 207 |
key: 0, |
| 208 |
element: ( |
| 209 |
<SecondaryButton |
| 210 |
dataCy="configuration-button-refresh" |
| 211 |
onClickHandler={onRefresh} |
| 212 |
> |
| 213 |
Refresh |
| 214 |
</SecondaryButton> |
| 215 |
), |
| 216 |
}, |
| 217 |
{ |
| 218 |
key: 1, |
| 219 |
element: ( |
| 220 |
<SecondaryButton |
| 221 |
dataCy="configuration-button-revert" |
| 222 |
isDisabled={!configData.modified} |
| 223 |
onClickHandler={onRevert} |
| 224 |
> |
| 225 |
Revert |
| 226 |
</SecondaryButton> |
| 227 |
), |
| 228 |
}, |
| 229 |
{ |
| 230 |
key: 2, |
| 231 |
element: ( |
| 232 |
<SecondaryButton |
| 233 |
dataCy="configuration-button-save" |
| 234 |
isDisabled={!configData.modified || isSaving} |
| 235 |
onClickHandler={onSave} |
| 236 |
isLoading={isSaving} |
| 237 |
spinnerAriaValueText="Saving" |
| 238 |
spinnerAriaLabelledBy="Saving" |
| 239 |
spinnerAriaLabel="Saving" |
| 240 |
> |
| 241 |
{isSaving ? "Saving" : "Save"} |
| 242 |
</SecondaryButton> |
| 243 |
), |
| 244 |
}, |
| 245 |
]; |
| 246 |
|
| 247 |
const style: React.CSSProperties = { |
| 248 |
overflowY: "auto", |
| 249 |
height: "64.6vh", |
| 250 |
}; |
| 251 |
|
| 252 |
// Get 'ipaObject' and 'recordOnChange' |
| 253 |
const { ipaObject, recordOnChange } = asRecord( |
| 254 |
configData.config, |
| 255 |
configData.setConfig |
| 256 |
); |
| 257 |
|
| 258 |
return ( |
| 259 |
<> |
| 260 |
<PageSection hasBodyWrapper={false} isFilled> |
| 261 |
<TitleLayout id="config title" headingLevel="h1" text="Configuration" /> |
| 262 |
</PageSection> |
| 263 |
<PageSection |
| 264 |
hasBodyWrapper={false} |
| 265 |
id="settings-page" |
| 266 |
style={style} |
| 267 |
isFilled |
| 268 |
> |
| 269 |
<Sidebar isPanelRight> |
| 270 |
<SidebarPanel variant="sticky"> |
| 271 |
<HelpTextWithIconLayout textContent="Help" /> |
| 272 |
<JumpLinks |
| 273 |
isVertical |
| 274 |
label="Jump to section" |
| 275 |
scrollableSelector="#settings-page" |
| 276 |
offset={220} // for masthead |
| 277 |
expandable={{ default: "expandable", md: "nonExpandable" }} |
| 278 |
> |
| 279 |
<JumpLinksItem key={0} href="#search-options"> |
| 280 |
Search options |
| 281 |
</JumpLinksItem> |
| 282 |
<JumpLinksItem key={1} href="#server-options"> |
| 283 |
Server options |
| 284 |
</JumpLinksItem> |
| 285 |
<JumpLinksItem key={2} href="#user-options"> |
| 286 |
User options |
| 287 |
</JumpLinksItem> |
| 288 |
<JumpLinksItem key={3} href="#group-options"> |
| 289 |
Group options |
| 290 |
</JumpLinksItem> |
| 291 |
<JumpLinksItem key={4} href="#selinux-options"> |
| 292 |
Selinux options |
| 293 |
</JumpLinksItem> |
| 294 |
<JumpLinksItem key={5} href="#service-options"> |
| 295 |
Service options |
| 296 |
</JumpLinksItem> |
| 297 |
</JumpLinks> |
| 298 |
</SidebarPanel> |
| 299 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 300 |
<Flex |
| 301 |
direction={{ default: "column", lg: "row" }} |
| 302 |
flex={{ default: "flex_1" }} |
| 303 |
> |
| 304 |
<FlexItem flex={{ default: "flex_1" }}> |
| 305 |
<TitleLayout |
| 306 |
key={0} |
| 307 |
headingLevel="h2" |
| 308 |
id="search-options" |
| 309 |
text="Search options" |
| 310 |
/> |
| 311 |
<ConfigSearchOptions |
| 312 |
ipaObject={ipaObject} |
| 313 |
recordOnChange={recordOnChange} |
| 314 |
metadata={configData.metadata} |
| 315 |
/> |
| 316 |
</FlexItem> |
| 317 |
<FlexItem flex={{ default: "flex_1" }}> |
| 318 |
<TitleLayout |
| 319 |
key={1} |
| 320 |
headingLevel="h2" |
| 321 |
id="server-options" |
| 322 |
text="Server options" |
| 323 |
/> |
| 324 |
<ConfigServerOptions |
| 325 |
config={configData.config} |
| 326 |
ipaObject={ipaObject} |
| 327 |
recordOnChange={recordOnChange} |
| 328 |
metadata={configData.metadata} |
| 329 |
/> |
| 330 |
</FlexItem> |
| 331 |
</Flex> |
| 332 |
<Flex |
| 333 |
direction={{ default: "column", lg: "row" }} |
| 334 |
flex={{ default: "flex_1" }} |
| 335 |
className="pf-v6-u-mt-lg" |
| 336 |
> |
| 337 |
<FlexItem flex={{ default: "flex_1" }}> |
| 338 |
<TitleLayout |
| 339 |
key={2} |
| 340 |
headingLevel="h2" |
| 341 |
id="user-options" |
| 342 |
text="User options" |
| 343 |
/> |
| 344 |
<ConfigUserOptions |
| 345 |
ipaObject={ipaObject} |
| 346 |
recordOnChange={recordOnChange} |
| 347 |
metadata={configData.metadata} |
| 348 |
groups={groups} |
| 349 |
onSearch={submitSearchValue} |
| 350 |
/> |
| 351 |
</FlexItem> |
| 352 |
</Flex> |
| 353 |
<Flex |
| 354 |
direction={{ default: "column", lg: "row" }} |
| 355 |
flex={{ default: "flex_1" }} |
| 356 |
className="pf-v6-u-mt-lg" |
| 357 |
> |
| 358 |
<FlexItem flex={{ default: "flex_1" }}> |
| 359 |
<TitleLayout |
| 360 |
key={3} |
| 361 |
headingLevel="h2" |
| 362 |
id="group-options" |
| 363 |
text="Group options" |
| 364 |
/> |
| 365 |
<ConfigGroupOptions |
| 366 |
ipaObject={ipaObject} |
| 367 |
recordOnChange={recordOnChange} |
| 368 |
metadata={configData.metadata} |
| 369 |
/> |
| 370 |
</FlexItem> |
| 371 |
<FlexItem flex={{ default: "flex_1" }}> |
| 372 |
<Flex |
| 373 |
direction={{ default: "column" }} |
| 374 |
flex={{ default: "flex_1" }} |
| 375 |
> |
| 376 |
<FlexItem flex={{ default: "flex_1" }}> |
| 377 |
<TitleLayout |
| 378 |
key={4} |
| 379 |
headingLevel="h2" |
| 380 |
id="selinux-options" |
| 381 |
text="SELinux options" |
| 382 |
/> |
| 383 |
<ConfigSELinuxOptions |
| 384 |
ipaObject={ipaObject} |
| 385 |
recordOnChange={recordOnChange} |
| 386 |
metadata={configData.metadata} |
| 387 |
/> |
| 388 |
</FlexItem> |
| 389 |
<FlexItem flex={{ default: "flex_1" }}> |
| 390 |
<TitleLayout |
| 391 |
key={5} |
| 392 |
headingLevel="h2" |
| 393 |
id="service-options" |
| 394 |
text="Service options" |
| 395 |
className="pf-v6-u-mt-lg" |
| 396 |
/> |
| 397 |
<ConfigServiceOptions |
| 398 |
ipaObject={ipaObject} |
| 399 |
recordOnChange={recordOnChange} |
| 400 |
metadata={configData.metadata} |
| 401 |
/> |
| 402 |
</FlexItem> |
| 403 |
</Flex> |
| 404 |
</FlexItem> |
| 405 |
</Flex> |
| 406 |
</SidebarContent> |
| 407 |
</Sidebar> |
| 408 |
</PageSection> |
| 409 |
<ToolbarLayout |
| 410 |
isSticky |
| 411 |
toolbarItems={toolbarFields} |
| 412 |
className="pf-v6-u-ml-lg" |
| 413 |
/> |
| 414 |
</> |
| 415 |
); |
| 416 |
}; |
| 417 |
|
| 418 |
export default Configuration; |