Yash2412 commented on code in PR #33924:
URL: https://github.com/apache/superset/pull/33924#discussion_r2192009929
##########
superset/jinja_context.py:
##########
@@ -284,6 +285,56 @@ def url_param(
self.cache_key_wrapper(result)
return result
+ def get_guest_user_attribute(
+ self,
+ attribute_name: str,
+ default: str | None = None,
+ add_to_cache_keys: bool = True,
+ ) -> str | None:
+ """
+ Get a specific user attribute from guest user.
+
+ Args:
+ attribute_name: Name of the attribute to retrieve
+ default: Default value if attribute not found
+ add_to_cache_keys: Whether the value should be included in the
cache key
+
+ Returns:
+ Attribute value or default
+ """
+
+ # Check if we have a request context and user
+ if not has_request_context():
+ return default
+
+ if not hasattr(g, "user") or g.user is None:
+ return default
+
+ user = g.user
+
+ # Check if current user is a guest user
+ if not (hasattr(user, "is_guest_user") and user.is_guest_user):
+ return default
+
+ # Get attributes from guest token
+ if hasattr(user, "guest_token") and user.guest_token:
+ token = user.guest_token
+ token_user = token.get("user", {})
+ user_attributes = token_user.get("attributes") or {}
+
+ # Only add to cache key if the variable actually exists in guest
token
+ if attribute_name in user_attributes:
+ result = user_attributes[attribute_name]
+ if add_to_cache_keys and result is not None:
+ self.cache_key_wrapper(
Review Comment:
Great question! I can confirm the cache key handling is safe:
- **Per-query scoped**: Each query execution creates a fresh `ExtraCache`
instance with a new `extra_cache_keys` list
- **No session persistence**: The cache keys are collected during template
processing and returned with the query results, then garbage collected
- **Multiple calls safe**: Multiple calls to `get_guest_user_attribute()`
within the same query template correctly accumulate in the same list, ensuring
the cache key reflects all used attributes
- **Automatic cleanup**: No manual cleanup needed - when the query
completes, the instance is destroyed
The cache keys are used to ensure query results are properly invalidated
when guest user attributes change, which is exactly what we want for
correctness.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]