我想用 django-graphene 做一个 API,但是我想在我用户发出请求时应用 django 默认组权限,如果他的用户组允许他进行 CRUD,这将有权限。
谢谢。
我想用 django-graphene 做一个 API,但是我想在我用户发出请求时应用 django 默认组权限,如果他的用户组允许他进行 CRUD,这将有权限。
谢谢。
这可能不是一个优雅的解决方案,但它适用于突变:
在我的突变中,我有一个自定义函数,如果从 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
限制:我目前还没有尝试用这个来管理查询,只是函数。如果有人在我之前做到这一点,请评论或更新我的回复。