Skip to content

Commit

Permalink
🐛 fix: auth permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus-Doreto committed Nov 16, 2023
1 parent 1b9467e commit 1c6ab62
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion graphemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def query(cls):
async def field(
self, info, filters: cls.filter | None = None
) -> list[cls.schema]:
if not Setup.get_permission(folder, info):
if not await Setup.get_permission(folder, info.context):
return []
return await get_all(cls, filters)

Expand Down
4 changes: 2 additions & 2 deletions graphemy/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ def __init__(
)

async def get_context(request: Request) -> dict:
context = context_getter(request) if context_getter else {}
context = await context_getter(request) if context_getter else {}
for n, f, m in functions:
context[n] = MyDataLoader(
load_fn=f
if Setup.get_permission(m, context)
if await Setup.get_permission(m, context)
else fake_dl_list
if type(inspect.signature(f).return_annotation)
== GenericAlias
Expand Down
6 changes: 3 additions & 3 deletions graphemy/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def setup(cls, engine, folder=None, get_perm=None):
cls.get_permission = get_perm
else:

def get_permission(module, context):
async def get_permission(module, context):
return True

cls.get_permission = get_permission

@classmethod
def get_auth(cls, module):
class IsAuthenticated(BasePermission):
def has_permission(self, source, info, **kwargs) -> bool:
if not cls.get_permission(module, info):
async def has_permission(self, source, info, **kwargs) -> bool:
if not await cls.get_permission(module, info.context):
info.context['response'].status_code = 403
if not 'errors' in info.context['request'].scope:
info.context['request'].scope['errors'] = [module]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "graphemy"
version = "0.1.3"
version = "0.1.4"
description = "A Python library for integrating SQLModel and Strawberry, providing a seamless GraphQL integration with FastAPI and advanced features for database interactions."
authors = ["Matheus Doreto <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def client():
from examples.tutorial.main import app, engine
from graphemy import Setup

def get_permission(module, context):
async def get_permission(module, context):
return 'account' in module

Setup.setup(engine=engine, get_perm=get_permission)
Expand Down

0 comments on commit 1c6ab62

Please sign in to comment.