From d1a1c827a84bd813d20a97017dc52c16b1f8c01a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20W=C3=B3jcik?= Date: Fri, 8 Mar 2024 09:25:19 +0100 Subject: [PATCH] PR fixes --- .../backend/apps/multitenancy/middleware.py | 50 +++++++++---------- packages/backend/apps/multitenancy/tokens.py | 4 +- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/packages/backend/apps/multitenancy/middleware.py b/packages/backend/apps/multitenancy/middleware.py index aa294b15e..4ee46ab89 100644 --- a/packages/backend/apps/multitenancy/middleware.py +++ b/packages/backend/apps/multitenancy/middleware.py @@ -40,32 +40,6 @@ def get_current_user_role(tenant, user): return None -def get_tenant_id_from_arguments(args): - """ - Extract the tenant ID from GraphQL arguments. - - Args: - args (dict): GraphQL arguments. - - Returns: - str or None: The extracted tenant ID or None if not found. - """ - request_input = args.get("input") - if request_input: - tenant_id = request_input.get("tenant_id") - if not tenant_id: - # for the purpose of Tenant CRUD actions - tenant_id = request_input.get("id") - else: - # for the purpose of queries, where is no input, just parameters - tenant_id = args.get("id") - if tenant_id: - id_type, pk = from_global_id(tenant_id) - if id_type == "TenantType": - return pk - return None - - class TenantUserRoleMiddleware(object): """ Middleware for resolving the current tenant and user role lazily. @@ -74,10 +48,32 @@ class TenantUserRoleMiddleware(object): The actual retrieval of the current tenant and user role is deferred until the values are accessed. Lazy loading is employed to optimize performance by loading these values only when necessary. """ + @staticmethod + def _get_tenant_id_from_arguments(args): + """ + Extract the tenant ID from GraphQL arguments. + + Args: + args (dict): GraphQL arguments. + + Returns: + str or None: The extracted tenant ID or None if not found. + """ + request_input = args.get("input") + if request_input: + tenant_id = request_input.get("tenant_id") or request_input.get("id") + else: + # for the purpose of queries, where is no input, just parameters + tenant_id = args.get("id") + if tenant_id: + id_type, pk = from_global_id(tenant_id) + if id_type == "TenantType": + return pk + return None def resolve(self, next, root, info, **args): if not hasattr(info.context, "tenant_id"): - info.context.tenant_id = get_tenant_id_from_arguments(args) + info.context.tenant_id = self._get_tenant_id_from_arguments(args) info.context.tenant = SimpleLazyObject(lambda: get_current_tenant(info.context.tenant_id)) info.context.user_role = SimpleLazyObject(lambda: get_current_user_role(info.context.tenant, info.context.user)) return next(root, info, **args) diff --git a/packages/backend/apps/multitenancy/tokens.py b/packages/backend/apps/multitenancy/tokens.py index e50a8dcd8..4e84fa8e9 100644 --- a/packages/backend/apps/multitenancy/tokens.py +++ b/packages/backend/apps/multitenancy/tokens.py @@ -27,7 +27,7 @@ def _make_token_with_timestamp(self, user_email, timestamp, tenant_membership_pk def make_token(self, user_email, tenant_membership): """ - Return a token that can be used once to do a password reset + Return a token that can be used once to make action on tenant invitation for the given user. """ return self._make_token_with_timestamp( @@ -46,7 +46,7 @@ def _now(self): def check_token(self, user_email, token, tenant_membership): """ - Check that a password reset token is correct for a given user. + Check that a tenant invitation token is correct for a given user. """ if not (user_email and token): return False