
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
djangorestframework-guardian
Advanced tools
django-rest-framework-guardian provides django-guardian integrations for Django REST Framework.
To use django-rest-framework-guardian, install it into your environment.
$ pip install djangorestframework-guardian
Ensure both Django REST Framework and django-guardian are configured and added to your INSTALLED_APPS
setting.
INSTALLED_APPS = [
'rest_framework',
'guardian',
]
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
]
The filter will ensure that querysets only returns objects for which the user has the appropriate view permission.
If you're using ObjectPermissionsFilter
, you'll probably also want to add an appropriate object permissions
class, to ensure that users can only operate on instances if they have the appropriate object permissions. The easiest
way to do this is to subclass DjangoObjectPermissions
and add 'view'
permissions to the perms_map
attribute.
An example using both ObjectPermissionsFilter
and DjangoObjectPermissions
might look like the following:
permissions.py:
from rest_framework import permissions
class CustomObjectPermissions(permissions.DjangoObjectPermissions):
"""
Similar to `DjangoObjectPermissions`, but adding 'view' permissions.
"""
perms_map = {
'GET': ['%(app_label)s.view_%(model_name)s'],
'OPTIONS': ['%(app_label)s.view_%(model_name)s'],
'HEAD': ['%(app_label)s.view_%(model_name)s'],
'POST': ['%(app_label)s.add_%(model_name)s'],
'PUT': ['%(app_label)s.change_%(model_name)s'],
'PATCH': ['%(app_label)s.change_%(model_name)s'],
'DELETE': ['%(app_label)s.delete_%(model_name)s'],
}
views.py:
from rest_framework import viewsets
from rest_framework_guardian import filters
from myapp.models import Event
from myapp.permissions import CustomObjectPermissions
from myapp.serializers import EventSerializer
class EventViewSet(viewsets.ModelViewSet):
"""
Viewset that only lists events if user has 'view' permissions, and only
allows operations on individual events if user has appropriate 'view', 'add',
'change' or 'delete' permissions.
"""
queryset = Event.objects.all()
serializer_class = EventSerializer
permission_classes = [CustomObjectPermissions]
filter_backends = [filters.ObjectPermissionsFilter]
A serializer mixin that allows permissions to be easily assigned to users and/or groups.
So each time an object is created or updated, the permissions_map
returned by Serializer.get_permissions_map
will be used to assign permission(s) to that object.
Please note that the existing permissions will remain intact.
A usage example might look like the following:
from rest_framework_guardian.serializers import ObjectPermissionsAssignmentMixin
from blog.models import Post
class PostSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):
class Meta:
model = Post
fields = '__all__'
def get_permissions_map(self, created):
current_user = self.context['request'].user
readers = Group.objects.get(name='readers')
supervisors = Group.objects.get(name='supervisors')
return {
'view_post': [current_user, readers],
'change_post': [current_user],
'delete_post': [current_user, supervisors]
}
$ pip install -U setuptools build twine
$ rm -rf dist/
$ python -m build
$ twine upload -r test dist/*
$ twine upload dist/*
See: LICENSE
FAQs
django-guardian support for Django REST Framework
We found that djangorestframework-guardian demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.