diff options
Diffstat (limited to 'server/user_session.py')
-rw-r--r-- | server/user_session.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/server/user_session.py b/server/user_session.py index 55f4853..5618c90 100644 --- a/server/user_session.py +++ b/server/user_session.py @@ -7,8 +7,9 @@ import base64 import random import db import server -from errors.session import NoSuchSession, AuthenticationFailed, PasswordChangeFailed +from errors.session import NoSuchSession, AuthenticationFailed, PasswordChangeFailed, UserExists, SignupFailed import psycopg2.extras +import datetime __all__ = ['get_session_by_id', 'get_or_create_session', 'UserSession'] @@ -216,12 +217,14 @@ def get_or_create_session(uid, username, sid=None, lan=None): return s settings = {} if lan is not None: + #settings['lan'] or settings['lang'] ???? settings['lan'] = lan s = UserSession(uid, username, settings) sessions[s.sid] = s return s def authenticate_and_create_session(username, password): + now = datetime.datetime.now() conn = db.get_connection() try: cur = conn.cursor() @@ -231,6 +234,7 @@ def authenticate_and_create_session(username, password): if row is None: raise AuthenticationFailed('No such user: {}'.format(username)) if verify_password(password, row[1]): + cur.execute('update codeq_user set last_login = %s where id = %s', (str(now), row[0],)) return get_or_create_session(row[0], username, None, row[2]) raise AuthenticationFailed('Password mismatch') finally: @@ -240,6 +244,28 @@ def authenticate_and_create_session(username, password): db.return_connection(conn) +def signup(username, password): + now = datetime.datetime.now() + conn = db.get_connection() + try: + cur = conn.cursor() + try: + cur.execute('select id from codeq_user where username = %s', (username,)) + row = cur.fetchone() + if row: + raise UserExists('User exists') + else: + cur.execute('insert into codeq_user (username, password, name, email, is_admin, is_active, date_joined, last_login, gui_lang) values (%s, %s, %s, %s, %s, %s, %s, %s, %s) returning id', (username, encrypt_password(password),None ,'', False, True, str(now), str(now), None )) + affected = cur.rowcount + if affected is None: + raise SignupFailed('Signn up failed') + finally: + cur.close() + finally: + conn.commit() + db.return_connection(conn) + + def verify_password(plain_password, encrypted_password): elts = encrypted_password.split('$') if len(elts) != 4: |