[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.
This commit is contained in:
Maxime Beauchemin 2017-10-10 17:52:22 -07:00 committed by GitHub
parent 6cc6637454
commit 76f8d33d81
1 changed files with 6 additions and 1 deletions

View File

@ -598,7 +598,12 @@ class Database(Model, AuditMixinNullable):
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):