From ccda3045338fecb7afe96773e809c8ff606eb275 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Mon, 13 Jun 2016 21:17:01 +0200 Subject: [PATCH] email for new users and users cache --- angular_app/login.html | 10 +++++++--- backend.py | 4 +++- eventman_server.py | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/angular_app/login.html b/angular_app/login.html index 960d86c..49e6107 100644 --- a/angular_app/login.html +++ b/angular_app/login.html @@ -7,7 +7,7 @@
- {{'Username' | translate}} + {{'Username or email' | translate}}
@@ -29,11 +29,15 @@
{{'Username' | translate}} - + +
+
+ {{'Email' | translate}} +
{{'Password' | translate}} - +
diff --git a/backend.py b/backend.py index 535117c..1c7e71b 100644 --- a/backend.py +++ b/backend.py @@ -131,7 +131,7 @@ class EventManDB(object): results = self.query(collection, convert({'_id': _id})) return results and results[0] or {} - def query(self, collection, query=None): + def query(self, collection, query=None, condition='or'): """Get multiple documents matching a query. :param collection: search for documents in this collection @@ -144,6 +144,8 @@ class EventManDB(object): """ db = self.connect() query = convert(query or {}) + if isinstance(query, (list, tuple)): + query = {'$%s' % condition: query} return list(db[collection].find(query)) def add(self, collection, data, _id=None): diff --git a/eventman_server.py b/eventman_server.py index 1dd1bb7..483ab17 100755 --- a/eventman_server.py +++ b/eventman_server.py @@ -92,6 +92,8 @@ class BaseHandler(tornado.web.RequestHandler): 'users|create': True } + _users_cache = {} + # A property to access the first value of each argument. arguments = property(lambda self: dict([(k, v[0]) for k, v in self.request.arguments.iteritems()])) @@ -161,6 +163,8 @@ class BaseHandler(tornado.web.RequestHandler): def current_user_info(self): """Information about the current user, including their permissions.""" current_user = self.current_user + if current_user in self._users_cache: + return self._users_cache[current_user] user_info = {'permissions': set([k for (k, v) in self.permissions.iteritems() if v is True])} if current_user: user_info['username'] = current_user @@ -169,6 +173,7 @@ class BaseHandler(tornado.web.RequestHandler): user = res[0] user_info['permissions'].update(set(user.get('permissions') or [])) user_info['permissions'] = list(user_info['permissions']) + self._users_cache[current_user] = user_info return user_info def has_permission(self, permission): @@ -199,6 +204,8 @@ class BaseHandler(tornado.web.RequestHandler): def logout(self): """Remove the secure cookie used fro authentication.""" + if self.current_user in self._users_cache: + del self._users_cache[self.current_user] self.clear_cookie("user") @@ -699,12 +706,14 @@ class UsersHandler(CollectionHandler): def filter_input_post_all(self, data): username = (data.get('username') or '').strip() password = (data.get('password') or '').strip() + email = (data.get('email') or '').strip() if not (username and password): raise InputException('missing username or password') res = self.db.query('users', {'username': username}) if res: raise InputException('username already exists') - return {'username': username, 'password': utils.hash_password(password)} + return {'username': username, 'password': utils.hash_password(password), + 'email': email, '_id': self.gen_id()} class EbCSVImportPersonsHandler(BaseHandler): @@ -852,9 +861,12 @@ class LoginHandler(BaseHandler): with open(self.angular_app_path + "/login.html", 'r') as fd: self.write(fd.read()) - def _authorize(self, username, password): + def _authorize(self, username, password, email=None): """Return True is this username/password is valid.""" - res = self.db.query('users', {'username': username}) + query = [{'username': username}] + if email is not None: + query.append({'email': email}) + res = self.db.query('users', query) if not res: return False user = res[0]