Skip to content

Commit

Permalink
[change] Temporarily import logic from openwisp_users
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Jan 23, 2021
1 parent 62e3298 commit 609c7ed
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 108 deletions.
3 changes: 1 addition & 2 deletions openwisp_ipam/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from openwisp_users.api.mixins import FilterSerializerByOrgManaged
from openwisp_utils.api.serializers import ValidatedModelSerializer
from rest_framework import serializers
from swapper import load_model

from .utils import FilterSerializerByOrgManaged

IpAddress = load_model('openwisp_ipam', 'IpAddress')
Subnet = load_model('openwisp_ipam', 'Subnet')

Expand Down
81 changes: 1 addition & 80 deletions openwisp_ipam/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,10 @@
import swapper
from django.contrib.auth.models import Permission
from django.core.exceptions import ValidationError
from rest_framework.exceptions import NotFound, PermissionDenied
from rest_framework.exceptions import PermissionDenied

Organization = swapper.load_model('openwisp_users', 'Organization')


class FilterByOrganization:
def get_queryset(self):
qs = super().get_queryset()
# superuser has access to every organization
if self.request.user.is_superuser:
return qs
# non superuser has access only to some organizations
return self.get_organization_queryset(qs)

def get_organization_queryset(self):
raise NotImplementedError()


class FilterByOrganizationManaged(FilterByOrganization):
"""
Allows to filter only organizations which the current user manages
"""

def get_organization_queryset(self, qs):
return qs.filter(organization__in=self.request.user.organizations_managed)


class FilterByParent:
def get_queryset(self):
qs = super().get_queryset()
self.assert_parent_exists()
return qs

def assert_parent_exists(self):
parent_queryset = self.get_parent_queryset()
if not self.request.user.is_superuser:
parent_queryset = self.get_organization_queryset(parent_queryset)
try:
assert parent_queryset.exists()
except (AssertionError, ValidationError):
raise NotFound(detail='No relevant data found.')

def get_parent_queryset(self):
raise NotImplementedError()

def get_organization_queryset(self):
raise NotImplementedError()


class FilterByParentManaged(FilterByParent):
def get_organization_queryset(self, qs):
return qs.filter(organization__in=self.request.user.organizations_managed)


class AuthorizeCSVImport:
def assert_organization_permissions(self, request):
if request.user.is_superuser:
Expand Down Expand Up @@ -83,32 +33,3 @@ def get_user_organizations(self):
class AuthorizeCSVOrgManaged(AuthorizeCSVImport):
def get_user_organizations(self):
return self.request.user.organizations_managed


class FilterSerializerByOrganization:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.context['request'].user.is_superuser:
return
self.filter_fields()

def filter_fields(self):
raise NotImplementedError()


class FilterSerializerByOrgManaged(FilterSerializerByOrganization):
def filter_fields(self):
user = self.context['request'].user
organization_filter = user.organizations_managed
for field in self.fields:
if field == 'organization':
self.fields[field].queryset = self.fields[field].queryset.filter(
pk__in=organization_filter
)
continue
try:
self.fields[field].queryset = self.fields[field].queryset.filter(
organization__in=organization_filter
)
except AttributeError:
pass
11 changes: 5 additions & 6 deletions openwisp_ipam/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from openwisp_users.api.authentication import BearerAuthentication
from openwisp_users.api.mixins import FilterByOrganizationManaged, FilterByParentManaged
from openwisp_users.api.permissions import IsOrganizationManager
from rest_framework import pagination, serializers, status
from rest_framework.authentication import SessionAuthentication
Expand All @@ -30,11 +31,7 @@
IpRequestSerializer,
SubnetSerializer,
)
from .utils import (
AuthorizeCSVOrgManaged,
FilterByOrganizationManaged,
FilterByParentManaged,
)
from .utils import AuthorizeCSVOrgManaged

IpAddress = swapper.load_model('openwisp_ipam', 'IpAddress')
Subnet = swapper.load_model('openwisp_ipam', 'Subnet')
Expand Down Expand Up @@ -178,7 +175,9 @@ def get_queryset(self):
return subnet.ipaddress_set.all().order_by('ip_address')


class SubnetListCreateView(FilterByOrganizationManaged, ProtectedAPIMixin, ListCreateAPIView):
class SubnetListCreateView(
FilterByOrganizationManaged, ProtectedAPIMixin, ListCreateAPIView
):
serializer_class = SubnetSerializer
pagination_class = ListViewPagination
queryset = Subnet.objects.all()
Expand Down
21 changes: 2 additions & 19 deletions openwisp_ipam/tests/test_multitenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
from openwisp_users.tests.utils import TestMultitenantAdminMixin
from swapper import load_model

from openwisp_ipam.api.utils import (
AuthorizeCSVImport,
FilterByOrganization,
FilterByParent,
FilterSerializerByOrganization,
)
from openwisp_ipam.api.utils import AuthorizeCSVImport

from . import CreateModelsMixin, PostDataMixin

Expand Down Expand Up @@ -430,21 +425,9 @@ def test_browsable_api_ipaddress_list(self):
self.assertContains(response, '10.0.0.0/24</option>')
self.assertContains(response, '10.10.0.0/24</option>')

def test_not_implemented_error(self):
with self.assertRaises(NotImplementedError):
FilterByOrganization.get_organization_queryset(self)

with self.assertRaises(NotImplementedError):
FilterByParent.get_parent_queryset(self)

with self.assertRaises(NotImplementedError):
FilterByParent.get_organization_queryset(self)

def test_authorize_csv_import_implementation_error(self):
with self.assertRaises(NotImplementedError):
AuthorizeCSVImport.get_csv_organization(self)

with self.assertRaises(NotImplementedError):
AuthorizeCSVImport.get_user_organizations(self)

with self.assertRaises(NotImplementedError):
FilterSerializerByOrganization.filter_fields(self)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
openwisp-users~=0.5.1
# TODO: change this when next version of openwisp_users is released
openwisp-users @ https://github.com/purhan/openwisp-users/tarball/issues/210-add-filter-mixins
openwisp-utils[rest]~=0.7.2
django>=2.2,<3.2
swapper~=1.1
Expand Down

0 comments on commit 609c7ed

Please sign in to comment.