From 1c6ab627a875b66dceaf72b075f03997fd4670c2 Mon Sep 17 00:00:00 2001 From: Matheus Doreto Date: Thu, 16 Nov 2023 09:09:19 -0300 Subject: [PATCH] :bug: fix: auth permissions --- graphemy/models.py | 2 +- graphemy/router.py | 4 ++-- graphemy/setup.py | 6 +++--- pyproject.toml | 2 +- tests/test_permission.py | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/graphemy/models.py b/graphemy/models.py index adb339e..5ced722 100644 --- a/graphemy/models.py +++ b/graphemy/models.py @@ -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) diff --git a/graphemy/router.py b/graphemy/router.py index bffb51b..c321a53 100644 --- a/graphemy/router.py +++ b/graphemy/router.py @@ -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 diff --git a/graphemy/setup.py b/graphemy/setup.py index 0d7fac1..7755ffe 100644 --- a/graphemy/setup.py +++ b/graphemy/setup.py @@ -18,7 +18,7 @@ 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 @@ -26,8 +26,8 @@ def get_permission(module, context): @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] diff --git a/pyproject.toml b/pyproject.toml index d71ed96..2048fe9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] readme = "README.md" diff --git a/tests/test_permission.py b/tests/test_permission.py index eec2876..4fdc594 100644 --- a/tests/test_permission.py +++ b/tests/test_permission.py @@ -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)