From eed5a5c924c72c305d09b56028d4e9e1d0056a5b Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 14 Aug 2017 10:53:21 -0400 Subject: [PATCH] fix adhoc IG bug and prevent related inventory deletion --- awx/main/access.py | 2 ++ awx/main/models/ad_hoc_commands.py | 2 ++ awx/main/tests/functional/test_rbac_inventory.py | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/awx/main/access.py b/awx/main/access.py index e39ce121c1..bf722c0e2c 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -613,6 +613,8 @@ class InventoryAccess(BaseAccess): for o in Job.objects.filter(inventory=obj, status__in=ACTIVE_STATES)]) active_jobs.extend([dict(type="inventory_update", id=o.id) for o in InventoryUpdate.objects.filter(inventory_source__inventory=obj, status__in=ACTIVE_STATES)]) + active_jobs.extend([dict(type="ad_hoc_command", id=o.id) + for o in AdHocCommand.objects.filter(inventory=obj, status__in=ACTIVE_STATES)]) if len(active_jobs) > 0: raise StateConflict({"conflict": _("Resource is being used by running jobs"), "active_jobs": active_jobs}) diff --git a/awx/main/models/ad_hoc_commands.py b/awx/main/models/ad_hoc_commands.py index b18c23d7f3..00773abcd8 100644 --- a/awx/main/models/ad_hoc_commands.py +++ b/awx/main/models/ad_hoc_commands.py @@ -218,6 +218,8 @@ class AdHocCommand(UnifiedJob, JobNotificationMixin): organization_groups = [] if self.inventory is not None: inventory_groups = [x for x in self.inventory.instance_groups.all()] + else: + inventory_groups = [] selected_groups = inventory_groups + organization_groups if not selected_groups: return self.global_instance_groups diff --git a/awx/main/tests/functional/test_rbac_inventory.py b/awx/main/tests/functional/test_rbac_inventory.py index 2172f90d6d..42218905f3 100644 --- a/awx/main/tests/functional/test_rbac_inventory.py +++ b/awx/main/tests/functional/test_rbac_inventory.py @@ -3,7 +3,8 @@ import pytest from awx.main.models import ( Host, CustomInventoryScript, - Schedule + Schedule, + AdHocCommand ) from awx.main.access import ( InventoryAccess, @@ -11,10 +12,19 @@ from awx.main.access import ( HostAccess, InventoryUpdateAccess, CustomInventoryScriptAccess, - ScheduleAccess + ScheduleAccess, + StateConflict ) +@pytest.mark.django_db +def test_running_job_protection(inventory, admin_user): + AdHocCommand.objects.create(inventory=inventory, status='running') + access = InventoryAccess(admin_user) + with pytest.raises(StateConflict): + access.can_delete(inventory) + + @pytest.mark.django_db def test_custom_inv_script_access(organization, user): u = user('user', False)