Comments (4)
Hi, sorry for the late reply.
I'm not sure how your current project or implementation looks like, but yes, it should work quite nice.
I added examples in README - you can use those, or look into tests
directory.
Generalizing, if you have model Project and user groups per project - you could do something like that
- create users, i.e
john
,mark
,adam
. - create project
foo
- assign users to appropriate project
- create permission classes
- create queries/mutations
# example code - You may need more options or fields to get it to work, refer to readme
from django.db import models
from django.contrib.auth.models import User
class Project(models.Model):
name = models.CharField(max_length=128)
owner = models.ForeignKey(User, on_delete=models.PROTECT)
scrum_master = models.ForeignKey(User, on_delete=models.PROTECT)
developer = models.ForeignKey(User, on_delete=models.PROTECT) # or M2M Field
# other fields ...
class AllowProjectOwner:
@staticmethod
def has_node_permission(info, id):
# check if user belongs to specific group or has required role
return info.context.user == Project.objects.get(pk=id).owner
class AllowProjectScrumMaster:
@staticmethod
def has_node_permission(info, id):
return info.context.user == Project.objects.get(pk=id).scrum_master
class ProjectInfoNode(AuthNode, DjangoObjectType):
permission_classes = (AllowProjectOwner,)
class Meta:
model = Project
filter_fields = ('name',)
interfaces = (relay.Node,)
class ProjectScrumNode(AuthNode, DjangoObjectType):
permission_classes = (AllowProjectScrumMaster,)
class Meta:
model = Project
filter_fields = ('name',)
# for example: exclude fields or allow different filters, etc here.
interfaces = (relay.Node,)
class ProjectQuery:
project_info = relay.Node.Field(ProjectInfoNode)
project_scrum_info = relay.Node.Field(ProjectScrumNode)
class Query(ProjectQuery, ObjectType):
pass
# main schema
schema = Schema(query=Query)
I didn't test it thoroughly with django-guardian, but creating groups and checking if user is in specific group should be easy enough. If further integration with guardian would be usefull for more developers, i'll think about adding that, just give some feedback.
from graphene-permissions.
@redzej the problem is here:
class AllowProjectOwner:
@staticmethod
def has_node_permission(info, id):
# check if user belongs to specific group or has required role
return info.context.user == Project.objects.get(pk=id).owner
That's one query, and after this method returns True there's going to be another DB trip to retrieve the same object.
from graphene-permissions.
@adamziel yes, i'm aware of that, this was just a temporary solution. Recently i got some spare time, so i`m going to submit 2 PR's regarding that.
from graphene-permissions.
Django will cache the Project.objects.get(pk=id)
result and there should be no more hit (if you only request that object, i.e. no join).
One improvement could be to specifically only get the owner from the database:
return info.context.user == Project.objects.only('owner').get(pk=id).owner
from graphene-permissions.
Related Issues (10)
- Publish new Pypi version HOT 1
- Python 3.6 and Django 2.1? HOT 3
- PyPI not updated with current requirements HOT 1
- Django integration HOT 1
- ImportError: cannot import name 'get_default_backend'
- Use permission in filter relay
- no module found 'packagin' HOT 4
- Importing ResolveInfo from 'graphql' fails with latest versions of Graphene
- There's no release with PR #36 included HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphene-permissions.