email for new users and users cache

This commit is contained in:
Davide Alberani 2016-06-13 21:17:01 +02:00
parent 42ad0756de
commit ccda304533
3 changed files with 25 additions and 7 deletions

View file

@ -7,7 +7,7 @@
<div class="panel-body"> <div class="panel-body">
<form method="POST"> <form method="POST">
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<span class="input-group-addon min150">{{'Username' | translate}}</span> <span class="input-group-addon min150">{{'Username or email' | translate}}</span>
<input type="text" id="username" name="username" ng-model="loginData.username" class="form-control"> <input type="text" id="username" name="username" ng-model="loginData.username" class="form-control">
</div> </div>
<div class="input-group input-group-lg top10"> <div class="input-group input-group-lg top10">
@ -29,11 +29,15 @@
<form method="POST"> <form method="POST">
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<span class="input-group-addon min150">{{'Username' | translate}}</span> <span class="input-group-addon min150">{{'Username' | translate}}</span>
<input type="text" id="username" name="username" ng-model="newUser.username" class="form-control"> <input type="text" id="new-username" name="new-username" ng-model="newUser.username" class="form-control" ng-required="1">
</div>
<div class="input-group input-group-lg top10">
<span class="input-group-addon min150">{{'Email' | translate}}</span>
<input type="email" id="new-email" name="new-email" ng-model="newUser.email" class="form-control">
</div> </div>
<div class="input-group input-group-lg top10"> <div class="input-group input-group-lg top10">
<span class="input-group-addon min150">{{'Password' | translate}}</span> <span class="input-group-addon min150">{{'Password' | translate}}</span>
<input type="password" id="password" name="password" ng-model="newUser.password" class="form-control"> <input type="password" id="new-password" name="new-password" ng-model="newUser.password" class="form-control" ng-required="1">
</div> </div>
<button type="submit" ng-click="register()" class="btn btn-success top10">{{'register' | translate}}</button> <button type="submit" ng-click="register()" class="btn btn-success top10">{{'register' | translate}}</button>
</form> </form>

View file

@ -131,7 +131,7 @@ class EventManDB(object):
results = self.query(collection, convert({'_id': _id})) results = self.query(collection, convert({'_id': _id}))
return results and results[0] or {} 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. """Get multiple documents matching a query.
:param collection: search for documents in this collection :param collection: search for documents in this collection
@ -144,6 +144,8 @@ class EventManDB(object):
""" """
db = self.connect() db = self.connect()
query = convert(query or {}) query = convert(query or {})
if isinstance(query, (list, tuple)):
query = {'$%s' % condition: query}
return list(db[collection].find(query)) return list(db[collection].find(query))
def add(self, collection, data, _id=None): def add(self, collection, data, _id=None):

View file

@ -92,6 +92,8 @@ class BaseHandler(tornado.web.RequestHandler):
'users|create': True 'users|create': True
} }
_users_cache = {}
# A property to access the first value of each argument. # A property to access the first value of each argument.
arguments = property(lambda self: dict([(k, v[0]) arguments = property(lambda self: dict([(k, v[0])
for k, v in self.request.arguments.iteritems()])) for k, v in self.request.arguments.iteritems()]))
@ -161,6 +163,8 @@ class BaseHandler(tornado.web.RequestHandler):
def current_user_info(self): def current_user_info(self):
"""Information about the current user, including their permissions.""" """Information about the current user, including their permissions."""
current_user = self.current_user 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])} user_info = {'permissions': set([k for (k, v) in self.permissions.iteritems() if v is True])}
if current_user: if current_user:
user_info['username'] = current_user user_info['username'] = current_user
@ -169,6 +173,7 @@ class BaseHandler(tornado.web.RequestHandler):
user = res[0] user = res[0]
user_info['permissions'].update(set(user.get('permissions') or [])) user_info['permissions'].update(set(user.get('permissions') or []))
user_info['permissions'] = list(user_info['permissions']) user_info['permissions'] = list(user_info['permissions'])
self._users_cache[current_user] = user_info
return user_info return user_info
def has_permission(self, permission): def has_permission(self, permission):
@ -199,6 +204,8 @@ class BaseHandler(tornado.web.RequestHandler):
def logout(self): def logout(self):
"""Remove the secure cookie used fro authentication.""" """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") self.clear_cookie("user")
@ -699,12 +706,14 @@ class UsersHandler(CollectionHandler):
def filter_input_post_all(self, data): def filter_input_post_all(self, data):
username = (data.get('username') or '').strip() username = (data.get('username') or '').strip()
password = (data.get('password') or '').strip() password = (data.get('password') or '').strip()
email = (data.get('email') or '').strip()
if not (username and password): if not (username and password):
raise InputException('missing username or password') raise InputException('missing username or password')
res = self.db.query('users', {'username': username}) res = self.db.query('users', {'username': username})
if res: if res:
raise InputException('username already exists') 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): class EbCSVImportPersonsHandler(BaseHandler):
@ -852,9 +861,12 @@ class LoginHandler(BaseHandler):
with open(self.angular_app_path + "/login.html", 'r') as fd: with open(self.angular_app_path + "/login.html", 'r') as fd:
self.write(fd.read()) self.write(fd.read())
def _authorize(self, username, password): def _authorize(self, username, password, email=None):
"""Return True is this username/password is valid.""" """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: if not res:
return False return False
user = res[0] user = res[0]