diff --git a/api/tacticalrmm/automation/models.py b/api/tacticalrmm/automation/models.py index bbe8fc27..c4b91f2f 100644 --- a/api/tacticalrmm/automation/models.py +++ b/api/tacticalrmm/automation/models.py @@ -17,23 +17,25 @@ class Policy(models.Model): explicit_clients = self.clients.all() explicit_sites = self.sites.all() - filtered_sites_pks = list() - client_pks = list() + filtered_agents_pks = list() for site in explicit_sites: if site.client not in explicit_clients: - filtered_sites_pks.append(site.pk) + filtered_agents_pks.append( + Agent.objects.filter(client=site.client.client, site=site.site) + .values_list('pk', flat=True) + ) for client in explicit_clients: - client_pks.append(client.pk) - for site in client.sites.all(): - filtered_sites_pks.append(site.pk) + filtered_agents_pks.append( + Agent.objects.filter(client=client.client).values_list('pk', flat=True) + ) return Agent.objects.filter( - models.Q(pk__in=explicit_agents.only("pk")) - | models.Q(pk__in=filtered_sites_pks) - | models.Q(pk__in=client_pks) - ).distinct() + models.Q(pk__in=filtered_agents_pks) + | models.Q(pk__in=explicit_agents.only("pk")) + ) + @staticmethod def cascade_policy_tasks(agent): diff --git a/api/tacticalrmm/tacticalrmm/test.py b/api/tacticalrmm/tacticalrmm/test.py index cbdc7f4b..d1c50d1c 100644 --- a/api/tacticalrmm/tacticalrmm/test.py +++ b/api/tacticalrmm/tacticalrmm/test.py @@ -125,3 +125,70 @@ class BaseTestCase(TestCase): } r = switch.get(method) self.assertEqual(r.status_code, 401) + + def generate_agents(self, numOfAgents, numOfClients, numOfSites): + + for c in range(numOfClients): + client = Client.objects.create( + client=f"Client{c}" + ) + + for s in range(numOfSites): + site = Site.objects.create( + client=client, + site=f"Site{s}" + ) + + for a in range(numOfAgents): + + Agent.objects.create( + operating_system="Windows 10", + plat="windows", + plat_release="windows-Server2019", + hostname=f"Agent{a}", + local_ip="10.0.25.188", + agent_id=f"71AHC-AA813-HH1BC-AAHH5-00013|Agent{a}", + services=[ + { + "pid": 880, + "name": "AeLookupSvc", + "status": "stopped", + "binpath": "C:\\Windows\\system32\\svchost.exe -k netsvcs", + "username": "localSystem", + "start_type": "manual", + "description": "Processes application compatibility cache requests for applications as they are launched", + "display_name": "Application Experience", + }, + { + "pid": 812, + "name": "ALG", + "status": "stopped", + "binpath": "C:\\Windows\\System32\\alg.exe", + "username": "NT AUTHORITY\\LocalService", + "start_type": "manual", + "description": "Provides support for 3rd party protocol plug-ins for Internet Connection Sharing", + "display_name": "Application Layer Gateway Service", + }, + ], + public_ip="74.13.24.14", + total_ram=16, + used_ram=33, + disks={ + "C:": { + "free": "42.3G", + "used": "17.1G", + "total": "59.5G", + "device": "C:", + "fstype": "NTFS", + "percent": 28, + } + }, + boot_time=8173231.4, + logged_in_username="John", + client=client.client, + site=site.site, + monitoring_type="server", + description="Test PC", + mesh_node_id="abcdefghijklmnopAABBCCDD77443355##!!AI%@#$%#*", + last_seen=djangotime.now(), + )