|
| 1 | +import { useTranslation } from 'react-i18next'; |
| 2 | +import { useApiAlert } from '@utils/useApiAlert'; |
| 3 | +import { useParams } from 'react-router-dom'; |
| 4 | +import { useFetchEditableConfiguration } from '@app/services/configHook'; |
| 5 | +import * as React from 'react'; |
| 6 | +import { useEffect, useState } from 'react'; |
| 7 | +import { |
| 8 | + Alert, |
| 9 | + Content, |
| 10 | + Form, |
| 11 | + FormGroup, |
| 12 | + FormHelperText, |
| 13 | + FormSection, |
| 14 | + Grid, |
| 15 | + GridItem, |
| 16 | + HelperText, |
| 17 | + HelperTextItem |
| 18 | +} from '@patternfly/react-core'; |
| 19 | +import { ExclamationCircleIcon } from '@patternfly/react-icons'; |
| 20 | +import { SelectMultiWithChips } from '@app/Common/SelectMultiWithChips'; |
| 21 | +import { selectOptionPropsFromArray } from '@utils/selectOptionPropsCreator'; |
| 22 | +import { TabsToolbar } from '@app/Caches/Configuration/Features/TabsToolbar'; |
| 23 | +import { TableLoadingState } from '@app/Common/TableLoadingState'; |
| 24 | +import { ConsoleServices } from '@services/ConsoleServices'; |
| 25 | +import { CONF_MUTABLE_SECURITY_AUTHORIZATION_ROLES } from '@services/cacheConfigUtils'; |
| 26 | +import { TableErrorState } from '@app/Common/TableErrorState'; |
| 27 | +import { useFetchAvailableRolesNames } from '@app/services/rolesHook'; |
| 28 | + |
| 29 | +const SecurityConfigEdition = () => { |
| 30 | + const { t } = useTranslation(); |
| 31 | + const brandname = t('brandname.brandname'); |
| 32 | + const { addAlert } = useApiAlert(); |
| 33 | + const cacheName = useParams()['cacheName'] as string; |
| 34 | + const { loadingConfig, errorConfig, editableConfig } = useFetchEditableConfiguration(cacheName); |
| 35 | + const { availableRoleNames, loading: loadingRoles, error: rolesError } = useFetchAvailableRolesNames(); |
| 36 | + const [roles, setRoles] = useState<string[]>([]); |
| 37 | + const [validEntity, setvalidEntity] = useState<'success' | 'error' | 'default'>('default'); |
| 38 | + const [updateConfigError, setUpdateConfigError] = useState<string>(''); |
| 39 | + |
| 40 | + const onSelectRoles = (selection) => { |
| 41 | + if (roles.includes(selection)) setRoles(roles.filter((role) => role !== selection)); |
| 42 | + else setRoles([...roles, selection]); |
| 43 | + }; |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (!loadingConfig && errorConfig.length == 0 && editableConfig) { |
| 47 | + setActualValues(); |
| 48 | + } |
| 49 | + }, [loadingConfig, errorConfig, editableConfig]); |
| 50 | + |
| 51 | + const updateSecurity = () => { |
| 52 | + if (roles.length == 0) { |
| 53 | + setvalidEntity('error'); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + setvalidEntity('success'); |
| 58 | + |
| 59 | + ConsoleServices.caches() |
| 60 | + .setConfigAttribute(cacheName, CONF_MUTABLE_SECURITY_AUTHORIZATION_ROLES, roles.join(' ')) |
| 61 | + .then((actionResponse) => { |
| 62 | + if (actionResponse.success) { |
| 63 | + addAlert(actionResponse); |
| 64 | + } else { |
| 65 | + setUpdateConfigError(actionResponse.message); |
| 66 | + } |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + const setActualValues = () => { |
| 71 | + if (!editableConfig) { |
| 72 | + return; |
| 73 | + } |
| 74 | + setRoles(editableConfig.securityAuthorizationRoles); |
| 75 | + }; |
| 76 | + |
| 77 | + const displayError = () => { |
| 78 | + if (updateConfigError.length == 0) { |
| 79 | + return <></>; |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <GridItem span={12}> |
| 84 | + <Alert |
| 85 | + variant="danger" |
| 86 | + isInline |
| 87 | + title={t('caches.edit-configuration.security-config-error', { error: updateConfigError })} |
| 88 | + /> |
| 89 | + </GridItem> |
| 90 | + ); |
| 91 | + }; |
| 92 | + |
| 93 | + if (loadingRoles || loadingConfig) { |
| 94 | + return <TableLoadingState message={t('common.loading')} />; |
| 95 | + } |
| 96 | + |
| 97 | + if (rolesError.length != 0) { |
| 98 | + return <TableErrorState error={rolesError} />; |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <Form |
| 103 | + isWidthLimited |
| 104 | + onSubmit={(e) => { |
| 105 | + e.preventDefault(); |
| 106 | + }} |
| 107 | + > |
| 108 | + <FormSection title={t('caches.edit-configuration.security-title')}> |
| 109 | + <Grid hasGutter md={6}> |
| 110 | + <GridItem span={12}> |
| 111 | + <Content> |
| 112 | + {t('caches.edit-configuration.security-description', { |
| 113 | + brandname: brandname |
| 114 | + })} |
| 115 | + </Content> |
| 116 | + </GridItem> |
| 117 | + </Grid> |
| 118 | + |
| 119 | + {displayError()} |
| 120 | + <FormGroup |
| 121 | + isRequired |
| 122 | + label={t('caches.edit-configuration.security-authz-roles')} |
| 123 | + fieldId="security-authz-roles" |
| 124 | + > |
| 125 | + <SelectMultiWithChips |
| 126 | + id="rolesSelector" |
| 127 | + placeholder={'Select an role'} |
| 128 | + options={selectOptionPropsFromArray(availableRoleNames)} |
| 129 | + onSelect={onSelectRoles} |
| 130 | + onClear={() => setRoles([])} |
| 131 | + selection={roles} |
| 132 | + /> |
| 133 | + |
| 134 | + {validEntity === 'error' && ( |
| 135 | + <FormHelperText> |
| 136 | + <HelperText> |
| 137 | + <HelperTextItem variant={validEntity} icon={<ExclamationCircleIcon />}> |
| 138 | + {t('caches.edit-configuration.security-authz-roles-helper-invalid')} |
| 139 | + </HelperTextItem> |
| 140 | + </HelperText> |
| 141 | + </FormHelperText> |
| 142 | + )} |
| 143 | + </FormGroup> |
| 144 | + {<TabsToolbar id="security" saveAction={updateSecurity} />} |
| 145 | + </FormSection> |
| 146 | + </Form> |
| 147 | + ); |
| 148 | +}; |
| 149 | +export { SecurityConfigEdition }; |
0 commit comments