Skip to main content

DRF like permission system for django graphene

Project description

DGP - Django graphene permissions REDUX (Updated to support Django 4.x)

Permission system inspired by DRF

Installation

Install the latest release:

$ pip3 install django-graphene-permissions

Or using pipenv

$ pipenv install django-graphene-permissions

Usage

Permission definition


You can create new permissions by subclassing BasePermission e.g.

from django_graphene_permissions.permissions import BasePermission

class MyPermission(BasePermission):

    @staticmethod
    def has_permission(context):
        return context.user and context.user.is_authenticated
    
    @staticmethod
    def has_object_permission(context, obj):
        return True

This package provides predefined permissions :

  • AllowAny : Allow any access.
  • IsAuthenticated : Allow only authenticated users.

Node Permission


Subclass PermissionDjangoObjectType and define the permissions via the static method permission_classes that should return an iterable of permission classes

from django_graphene_permissions import PermissionDjangoObjectType
from django_graphene_permissions.permissions import IsAuthenticated

class ExampleNode(PermissionDjangoObjectType):
    class Meta:
        model = Example
        interfaces = (relay.Node,)

    @staticmethod
    def permission_classes():
        return [IsAuthenticated]

Mutation Permission


Apply the permissions_checker([Permission,...]) decorator to mutate e.g.

from django_graphene_permissions import permissions_checker
from django_graphene_permissions.permissions import IsAuthenticated

class ExampleDeleteMutation(graphene.Mutation):
    ok = graphene.Boolean()

    class Arguments:
        id = graphene.ID()

    @permissions_checker([IsAuthenticated])
    def mutate(self, info, id):
        instance = get_instance(id)
        instance.delete()
        return ExampleDeleteMutation(ok=True)

Query Permission


Apply the permissions_checker([Permission,...]) decorator to the field resolver e.g.

from django_graphene_permissions import permissions_checker
from django_graphene_permissions.permissions import IsAuthenticated

class Query(graphene.ObjectType):
    post = relay.Node.Field(PostNode)
    posts = DjangoFilterConnectionField(PostNode)

    @permissions_checker([IsAuthenticated])
    def resolve_posts(self, info, **kwargs):
        return Post.objects.all()

TODO

  • Improvements
  • Tests
  • Add a PermissionDjangoFilterConnectionField
  • Better docs

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

Built Distribution

File details

Details for the file django_graphene_permissions_redux-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django_graphene_permissions_redux-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a5e79af837db2092525b5608097129ce25e422870c3a78f36f118a6fea68f086
MD5 c97957114385df311a7ee33534242beb
BLAKE2b-256 78ce1bb1f45c4a0bc45ff7598e1b7bbce28404aea86a259c016bfe3b55440240

See more details on using hashes here.

File details

Details for the file django_graphene_permissions_redux-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_graphene_permissions_redux-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c738e4f508ce570a6d7745a6e577185f9b836282a073947c72a3b913eed10bb0
MD5 990b017391849757b4bfa22b9a956dc7
BLAKE2b-256 6cba900cf01e27526150a64b91143db61308a1d91c298d1dd79254aaf56b9d21

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