Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wojcikmat committed Mar 8, 2024
1 parent e8c9c0e commit d1a1c82
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
50 changes: 23 additions & 27 deletions packages/backend/apps/multitenancy/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
4 changes: 2 additions & 2 deletions packages/backend/apps/multitenancy/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit d1a1c82

Please sign in to comment.