Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

Implement REST API permissions for staff only #178

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ACM_General/ACM_General/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.backends.DjangoFilterBackend',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_api.permissions.IsStaffOrReadonly',
)
}

Expand Down
1 change: 1 addition & 0 deletions ACM_General/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TransactionCategory(models.Model):
verbose_name=_('Category Name'),
help_text=_('The name of the Category'),
max_length=50,
unique=True
)

def __str__(self):
Expand Down
34 changes: 12 additions & 22 deletions ACM_General/rest_api/permissions.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
"""
from rest_framework import permissions


class IsStaffOrReadOnly(permissions.BasePermission):

Custom permission which only allows admins to edit data.


def has_object_permission(self, request, view, obj):
return request.user.is_staff


class IsOwnerOrReadOnly(permissions.BasePermission):

Custom permission which only allows Owners to edit data

Requires that the Model/Serializer has field email.

TODO: Make it so that it does not require there to be email.


def has_object_permission(self, request, view, obj):
class IsStaffOrReadonly(permissions.BasePermission):
"""
Allows only staff members, classified by the `is_staff` method of the User
model, to have complete permissions if they are logged in. Otherwise, the
Users can only perform the safe methods of GET, HEAD, or OPTIONS.
"""
@staticmethod
def has_permission(request, view):
if request.method in permissions.SAFE_METHODS:
return True

return request.user.email == obj.email
"""
if not request.user.is_authenticated:
return False

return request.user.is_staff
Loading