Skip to main content

graphene-permissions

Permission system for graphene-django apps.

Build Status PyPI version Python 3.6 codecov Maintainability

Overview

DRF-inspired permission system based on classes for graphene-django. Allows easy customization of permission classes for for queries and mutations.

Requirements

  • Python 3.5+
  • Django 2.0+
  • graphene-django 2.0+

Installation

Install using pip:

pip install graphene-permissions

Example

To enforce permission system, add appropriate mixin and set attribute permission_classes.

### models.py
from django.db import models


class Pet(models.Model):
    name = models.CharField(max_length=32)
    race = models.CharField(max_length=64)
### schema.py
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_permissions.mixins import AuthNode
from graphene_permissions.permissions import AllowAuthenticated


class PetNode(AuthNode, DjangoObjectType):
    permission_classes = (AllowAuthenticated,)

    class Meta:
        model = Pet
        filter_fields = ('name',)
        interfaces = (relay.Node,)

Docs

Setting up permission check

For queries use AuthNode mixin and inherite from AuthFilter class.

class AllowAuthenticatedPetNode(AuthNode, DjangoObjectType):
    permission_classes = (AllowAuthenticated,)

    class Meta:
        model = Pet
        filter_fields = ('name',)
        interfaces = (relay.Node,)


class AllowAuthenticatedFilter(AuthFilter):
    permission_classes = (AllowAuthenticated,)


class PetsQuery:
    user_pet = relay.Node.Field(AllowAuthenticatedPetNode)
    all_user_pets = AllowAuthenticatedFilter(AllowAuthenticatedPetNode)

For mutations use AuthMutation mixin.

class AuthenticatedAddPet(AuthMutation, ClientIDMutation):
    permission_classes = (AllowAuthenticated,)
    pet = graphene.Field(AllowAuthenticatedPetNode)

    class Input:
        name = graphene.String()
        race = graphene.String()
        owner = graphene.ID()

    @classmethod
    def mutate_and_get_payload(cls, root, info, **input):
        if cls.has_permission(root, info, input):
            owner = User.objects.get(pk=from_global_id(input['owner'])[1])
            pet = Pet.objects.create(name=input['name'], race=input['race'], owner=owner)
            return AuthenticatedAddPet(pet=pet)
        return AuthenticatedAddPet(pet=None)


class PetsMutation:
    authenticated_add_pet = AuthenticatedAddPet.Field()

Customizing permission classes

Default permission classes are: AllowAny, AllowAuthenticated, AllowStaff. You can set up equal permission for both queries and mutations with one class, simply subclass one of these classes and to limit access for given object, override appropriate method. Remember to return true if user should be given access and false, if denied.

class AllowMutationForStaff(AllowAuthenticated):
    @staticmethod
    def has_node_permission(info, id):
        # logic here
        # return boolean

    @staticmethod
    def has_mutation_permission(root, info, input):
        if info.request.user.is_staff:
            return True
        return False

    @staticmethod
    def has_filter_permission(info):
        # logic here
        # return boolean

Multiple permissions

You can set up multiple permissions checks, simply adding more classes. Permission is evaluated for every class. If one of the checks fails, access is denied.

class CustomPetNode(AuthNode, DjangoObjectType):
    permission_classes = (AllowAuthenticated, AllowStaff, AllowCustom)

    class Meta:
        model = Pet
        interfaces = (relay.Node,)

Metadata

Release files for graphene-permissions 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphene-permissions 1.1.4
File Size Uploaded
graphene-permissions-1.1.4.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graphene-permissions 1.1.4
File Interpreter ABI Platform
graphene_permissions-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / graphene-permissions-1.1.4.tar.gz

Download URL graphene-permissions-1.1.4.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8a201e352ee8b559ce069093a4485dbf0aba38cbb82042ebd521368a8310ab86
BLAKE2b-256 checksum
How to use checksums
2657220693c946361eaf3683dc217bc680bd63dd574ae1cf0dff69f40cd48a83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.12

Release files / graphene_permissions-1.1.4-py3-none-any.whl

Download URL graphene_permissions-1.1.4-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b55d2dc45c9a708fd642ca1e8890075a733807a1c634d5696dbfe71eb4a7446
BLAKE2b-256 checksum
How to use checksums
d2e390bcf3d3a0cd5ae15f365945a411483833f07a1ed0860c54afb61673d647
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.12

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

1 release file

1.1.0

2 release files

1.0.12

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page