From c3299ff0e648afce295c3140d12a5145b689b816 Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Mon, 21 Mar 2016 12:38:28 +0200 Subject: [PATCH] Fix: event reporting breaks when using ApiUser. --- redash/authentication/__init__.py | 4 ++-- redash/handlers/base.py | 2 +- redash/handlers/query_results.py | 2 +- redash/models.py | 9 +++++---- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/redash/authentication/__init__.py b/redash/authentication/__init__.py index 4787732c..fe2be323 100644 --- a/redash/authentication/__init__.py +++ b/redash/authentication/__init__.py @@ -62,7 +62,7 @@ def hmac_load_user_from_request(request): calculated_signature = sign(query.api_key, request.path, expires) if query.api_key and signature == calculated_signature: - return models.ApiUser(query.api_key, query.org, query.groups.keys()) + return models.ApiUser(query.api_key, query.org, query.groups.keys(), name="ApiKey: Query {}".format(query.id)) return None @@ -84,7 +84,7 @@ def get_user_from_api_key(api_key, query_id): if query_id: query = models.Query.get_by_id_and_org(query_id, current_org.id) if query and query.api_key == api_key: - user = models.ApiUser(api_key, query.org, query.groups.keys()) + user = models.ApiUser(api_key, query.org, query.groups.keys(), name="ApiKey: Query {}".format(query.id)) return user diff --git a/redash/handlers/base.py b/redash/handlers/base.py index 36afb555..5ffe08ae 100644 --- a/redash/handlers/base.py +++ b/redash/handlers/base.py @@ -35,7 +35,7 @@ class BaseResource(Resource): def record_event(self, options): if isinstance(self.current_user, ApiUser): options.update({ - 'api_key': self.current_user.id, + 'api_key': self.current_user.name, 'org_id': self.current_org.id }) else: diff --git a/redash/handlers/query_results.py b/redash/handlers/query_results.py index 711c2174..772cd04d 100644 --- a/redash/handlers/query_results.py +++ b/redash/handlers/query_results.py @@ -114,7 +114,7 @@ class QueryResultResource(BaseResource): 'org_id': self.current_org.id, 'action': 'api_get', 'timestamp': int(time.time()), - 'api_key': self.current_user.id, + 'api_key': self.current_user.name, 'file_type': filetype, 'user_agent': request.user_agent.string, 'ip': request.remote_addr diff --git a/redash/models.py b/redash/models.py index 3d4a6138..3d0d459e 100644 --- a/redash/models.py +++ b/redash/models.py @@ -145,19 +145,20 @@ class AnonymousUser(AnonymousUserMixin, PermissionsCheckMixin): class ApiUser(UserMixin, PermissionsCheckMixin): - def __init__(self, api_key, org, groups): + def __init__(self, api_key, org, groups, name=None): self.object = None if isinstance(api_key, basestring): self.id = api_key + self.name = name else: self.id = api_key.api_key + self.name = "ApiKey: {}".format(api_key.id) self.object = api_key.object - # self.name = self.groups = groups self.org = org def __repr__(self): - return u"".format(self.id) + return u"<{}>".format(self.name) @property def permissions(self): @@ -315,7 +316,7 @@ class User(ModelTimestampsMixin, BaseModel, BelongsToOrgMixin, UserMixin, Permis @classmethod def all(cls, org): return cls.select().where(cls.org == org) - + @classmethod def find_by_email(cls, email): return cls.select().where(cls.email == email)