Skip to main content

A simple way to manage object permissions in drf.

Project description

Permission Manager for django rest framework

PyPI - Version 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-1.0.1.tar.gz (50.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

permission_manager_drf-1.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: permission_manager_drf-1.0.1.tar.gz
  • Upload date:
  • Size: 50.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for permission_manager_drf-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f547749d7ddfe91c48edba53aa42dfea3304e63eb1e22fb5a86dc059d8416bf1
MD5 deddb14c1054b194cef2d710de50d70a
BLAKE2b-256 ea01c9a21e29e567d2bbcb1dd3f29a77dd35272cddf113684da11277149ac666

See more details on using hashes here.

Provenance

The following attestation bundles were made for permission_manager_drf-1.0.1.tar.gz:

Publisher: release.yml on kindlycat/permission-manager-drf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for permission_manager_drf-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3539d5bcabb7a31b1a5f19d04a999a15fda31eb9d8edd0ac23844831e5d16ee4
MD5 f3d9086523bfc0e588812da24a91030f
BLAKE2b-256 c8bfdb5016444cad96078e6da6d79523a0d89b7a0b0a0d6b5e1c919d9b003f2d

See more details on using hashes here.

Provenance

The following attestation bundles were made for permission_manager_drf-1.0.1-py3-none-any.whl:

Publisher: release.yml on kindlycat/permission-manager-drf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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