Skip to content

Commit

Permalink
Merge pull request ansible#12874 from mabashian/wf-inv-permissions
Browse files Browse the repository at this point in the history
Fixed bug where inventory field was erroneously disabled on WFJT form
  • Loading branch information
akus062381 authored Sep 27, 2022
2 parents 84fa19f + 7143777 commit 4ffa577
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
20 changes: 4 additions & 16 deletions awx/ui/src/components/Lookup/InventoryLookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ function InventoryLookup({
hideSmartInventories,
history,
isDisabled,
isOverrideDisabled,
isPromptableField,
onBlur,
onChange,
Expand All @@ -39,13 +38,7 @@ function InventoryLookup({
const autoPopulateLookup = useAutoPopulateLookup(onChange);

const {
result: {
inventories,
count,
relatedSearchableKeys,
searchableKeys,
canEdit,
},
result: { inventories, count, relatedSearchableKeys, searchableKeys },
request: fetchInventories,
error,
isLoading,
Expand Down Expand Up @@ -85,8 +78,6 @@ function InventoryLookup({
key,
type: actionsResponse.data.actions?.GET[key].type,
})),
canEdit:
Boolean(actionsResponse.data.actions.POST) || isOverrideDisabled,
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [autoPopulate, autoPopulateLookup, history.location]),
Expand All @@ -95,7 +86,6 @@ function InventoryLookup({
count: 0,
relatedSearchableKeys: [],
searchableKeys: [],
canEdit: false,
}
);

Expand Down Expand Up @@ -129,7 +119,7 @@ function InventoryLookup({
label={t`Inventory`}
promptId={promptId}
promptName={promptName}
isDisabled={!canEdit || isDisabled}
isDisabled={isDisabled}
tooltip={t`Select the inventory containing the hosts
you want this job to manage.`}
>
Expand All @@ -145,7 +135,7 @@ function InventoryLookup({
fieldName={fieldName}
validate={validate}
isLoading={isLoading}
isDisabled={!canEdit || isDisabled}
isDisabled={isDisabled}
qsConfig={QS_CONFIG}
renderOptionsList={({ state, dispatch, canDelete }) => (
<OptionsList
Expand Down Expand Up @@ -200,7 +190,7 @@ function InventoryLookup({
onBlur={onBlur}
required={required}
isLoading={isLoading}
isDisabled={!canEdit || isDisabled}
isDisabled={isDisabled}
qsConfig={QS_CONFIG}
renderOptionsList={({ state, dispatch, canDelete }) => (
<OptionsList
Expand Down Expand Up @@ -251,7 +241,6 @@ InventoryLookup.propTypes = {
fieldName: string,
hideSmartInventories: bool,
isDisabled: bool,
isOverrideDisabled: bool,
onChange: func.isRequired,
required: bool,
validate: func,
Expand All @@ -264,7 +253,6 @@ InventoryLookup.defaultProps = {
fieldName: 'inventory',
hideSmartInventories: false,
isDisabled: false,
isOverrideDisabled: false,
required: false,
validate: () => {},
value: null,
Expand Down
4 changes: 2 additions & 2 deletions awx/ui/src/components/Lookup/InventoryLookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('InventoryLookup', () => {
await act(async () => {
wrapper = mountWithContexts(
<Formik>
<InventoryLookup isOverrideDisabled onChange={() => {}} />
<InventoryLookup onChange={() => {}} />
</Formik>
);
});
Expand All @@ -121,7 +121,7 @@ describe('InventoryLookup', () => {
await act(async () => {
wrapper = mountWithContexts(
<Formik>
<InventoryLookup onChange={() => {}} />
<InventoryLookup isDisabled onChange={() => {}} />
</Formik>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { useHistory } from 'react-router-dom';

import { CardBody } from 'components/Card';
import { getAddedAndRemoved } from 'util/lists';
import { WorkflowJobTemplatesAPI, OrganizationsAPI, UsersAPI } from 'api';
import {
InventoriesAPI,
WorkflowJobTemplatesAPI,
OrganizationsAPI,
UsersAPI,
} from 'api';
import { useConfig } from 'contexts/Config';
import useRequest from 'hooks/useRequest';
import ContentError from 'components/ContentError';
Expand Down Expand Up @@ -80,15 +85,16 @@ function WorkflowJobTemplateEdit({ template }) {
};

const {
isLoading,
isLoading: isFetchUserRoleLoading,
request: fetchUserRole,
result: { orgAdminResults, isOrgAdmin },
error: contentError,
error: fetchUserRoleError,
} = useRequest(
useCallback(async () => {
const {
data: { results, count },
} = await UsersAPI.readAdminOfOrganizations(me?.id);

return { isOrgAdmin: count > 0, orgAdminResults: results };
}, [me.id]),
{ isOrgAdmin: false, orgAdminResults: null }
Expand All @@ -98,11 +104,37 @@ function WorkflowJobTemplateEdit({ template }) {
fetchUserRole();
}, [fetchUserRole]);

if (contentError) {
return <ContentError error={contentError} />;
const {
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 />;
}

Expand All @@ -114,6 +146,7 @@ function WorkflowJobTemplateEdit({ template }) {
template={template}
submitError={formSubmitError}
isOrgAdmin={isOrgAdmin}
isInventoryDisabled={!canChangeInventory}
/>
</CardBody>
);
Expand Down
4 changes: 4 additions & 0 deletions awx/ui/src/screens/Template/shared/WorkflowJobTemplateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function WorkflowJobTemplateForm({
handleCancel,
submitError,
isOrgAdmin,
isInventoryDisabled,
}) {
const helpText = getHelpText();
const { setFieldValue, setFieldTouched } = useFormikContext();
Expand Down Expand Up @@ -150,6 +151,7 @@ function WorkflowJobTemplateForm({
onChange={handleInventoryUpdate}
touched={inventoryMeta.touched}
error={inventoryMeta.error}
isDisabled={isInventoryDisabled}
/>
</FormGroup>
<FieldWithPrompt
Expand Down Expand Up @@ -284,6 +286,7 @@ WorkflowJobTemplateForm.propTypes = {
handleCancel: PropTypes.func.isRequired,
submitError: shape({}),
isOrgAdmin: PropTypes.bool,
isInventoryDisabled: PropTypes.bool,
};

WorkflowJobTemplateForm.defaultProps = {
Expand All @@ -295,6 +298,7 @@ WorkflowJobTemplateForm.defaultProps = {
project: undefined,
},
isOrgAdmin: false,
isInventoryDisabled: false,
};

const FormikApp = withFormik({
Expand Down

0 comments on commit 4ffa577

Please sign in to comment.