Created
December 31, 2011 20:30
-
-
Save pydanny/1545255 to your computer and use it in GitHub Desktop.
django-rest-framework permissions by groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" User Django Rest Framework to check to see if an authenticated user | |
is in a particular group | |
Usage:: | |
from api.group_permissions import GroupAPIGETPermission | |
class SearchProductView(View): | |
permissions = (IsAuthenticated, GroupAPIGETPermission,) | |
""" | |
from django.contrib.auth.models import Group | |
from djangorestframework.permissions import _403_FORBIDDEN_RESPONSE, BasePermission | |
class GroupBasePermission(BasePermission): | |
group_name = "" | |
def check_permission(self, user): | |
""" | |
Should simply return, or raise a 403 response. | |
""" | |
try: | |
user.groups.get(name=self.group_name) | |
except Group.DoesNotExist: | |
raise _403_FORBIDDEN_RESPONSE | |
class GroupAPIGETPermission(GroupBasePermission): | |
""" | |
Checks to see if a user is in a particular group | |
""" | |
group_name = "API GET" | |
class GroupAPIPOSTPermission(GroupBasePermission): | |
""" | |
Checks to see if a user is in a particular group | |
""" | |
group_name = "API POST" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possibly something like this?...
Then:
Add 'api_reader' and 'api_writer' groups.
Give the groups the 'api_read' and 'api_write' permissions.
Assign users to groups.