Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taoufik07/django-graphene-permissions
DGP - A DRF like permission system for django graphene
https://github.com/taoufik07/django-graphene-permissions
django django-graphene permissions
Last synced: 10 days ago
JSON representation
DGP - A DRF like permission system for django graphene
- Host: GitHub
- URL: https://github.com/taoufik07/django-graphene-permissions
- Owner: taoufik07
- License: bsd-3-clause
- Created: 2018-11-19T09:32:28.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T01:29:28.000Z (about 2 years ago)
- Last Synced: 2025-01-06T03:23:00.069Z (14 days ago)
- Topics: django, django-graphene, permissions
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 30
- Watchers: 5
- Forks: 10
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DGP - Django graphene permissions
Permission system inspired by DRF
## Installation
Install the latest release:
```shell
$ pip3 install django-graphene-permissions
```
Or using pipenv```shell
$ pipenv install django-graphene-permissions
```## Usage
### Permission definition
---You can create new permissions by subclassing `BasePermission` e.g.
```python
from django_graphene_permissions.permissions import BasePermissionclass 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
```python
from django_graphene_permissions import PermissionDjangoObjectType
from django_graphene_permissions.permissions import IsAuthenticatedclass 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.
```python
from django_graphene_permissions import permissions_checker
from django_graphene_permissions.permissions import IsAuthenticatedclass 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.
```python
from django_graphene_permissions import permissions_checker
from django_graphene_permissions.permissions import IsAuthenticatedclass 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