Skip to content

Commit

Permalink
feat(back): add is owner permission in spot, bar & shop views
Browse files Browse the repository at this point in the history
  • Loading branch information
Asiberus committed Dec 11, 2022
1 parent c6b419b commit 7b1f63c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .conf/development/conf.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BACKEND_CONTAINER_PORT=8000
BACKEND_SECRET_KEY=secretKeyHasToBeChanged!
BACKEND_DEBUG=1
BACKEND_ALLOWED_HOSTS=*
BACKEND_USE_EMAIL_FILE_SYSTEM=0
BACKEND_USE_EMAIL_FILE_SYSTEM=1

# DATABASE
DB_POSTGRES_VERSION=14.2-alpine
Expand Down
1 change: 1 addition & 0 deletions back/api/permissions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .is_owner_or_read_only import IsOwnerOrReadOnly
9 changes: 9 additions & 0 deletions back/api/permissions/is_owner_or_read_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework.permissions import BasePermission, SAFE_METHODS


class IsOwnerOrReadOnly(BasePermission):
def has_object_permission(self, request, view, obj):
if request.method in SAFE_METHODS:
return True

return obj.user == request.user
3 changes: 3 additions & 0 deletions back/api/views/bar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAdminUser

from api.permissions import IsOwnerOrReadOnly
from api.serializers.bar import BarExtendedSerializer, BarSerializer
from app.models.bar import Bar


class BarViewSet(viewsets.ModelViewSet):
queryset = Bar.objects.all()
permission_classes = [IsOwnerOrReadOnly | IsAdminUser]

def get_serializer_class(self):
if hasattr(self, 'action'):
Expand Down
3 changes: 3 additions & 0 deletions back/api/views/shop.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAdminUser

from api.permissions import IsOwnerOrReadOnly
from api.serializers.shop import ShopExtendedSerializer, ShopSerializer
from app.models.shop import Shop


class ShopViewSet(viewsets.ModelViewSet):
queryset = Shop.objects.all()
permission_classes = [IsOwnerOrReadOnly | IsAdminUser]

def get_serializer_class(self):
if hasattr(self, 'action'):
Expand Down
3 changes: 3 additions & 0 deletions back/api/views/spot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from rest_framework import viewsets
from rest_framework.permissions import IsAdminUser

from api.permissions import IsOwnerOrReadOnly
from api.serializers.spot import SpotExtendedSerializer, SpotSerializer
from app.models.spot import Spot


class SpotViewSet(viewsets.ModelViewSet):
queryset = Spot.objects.all()
permission_classes = [IsOwnerOrReadOnly | IsAdminUser]

def get_serializer_class(self):
if hasattr(self, 'action'):
Expand Down

0 comments on commit 7b1f63c

Please sign in to comment.