Mercurial > kallithea
changeset 5320:837fc99ca140
auth: have fill_data take User object, not lookup key
Simplify the UserModel.fill_data interface by passing the actual db.User
object instead of a key by which it can be looked up. This also saves a
lookup in case the db.User is already loaded (which for now is only the
case in one place, but that'll change in the following changesets).
Also enhance fill_data docstring.
author | Søren Løvborg <kwi@kwi.dk> |
---|---|
date | Sun, 26 Jul 2015 13:58:50 +0200 |
parents | 598d0d0c4190 |
children | b92c2e4324b0 |
files | kallithea/lib/auth.py kallithea/model/user.py |
diffstat | 2 files changed, 11 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/lib/auth.py Sun Jul 26 13:58:50 2015 +0200 +++ b/kallithea/lib/auth.py Sun Jul 26 13:58:50 2015 +0200 @@ -508,24 +508,24 @@ # lookup by userid if self.user_id is not None and self.user_id != self.anonymous_user.user_id: log.debug('Auth User lookup by USER ID %s' % self.user_id) - is_user_loaded = user_model.fill_data(self, user_id=self.user_id) + is_user_loaded = user_model.fill_data(self, user_model.get(self.user_id)) # try go get user by API key elif self._api_key and self._api_key != self.anonymous_user.api_key: log.debug('Auth User lookup by API key %s' % self._api_key) - is_user_loaded = user_model.fill_data(self, api_key=self._api_key) + is_user_loaded = user_model.fill_data(self, User.get_by_api_key(self._api_key)) # lookup by username elif self.username: log.debug('Auth User lookup by USER NAME %s' % self.username) - is_user_loaded = user_model.fill_data(self, username=self.username) + is_user_loaded = user_model.fill_data(self, User.get_by_username(self.username)) else: log.debug('No data in %s that could been used to log in' % self) if not is_user_loaded: # if we cannot authenticate user try anonymous if self.anonymous_user.active: - user_model.fill_data(self, user_id=self.anonymous_user.user_id) + user_model.fill_data(self, self.anonymous_user) # then we set this user is logged in self.is_authenticated = True else:
--- a/kallithea/model/user.py Sun Jul 26 13:58:50 2015 +0200 +++ b/kallithea/model/user.py Sun Jul 26 13:58:50 2015 +0200 @@ -322,27 +322,17 @@ return True - def fill_data(self, auth_user, user_id=None, api_key=None, username=None): + def fill_data(self, auth_user, dbuser): """ - Fetches auth_user by user_id, api_key or username, if present. - Fills auth_user attributes with those taken from database. + Copies database fields from a `db.User` to an `AuthUser`. Does + not copy `api_keys` and `permissions` attributes. + + Checks that `dbuser` is `active` (and not None) before copying; + returns True on success. :param auth_user: instance of user to set attributes - :param user_id: user id to fetch by - :param api_key: API key to fetch by - :param username: username to fetch by + :param dbuser: `db.User` instance to copy from """ - if user_id is None and api_key is None and username is None: - raise Exception('You need to pass user_id, api_key or username') - - dbuser = None - if user_id is not None: - dbuser = self.get(user_id) - elif api_key is not None: - dbuser = User.get_by_api_key(api_key) - elif username is not None: - dbuser = User.get_by_username(username) - if dbuser is not None and dbuser.active: log.debug('filling %s data' % dbuser) for k, v in dbuser.get_dict().iteritems():