From 76f8d33d81a7bec284d9cb95afaf397161e8c22b Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Tue, 10 Oct 2017 17:52:22 -0700 Subject: [PATCH] [sql lab] fix impersonation + template issue (#3644) When the database impersonation flag is on, a query using a template fails. It has to do with templating using a database connection without a username being specified by the caller, along with the fact that the work is taking place on a worker, outside a web request, where referencing g.user raises this exception. --- superset/models/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/superset/models/core.py b/superset/models/core.py index b8293b59d34f5..7392e8796a571 100644 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -598,7 +598,12 @@ def get_sqla_engine(self, schema=None, nullpool=False, user_name=None): params['poolclass'] = NullPool uri = self.db_engine_spec.adjust_database_uri(uri, schema) if self.impersonate_user: - uri.username = user_name if user_name else g.user.username + eff_username = uri.username + if user_name: + eff_username = user_name + elif hasattr(g, 'user') and g.user.username: + eff_username = g.user.username + uri.username = eff_username return create_engine(uri, **params) def get_reserved_words(self):