Skip to main content

A simple way to manage object permissions in drf.

Project description

Permission Manager for django rest framework

example workflow codecov

Use permission_manager for django rest framework.

Full documentation on read the docs.

Install

pip install permission-manager-drf

Example

from django.db import models
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action

from permission_manager_drf import DRFPermissionManager, ManagerPermission
from permission_manager import PermissionResult


# Define permission manager
class SomeModelPermissionManager(DRFPermissionManager):
    def has_create_permission(self) -> bool:
        return self.user.is_staff

    def has_update_permission(self) -> bool:
        return self.user.is_staff

    def has_delete_permission(self) -> bool:
        return self.user.is_staff

    def has_view_permission(self) -> bool:
        return True

    def has_list_permission(self) -> bool:
        return True

    def has_custom_permission(self) -> bool:
        return PermissionResult(
            message="You can't do it",
            value=self.user.is_staff,
        )


# Define model with permission manager attribute
class SomeModel(models.Model):
    permission_manager = SomeModelPermissionManager
    ...


# ViewSet
class TestModelViewSet(ModelViewSet):
    permission_classes = [IsAuthenticated, ManagerPermission]
    ...

    @action(detail=True)
    def custom(self, request, **kwargs):
        ...

That's all. Now every drf action will be checked by the permission manager.

Also, you can use the serializer field for retrieve permissions you need.

from permission_manager_drf import PermissionField
from rest_framework.serializers import ModelSerializer

class SomeModelSerializer(ModelSerializer):
    permissions = PermissionField(actions=('view', 'custom'),)
    ...

"""
Example output:
{
    ...,
    'permissions': {
        'view': {
            'allow': True,
            'messages': None,
        },
        'custom': {
            'allow': False,
            'messages': ["You can't do it"],
        },
    }
}
"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

permission_manager_drf-0.3.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

permission_manager_drf-0.3.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file permission_manager_drf-0.3.1.tar.gz.

File metadata

File hashes

Hashes for permission_manager_drf-0.3.1.tar.gz
Algorithm Hash digest
SHA256 918e23fb06d49f9e579f66239d86f2bae6bb17300685f73f90c2f3d5a7368bb1
MD5 51e4d9c763f58bdc656d4a3346566827
BLAKE2b-256 6d56ee19e5196d18f7d807b1ed66a4091bdbeb32fcddf995bf8369efefb777d5

See more details on using hashes here.

File details

Details for the file permission_manager_drf-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for permission_manager_drf-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e61dcb2a13012802ca7997b8ed12c1a899f2a91f7774d2da6bbff9772f93c8c
MD5 c6087b1c409e7abab8607159173d4870
BLAKE2b-256 20e3d88060831a8cab873afd155e62072d6857b057401527c481a8b2064287e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page