From 2768eb14d8042206c47bdabdcc8ae14efe4e080b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Pu=C5=A1nik?= Date: Tue, 6 Oct 2015 02:30:58 +0200 Subject: sign up, update last_login --- errors/session.py | 6 ++++++ server/handlers.py | 23 +++++++++++++++++++++++ server/user_session.py | 28 +++++++++++++++++++++++++++- web/main.js | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/errors/session.py b/errors/session.py index f58f2af..06916c3 100644 --- a/errors/session.py +++ b/errors/session.py @@ -6,6 +6,12 @@ class NoSuchSession(Exception): class AuthenticationFailed(Exception): pass +class UserExists(Exception): + pass + +class SignupFailed(Exception): + pass + class PasswordChangeFailed(Exception): pass diff --git a/server/handlers.py b/server/handlers.py index a7f4f76..a7e0fef 100644 --- a/server/handlers.py +++ b/server/handlers.py @@ -55,6 +55,28 @@ class Login(CodeqService): request.reply({'code': 0, 'message': 'OK', 'sid':session.get_sid(), 'settings':settings}) +class Signup(CodeqService): + session_is_optional = True + + def process(self, request): + js = request.data + username = js.get('username') + password = js.get('password') + if username is None: + request.reply({'code': 1, 'message': 'Username was not provided'}) + elif password is None: + request.reply({'code': 2, 'message': 'Password was not provided'}) + else: + try: + server.user_session.signup(username, password) + except UserExists: + request.reply({'code': 3, 'message': 'Username already exists'}) + except SignupFailed: + request.reply({'code': 4, 'message': 'Signn up failed'}) + else: + request.reply({'code': 0, 'message': 'OK'}) + + class ChangePassword(CodeqService): def process(self, request): js = request.data @@ -260,6 +282,7 @@ class EndProblem(CodeqService): incoming_handlers = { 'list_problems': ProblemList(), 'login': Login(), + 'signup': Signup(), 'change_password': ChangePassword(), 'get_problem': GetProblem(), 'logout': None, 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: diff --git a/web/main.js b/web/main.js index 8d5446b..a4dbf08 100644 --- a/web/main.js +++ b/web/main.js @@ -109,6 +109,7 @@ var guiHandlers = { // actions to use default handling should define truthy values that are not functions // (this is to filter out unnecessary traffic before it hits Python) + 'signup': true, 'change_password': true, 'activity': true, 'query': true, -- cgit v1.2.1