【问题标题】:How to use django default group permission in django-graphene?如何在 django-graphene 中使用 django 默认组权限?
【发布时间】:2017-11-11 00:15:52
【问题描述】:

我想用 django-graphene 做一个 API,但是我想在用户提出请求时应用 django 默认组权限,如果他的用户组允许他进行 CRUD,这将具有权限。

谢谢。

【问题讨论】:

    标签: django permissions graphql graphene-python


    【解决方案1】:

    这可能不是一个优雅的解决方案,但它适用于突变:

    在我的突变中,我有一个自定义函数,如果从 info.context.user 中提取的用户在一个组中,则返回 true,否则返回 false:

    class RelayCreateConversation(relay.ClientIDMutation):
        # Set a variable as the field we want to use
        conversation = graphene.Field(ConversationNode)
        # Create a custom response as a string if the user doesn't have authentication
        custom_response = graphene.String()
    
        # What is passed through in the mutation
        class Input:
            participant_number = graphene.String()
    
        def mutate_and_get_payload(root, info, **input):
            submitted_by = info.context.user
            # How I manage authentication
            if not group_required(submitted_by, "send_and_receive_texts"):
                custom_response = "You do not have the correct permissions to do this action"
                return RelayCreateConversation(conversation=custom_response)
            # mutation code here
    

    然后我导入了一个非常简单的辅助函数:

    def group_required(user, *group_names):
        if bool(user.groups.filter(name__in=group_names)) | user.is_superuser:
            return True
        return False
    

    限制:我目前还没有尝试用这个来管理查询,只是函数。如果有人在我之前得到了这个,请评论或更新我的回复。

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2015-02-05
      • 2014-10-16
      • 2018-03-31
      相关资源
      最近更新 更多