Vitor-Avila commented on code in PR #33924:
URL: https://github.com/apache/superset/pull/33924#discussion_r2835789746
##########
superset/jinja_context.py:
##########
@@ -300,6 +306,75 @@ def url_param(
self.cache_key_wrapper(result)
return result
+ def get_guest_user_attribute(
+ self,
+ attribute_name: str,
+ default: JsonValue = None,
+ add_to_cache_keys: bool = True,
+ ) -> JsonValue:
+ """
+ Get a specific user attribute from guest user.
+
+ This function retrieves attributes from the guest user token and
supports
+ all JSON-native types (string, number, boolean, array, object, null).
+
+ Args:
+ attribute_name: Name of the attribute to retrieve
+ default: Default value if attribute not found (can be any
JSON-native type)
+ add_to_cache_keys: Whether the value should be included in the
cache key
+
+ Returns:
+ The attribute value from the guest user token, or the default
value.
+ Can be any JSON-native type: string, number, boolean, array,
object, or
+ null.
+
+ Examples:
+ {{ get_guest_user_attribute('department') }} # Returns:
"Engineering"
+ {{ get_guest_user_attribute('is_admin') }} # Returns: True
+ {{ get_guest_user_attribute('permissions') }} # Returns: ["read",
"write"]
+ {{ get_guest_user_attribute('config') }} # Returns: {"theme":
"dark"}
+ {{ get_guest_user_attribute('missing', 'default') }} # Returns:
"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
+ # ensure token is a mapping before calling .get
+ if not isinstance(token, dict):
+ return default
+ token_user = token.get("user", {})
Review Comment:
Also, once `is_guest_user()` returned `True`, I think it's fine to just
access `guest_token` directly (like we do
[here](https://github.com/apache/superset/blob/master/superset/dashboards/api.py#L1446-L1450)),
and then just `user_attributes = token.get("user", {}).get("attributes", {})`
--
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]