{{ current_user_email() }} doesn't work
Bug description
After enabling ENABLE_TEMPLATE_PROCESSING=True It doesn't work with "{{ current_user_email() }}"
It works well with "{{ current_user_id() }}" and "{{ current_username() }}"
I have tried 3 version: latest, 4.0.1 and 3.13 , all has same issue!
How to reproduce the bug
- Go to SQL Lab
- Run this query: SELECT "{{ current_user_email() }}"
Screenshots/recordings
Superset version
master / latest-dev
Python version
3.9
Node version
16
Browser
Chrome
Additional context
No response
Checklist
- [X] I have searched Superset docs and Slack and didn't find a solution to my problem.
- [X] I have searched the GitHub issue tracker and didn't find a similar bug report.
- [X] I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.
The issue with {{ current_user_email() }} not working after enabling ENABLE_TEMPLATE_PROCESSING=True is likely due to the current_user_email function not being properly defined within the ExtraCache class. Here is the corrected code to ensure it is properly defined:
class ExtraCache:
# ... other methods ...
def current_user_email(self, add_to_cache_keys: bool = True) -> Optional[str]:
"""
Return the email address of the user who is currently logged in.
:param add_to_cache_keys: Whether the value should be included in the cache key
:returns: The user email address
"""
if email_address := get_user_email():
if add_to_cache_keys:
self.cache_key_wrapper(email_address)
return email_address
return None
# ... other methods ...
Ensure that ENABLE_TEMPLATE_PROCESSING is set to True in your configuration:
DEFAULT_FEATURE_FLAGS = {
# ... other feature flags ...
"ENABLE_TEMPLATE_PROCESSING": True,
# ... other feature flags ...
}
If the issue persists, you can use an alternative method to retrieve the current user's email address:
from flask import g
from superset.models.core import User
def get_current_user_email():
user = g.user if hasattr(g, "user") and g.user else None
if user and not user.is_anonymous:
return user.email
return None
# Usage
email = get_current_user_email()
print(email)
Add this function to the Jinja context by defining the JINJA_CONTEXT_ADDONS in your superset_config.py:
JINJA_CONTEXT_ADDONS = {
'get_current_user_email': get_current_user_email,
}
This way, you can use {{ get_current_user_email() }} in your SQL queries to retrieve the current user's email address.
For further troubleshooting, ensure that all required template parameters are provided and correctly formatted, check for syntax errors in your SQL query, and review specific error messages returned by Superset.
Details
Sources
- superset/jinja_context.py
- superset/config.py
- tests/unit_tests/jinja_context_test.py
- superset/errors.py
- superset/sqllab/query_render.py
- superset/tasks/utils.py
- docs/docs/configuration/sql-templating.mdx
- CHANGELOG/2.0.1.md
About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
To continue the conversation, mention @dosu.
Hey @nhat2008 have you managed to find a workaround? I encounter the same bug on version 3.0.2
hi @ChernakovEgor , yes, i found a workaround way to solve it As the {{ current_user_email() }} is not in the latest released yet, its only available in the master branch , to use it, you may need to override 2 files in your version with the master one: superset/jinja_context.py, superset/utils/core.py
Do you know how to override it if I'm running in kube and only can change values.yaml ?
My helm chart version is 0.12.11 , superset version 4.0.1 and I see current_user_email() in github for that chart tag, but not in the files in my kube pod. I checked the latest docker image with 4.0.2 - it doesn't have current_user_email()