Skip to content

Commit 944c990

Browse files
committed
[fix] Made requested changes
1 parent 4aae33d commit 944c990

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

openwisp_radius/api/serializers.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,19 @@ class Meta:
373373

374374
def __init__(self, *args, **kwargs):
375375
super().__init__(*args, **kwargs)
376+
view = self.context.get("view")
376377
if (
377-
self.context.get("view")
378-
and getattr(self.context["view"], "get_parent_queryset", None)
379-
and not getattr(self.context["view"], "swagger_fake_view", False)
378+
view
379+
and getattr(view, "get_parent_queryset", None)
380+
and not getattr(view, "swagger_fake_view", False)
380381
):
381-
self._user = self.context["view"].get_parent_queryset().first()
382+
self._user = view.get_parent_queryset().first()
382383
else:
383384
self._user = None
384-
if self._user:
385+
if self._user and view and getattr(view.request, "user", None):
386+
orgs = view.request.user.organizations_managed
385387
self.fields["group"].queryset = self.fields["group"].queryset.filter(
386-
organization_id__in=self._user.organizations_dict.keys()
388+
organization__in=orgs
387389
)
388390
else:
389391
self.fields["group"].queryset = self.fields["group"].queryset.none()

openwisp_radius/tests/test_api/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,21 @@ def test_radius_user_group_detail(self):
14951495
self.assertEqual(rug.group, org1_power_users_group)
14961496
self.assertEqual(rug.priority, 4)
14971497

1498+
with self.subTest("Org manager cannot assign group from another org"):
1499+
self._create_org_user(user=target_user, organization=org2)
1500+
org2_group = RadiusGroup.objects.get(organization=org2, name="org-2-users")
1501+
response = self.client.put(
1502+
url,
1503+
{"group": str(org2_group.pk), "priority": 8},
1504+
content_type="application/json",
1505+
)
1506+
self.assertEqual(
1507+
response.status_code,
1508+
status.HTTP_400_BAD_REQUEST,
1509+
)
1510+
rug.refresh_from_db()
1511+
self.assertEqual(rug.group, org1_power_users_group)
1512+
14981513
with self.subTest("DELETE operation"):
14991514
response = self.client.delete(url)
15001515
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

0 commit comments

Comments
 (0)