Skip to content

Commit

Permalink
notification permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 7, 2015
1 parent 778d61a commit 816a00e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions ipynbsrv/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ def is_creator(self, user, obj):
return obj.creator == user.backend_user


class IsObjectSenderMixin(object):

def is_sender(self, user, obj):
if type(obj.sender) == User:
return obj.sender == user
elif type(obj.sender) == BackendUser:
return obj.sender == user.backend_user


class IsSuperUserMixin(object):

def is_superuser(self, user):
Expand Down Expand Up @@ -122,6 +131,22 @@ def has_object_permission(self, request, view, obj):
return False


class IsSuperUserOrSender(
permissions.BasePermission,
IsObjectSenderMixin,
IsBackendUserMixin,
IsSuperUserMixin):
"""
Only allow access to User which is set as sender of the object.
Created for permissions on notifications.
"""

def has_object_permission(self, request, view, obj):
if self.is_superuser(request.user):
return True
return self.is_sender(request.user, obj)


class IsSuperUserOrIsObjectOwnerOrReadOnlyIfPublic(
IsSuperUserOrIsObjectOwner,
IsPublicMixin,
Expand Down
4 changes: 2 additions & 2 deletions ipynbsrv/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def perform_create(self, serializer):
serializer.save(sender=self.request.user)


class NotificationDetail(generics.RetrieveUpdateDestroyAPIView):
class NotificationDetail(generics.RetrieveDestroyAPIView):
"""
Get details of a notification.
"""
Expand All @@ -851,7 +851,7 @@ def get_serializer_class(self, *args, **kwargs):
return FlatNotificationSerializer
return NestedNotificationSerializer

permission_classes = [IsSuperUserOrReadOnly]
permission_classes = [IsSuperUserOrSender]

def get_queryset(self):
if self.request.user.is_superuser:
Expand Down

0 comments on commit 816a00e

Please sign in to comment.