superset icon indicating copy to clipboard operation
superset copied to clipboard

{{ current_user_email() }} doesn't work

Open nhat2008 opened this issue 1 year ago • 1 comments

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

  1. Go to SQL Lab
  2. Run this query: SELECT "{{ current_user_email() }}"

Screenshots/recordings

Current_user

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.

nhat2008 avatar May 23 '24 14:05 nhat2008

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


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.

dosubot[bot] avatar May 23 '24 14:05 dosubot[bot]

Hey @nhat2008 have you managed to find a workaround? I encounter the same bug on version 3.0.2

ChernakovEgor avatar Jun 03 '24 08:06 ChernakovEgor

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

nhat2008 avatar Jun 07 '24 09:06 nhat2008

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()

kraftaa avatar Aug 28 '24 19:08 kraftaa