minor: filterbackends for permissions
This commit is contained in:
parent
12220a9e18
commit
c1adcfeb8c
18
djangoldp_notification/filters.py
Normal file
18
djangoldp_notification/filters.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from djangoldp.filters import LDPPermissionsFilterBackend
|
||||||
|
|
||||||
|
|
||||||
|
class InboxFilterBackend(LDPPermissionsFilterBackend):
|
||||||
|
def filter_queryset(self, request, queryset, view):
|
||||||
|
if not request.user.is_anonymous:
|
||||||
|
return queryset.filter(user=request.user)
|
||||||
|
else:
|
||||||
|
from djangoldp_notification.models import Notification
|
||||||
|
return Notification.objects.none()
|
||||||
|
|
||||||
|
|
||||||
|
class SubscriptionsFilterBackend(LDPPermissionsFilterBackend):
|
||||||
|
def filter_queryset(self, request, queryset, view):
|
||||||
|
if request.method == "OPTIONS":
|
||||||
|
return queryset
|
||||||
|
else:
|
||||||
|
return super().filter_queryset(request, queryset, view)
|
@ -1,9 +1,13 @@
|
|||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
from djangoldp.permissions import LDPPermissions
|
from djangoldp.permissions import LDPPermissions
|
||||||
|
from djangoldp_notification.filters import InboxFilterBackend, SubscriptionsFilterBackend
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
|
||||||
|
|
||||||
class InboxPermissions(LDPPermissions):
|
class InboxPermissions(LDPPermissions):
|
||||||
|
filter_backends = [InboxFilterBackend]
|
||||||
|
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
from djangoldp.models import Model
|
from djangoldp.models import Model
|
||||||
|
|
||||||
@ -41,6 +45,8 @@ class InboxPermissions(LDPPermissions):
|
|||||||
|
|
||||||
|
|
||||||
class SubscriptionsPermissions(LDPPermissions):
|
class SubscriptionsPermissions(LDPPermissions):
|
||||||
|
filter_backends = [SubscriptionsFilterBackend]
|
||||||
|
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
if request.user.is_anonymous and not request.method == "OPTIONS":
|
if request.user.is_anonymous and not request.method == "OPTIONS":
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user