From cc6eaa7f447f7692176cf4cc19faf66bfb213ddc Mon Sep 17 00:00:00 2001 From: mabashian Date: Tue, 13 Sep 2022 10:33:08 -0400 Subject: [PATCH] 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. --- .../src/components/Lookup/InventoryLookup.js | 17 ++----- .../WorkflowJobTemplateEdit.js | 45 ++++++++++++++++--- .../shared/WorkflowJobTemplateForm.js | 4 ++ 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/awx/ui/src/components/Lookup/InventoryLookup.js b/awx/ui/src/components/Lookup/InventoryLookup.js index 04f7e522df..997f18c752 100644 --- a/awx/ui/src/components/Lookup/InventoryLookup.js +++ b/awx/ui/src/components/Lookup/InventoryLookup.js @@ -39,13 +39,7 @@ function InventoryLookup({ const autoPopulateLookup = useAutoPopulateLookup(onChange); const { - result: { - inventories, - count, - relatedSearchableKeys, - searchableKeys, - canEdit, - }, + result: { inventories, count, relatedSearchableKeys, searchableKeys }, request: fetchInventories, error, isLoading, @@ -85,8 +79,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]), @@ -95,7 +87,6 @@ function InventoryLookup({ count: 0, relatedSearchableKeys: [], searchableKeys: [], - canEdit: false, } ); @@ -129,7 +120,7 @@ function InventoryLookup({ label={t`Inventory`} promptId={promptId} promptName={promptName} - isDisabled={!canEdit || isDisabled} + isDisabled={isOverrideDisabled || isDisabled} tooltip={t`Select the inventory containing the hosts you want this job to manage.`} > @@ -145,7 +136,7 @@ function InventoryLookup({ fieldName={fieldName} validate={validate} isLoading={isLoading} - isDisabled={!canEdit || isDisabled} + isDisabled={isOverrideDisabled || isDisabled} qsConfig={QS_CONFIG} renderOptionsList={({ state, dispatch, canDelete }) => ( ( { const { data: { results, count }, } = await UsersAPI.readAdminOfOrganizations(me?.id); + return { isOrgAdmin: count > 0, orgAdminResults: results }; }, [me.id]), { isOrgAdmin: false, orgAdminResults: null } @@ -98,11 +104,37 @@ function WorkflowJobTemplateEdit({ template }) { fetchUserRole(); }, [fetchUserRole]); - if (contentError) { - return ; + 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 ; } - if (isLoading || !orgAdminResults) { + if (isFetchUserRoleLoading || isFetchInventoryLoading || !orgAdminResults) { return ; } @@ -114,6 +146,7 @@ function WorkflowJobTemplateEdit({ template }) { template={template} submitError={formSubmitError} isOrgAdmin={isOrgAdmin} + isInventoryDisabled={!canChangeInventory} /> ); diff --git a/awx/ui/src/screens/Template/shared/WorkflowJobTemplateForm.js b/awx/ui/src/screens/Template/shared/WorkflowJobTemplateForm.js index 9d974f3105..a8f74cefe5 100644 --- a/awx/ui/src/screens/Template/shared/WorkflowJobTemplateForm.js +++ b/awx/ui/src/screens/Template/shared/WorkflowJobTemplateForm.js @@ -39,6 +39,7 @@ function WorkflowJobTemplateForm({ handleCancel, submitError, isOrgAdmin, + isInventoryDisabled, }) { const helpText = getHelpText(); const { setFieldValue, setFieldTouched } = useFormikContext(); @@ -150,6 +151,7 @@ function WorkflowJobTemplateForm({ onChange={handleInventoryUpdate} touched={inventoryMeta.touched} error={inventoryMeta.error} + isOverrideDisabled={isInventoryDisabled} />