Skip to main content

Simple graphene-django permission system.

Project description

Fork from redzej/graphene-permissions

Forked from the original repository and modified to run on the latest graphene (3.2~)

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,)

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

graphene-permissions2-1.2.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

graphene_permissions2-1.2.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file graphene-permissions2-1.2.1.tar.gz.

File metadata

  • Download URL: graphene-permissions2-1.2.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for graphene-permissions2-1.2.1.tar.gz
Algorithm Hash digest
SHA256 504a43eb1bfc73b65c5c90c6a860597e41550e254d16b3c48e7ec8dc8501188d
MD5 b75c49d23f87fa0df9dd45948c40b669
BLAKE2b-256 4c4c41be9e6a108eaf1635f90b5342d7d2aba9bded502f6e65fb22229973c374

See more details on using hashes here.

File details

Details for the file graphene_permissions2-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_permissions2-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98097bb0fea383aaed859af49cfd0b23abce9b0c08144164227cded4d177b0d8
MD5 fd6eda5fdf748c5b2494601d19e30cfe
BLAKE2b-256 bb9a1a80034ad1f6f212832ffd0732d1010022d02029bf5c651cadf72d39efe0

See more details on using hashes here.

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