Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissions per object like in DRF and django-guardian #10

Open
sgaseretto opened this issue Mar 4, 2018 · 4 comments
Open

Permissions per object like in DRF and django-guardian #10

sgaseretto opened this issue Mar 4, 2018 · 4 comments
Assignees
Labels

Comments

@sgaseretto
Copy link

Like, for example, you implement a project management software where each user can participate in different projects and have a different role in each project, like SCRUM Master, Product Owner, and Developer. One user can be in different projects and have a different role for each project that will allow him to do different kinds of queries and mutations in each project. Is it possible to achieve something like this with graphene-permissions and how?

@redzej
Copy link
Owner

redzej commented Mar 6, 2018

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.

@redzej redzej added the question label Mar 6, 2018
@redzej redzej self-assigned this Mar 8, 2018
@adamziel
Copy link

@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.

@redzej
Copy link
Owner

redzej commented Jun 20, 2018

@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.

@jrd
Copy link
Contributor

jrd commented Dec 23, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants