Fixes bug where inventory field was erroneously disabled on WFJT form

We were disabling the field when a user did not have sufficient permissions to create an Inventory.  I updated this logic to check if a user has use permissions on the selected inventory before disabling the field.
This commit is contained in:
mabashian
2022-09-13 10:33:08 -04:00
parent 84fa19f2ad
commit cc6eaa7f44
3 changed files with 47 additions and 19 deletions

View File

@@ -39,13 +39,7 @@ function InventoryLookup({
const autoPopulateLookup = useAutoPopulateLookup(onChange); const autoPopulateLookup = useAutoPopulateLookup(onChange);
const { const {
result: { result: { inventories, count, relatedSearchableKeys, searchableKeys },
inventories,
count,
relatedSearchableKeys,
searchableKeys,
canEdit,
},
request: fetchInventories, request: fetchInventories,
error, error,
isLoading, isLoading,
@@ -85,8 +79,6 @@ function InventoryLookup({
key, key,
type: actionsResponse.data.actions?.GET[key].type, type: actionsResponse.data.actions?.GET[key].type,
})), })),
canEdit:
Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled,
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [autoPopulate, autoPopulateLookup, history.location]), }, [autoPopulate, autoPopulateLookup, history.location]),
@@ -95,7 +87,6 @@ function InventoryLookup({
count: 0, count: 0,
relatedSearchableKeys: [], relatedSearchableKeys: [],
searchableKeys: [], searchableKeys: [],
canEdit: false,
} }
); );
@@ -129,7 +120,7 @@ function InventoryLookup({
label={t`Inventory`} label={t`Inventory`}
promptId={promptId} promptId={promptId}
promptName={promptName} promptName={promptName}
isDisabled={!canEdit || isDisabled} isDisabled={isOverrideDisabled || isDisabled}
tooltip={t`Select the inventory containing the hosts tooltip={t`Select the inventory containing the hosts
you want this job to manage.`} you want this job to manage.`}
> >
@@ -145,7 +136,7 @@ function InventoryLookup({
fieldName={fieldName} fieldName={fieldName}
validate={validate} validate={validate}
isLoading={isLoading} isLoading={isLoading}
isDisabled={!canEdit || isDisabled} isDisabled={isOverrideDisabled || isDisabled}
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
renderOptionsList={({ state, dispatch, canDelete }) => ( renderOptionsList={({ state, dispatch, canDelete }) => (
<OptionsList <OptionsList
@@ -200,7 +191,7 @@ function InventoryLookup({
onBlur={onBlur} onBlur={onBlur}
required={required} required={required}
isLoading={isLoading} isLoading={isLoading}
isDisabled={!canEdit || isDisabled} isDisabled={isOverrideDisabled || isDisabled}
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
renderOptionsList={({ state, dispatch, canDelete }) => ( renderOptionsList={({ state, dispatch, canDelete }) => (
<OptionsList <OptionsList

View File

@@ -3,7 +3,12 @@ import { useHistory } from 'react-router-dom';
import { CardBody } from 'components/Card'; import { CardBody } from 'components/Card';
import { getAddedAndRemoved } from 'util/lists'; import { getAddedAndRemoved } from 'util/lists';
import { WorkflowJobTemplatesAPI, OrganizationsAPI, UsersAPI } from 'api'; import {
InventoriesAPI,
WorkflowJobTemplatesAPI,
OrganizationsAPI,
UsersAPI,
} from 'api';
import { useConfig } from 'contexts/Config'; import { useConfig } from 'contexts/Config';
import useRequest from 'hooks/useRequest'; import useRequest from 'hooks/useRequest';
import ContentError from 'components/ContentError'; import ContentError from 'components/ContentError';
@@ -80,15 +85,16 @@ function WorkflowJobTemplateEdit({ template }) {
}; };
const { const {
isLoading, isLoading: isFetchUserRoleLoading,
request: fetchUserRole, request: fetchUserRole,
result: { orgAdminResults, isOrgAdmin }, result: { orgAdminResults, isOrgAdmin },
error: contentError, error: fetchUserRoleError,
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const { const {
data: { results, count }, data: { results, count },
} = await UsersAPI.readAdminOfOrganizations(me?.id); } = await UsersAPI.readAdminOfOrganizations(me?.id);
return { isOrgAdmin: count > 0, orgAdminResults: results }; return { isOrgAdmin: count > 0, orgAdminResults: results };
}, [me.id]), }, [me.id]),
{ isOrgAdmin: false, orgAdminResults: null } { isOrgAdmin: false, orgAdminResults: null }
@@ -98,11 +104,37 @@ function WorkflowJobTemplateEdit({ template }) {
fetchUserRole(); fetchUserRole();
}, [fetchUserRole]); }, [fetchUserRole]);
if (contentError) { const {
return <ContentError error={contentError} />; isLoading: isFetchInventoryLoading,
request: fetchInventory,
result: { canChangeInventory },
error: fetchInventoryError,
} = useRequest(
useCallback(async () => {
if (template.inventory) {
const {
data: { count },
} = await InventoriesAPI.read({
role_level: 'use_role',
id: template.inventory,
});
return { canChangeInventory: count && count > 0 };
}
return { canChangeInventory: true };
}, [template.inventory]),
{ canChangeInventory: false }
);
useEffect(() => {
fetchInventory();
}, [fetchInventory]);
if (fetchUserRoleError || fetchInventoryError) {
return <ContentError error={fetchUserRoleError || fetchInventoryError} />;
} }
if (isLoading || !orgAdminResults) { if (isFetchUserRoleLoading || isFetchInventoryLoading || !orgAdminResults) {
return <ContentLoading />; return <ContentLoading />;
} }
@@ -114,6 +146,7 @@ function WorkflowJobTemplateEdit({ template }) {
template={template} template={template}
submitError={formSubmitError} submitError={formSubmitError}
isOrgAdmin={isOrgAdmin} isOrgAdmin={isOrgAdmin}
isInventoryDisabled={!canChangeInventory}
/> />
</CardBody> </CardBody>
); );

View File

@@ -39,6 +39,7 @@ function WorkflowJobTemplateForm({
handleCancel, handleCancel,
submitError, submitError,
isOrgAdmin, isOrgAdmin,
isInventoryDisabled,
}) { }) {
const helpText = getHelpText(); const helpText = getHelpText();
const { setFieldValue, setFieldTouched } = useFormikContext(); const { setFieldValue, setFieldTouched } = useFormikContext();
@@ -150,6 +151,7 @@ function WorkflowJobTemplateForm({
onChange={handleInventoryUpdate} onChange={handleInventoryUpdate}
touched={inventoryMeta.touched} touched={inventoryMeta.touched}
error={inventoryMeta.error} error={inventoryMeta.error}
isOverrideDisabled={isInventoryDisabled}
/> />
</FormGroup> </FormGroup>
<FieldWithPrompt <FieldWithPrompt
@@ -284,6 +286,7 @@ WorkflowJobTemplateForm.propTypes = {
handleCancel: PropTypes.func.isRequired, handleCancel: PropTypes.func.isRequired,
submitError: shape({}), submitError: shape({}),
isOrgAdmin: PropTypes.bool, isOrgAdmin: PropTypes.bool,
isInventoryDisabled: PropTypes.bool,
}; };
WorkflowJobTemplateForm.defaultProps = { WorkflowJobTemplateForm.defaultProps = {
@@ -295,6 +298,7 @@ WorkflowJobTemplateForm.defaultProps = {
project: undefined, project: undefined,
}, },
isOrgAdmin: false, isOrgAdmin: false,
isInventoryDisabled: false,
}; };
const FormikApp = withFormik({ const FormikApp = withFormik({