Package: graphene-acl
graphene-aclThe motivation for this library is to simplify access control protection for Graphene Fields. A common approach to ACL protection is through the use of a reusable permissions validation decorator. The problem is this is cumbersome for Graphene Fields that use the standard resolver. You are forced to write an unnecessary resolver function just to annotate it with your permissions validator. The second cumbersome problem this library addresses is ACL role based resolvers. Depending on the users role you might want to perform different business logic in order to retrieve the data they requested for a Graphene Field.Installation$pipinstallgraphene-aclUsageacl_classifierThe purpose of the classifier is to return a route key that will be used to determine which resolver function is used for resolving the field. The classifier function has access to all the arguments from the field resolver and can be excuted synchonously or asynchronously.acl_validatorThe purpose of the validator is to authorize access to the field. This validation will occurr before classification routing happens. If authorization validation is different per classification route then you should not use this validator to enforce authorization access. Instead you should authorize at the specific classifier resolver definition.Examplefromgraphene_aclimportAclFieldimportgraphenedefclassifier(root,info,*args,**kwargs):permissions=info.context.jwt.permissionsif'admin'inpermissions:return'admin'if'perm1'inpermissionsandnot'perm2'inpermissions:return'perm1'if'perm2'inpermissionsandnot'perm1'inpermissions:return'perm2'if'perm2'inpermissionsand'perm1'inpermissions:return['perm1','perm2']if'perm3'inpermissions:return'perm3'if'perm4'inpermissions:return'perm4'returnNonedefhas_permissions(permissions):defvalidator(root,info,*args,**kwars):if(any([permissionininfo.context.jwt.permissionsforpermissioninpermissions])):returnTrueraiseAuthorizationError(f'Not authorized to query field{info.field_name}')returnvalidatorclassFoo(graphene.ObjectType):# Demonstrates simple routing based on an Admin classifier route keyadmin_field=AclField(graphene.String,acl_classifier=classifier)restricted_name=AclField(graphene.String,acl_validator=has_permissions(['foo:name:read','admin']))tenant_field=AclField(graphene.String,acl_classifier=classifier)@Foo.admin_field.resolve('admin')defresolve_admin_field(root,info,*args,**kwargs):pass@Foo.admin_field.resolve()defresolve_default_admin_field(root,info,*args,**kwargs):raiseError('Not Authorized')@Foo.tenant_field.resolve('perm1')defresolve_perm1_field(root,info,*args,**kwargs):pass@Foo.tenant_field.resolve('perm2')defresolve_perm2_field(root,info,*args,**kwargs):pass@Foo.tenant_field.resolve(['perm1','perm2'])defresolve_perm1_and_perm2_field(root,info,*args,**kwargs):# Uses sorted() + tuple hashing to register functionpass@Foo.tenant_field.resolve('perm3')@Foo.tenant_field.resolve('perm4')defresolve_perm3_or_perm4_field(root,info,*args,**kwargs):# Registers function for both 'perm3' and 'perm4' route keyspassACL Connection Fieldsfromgraphene_django.filterimportDjangoFilterConnectionFieldfromgraphene_aclimportacl_field_typeBarConnectionField=acl_field_type('BarConnectionField',DjangoFilterConnectionField)classFoo(graphene.ObjectType):bar=BarConnectionField(MyNode,acl_permissions=has_permission('FOO'))DevelopmentFirst time setupInstall Precommit hooksbrew install pre-commit && pre-commit install && pre-commit install --install-hooksInstall poetry:https://github.com/sdispater/poetry#installationcurl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | pythonInstall dependenciespoetry install