From 4c376fb2adde669f34c4b17e059e65a174629b54 Mon Sep 17 00:00:00 2001 From: Robert Zorko Date: Tue, 29 Sep 2015 15:31:42 +0200 Subject: added settings to the user session and the login function now also returns those settings - currently only gui language is implemented. The create.sql script is updated for that change as well. --- db/create.sql | 1 + server/handlers.py | 3 ++- server/user_session.py | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/db/create.sql b/db/create.sql index e720238..57c3647 100644 --- a/db/create.sql +++ b/db/create.sql @@ -33,6 +33,7 @@ create table codeq_user ( is_active bool not null, date_joined timestamp not null, last_login timestamp not null, + gui_lang varchar(2), constraint codeq_user_pk primary key (id), constraint codeq_user_uq1 unique (username) ); diff --git a/server/handlers.py b/server/handlers.py index 08994a0..997e2f2 100644 --- a/server/handlers.py +++ b/server/handlers.py @@ -50,7 +50,8 @@ class Login(CodeqService): else: if request.session: request.session.destroy() - request.reply({'code': 0, 'message': 'OK', 'sid':session.get_sid()}) + settings = session.get_settings() + request.reply({'code': 0, 'message': 'OK', 'sid':session.get_sid(), 'settings':settings}) class Activity(CodeqService): diff --git a/server/user_session.py b/server/user_session.py index 00ea039..6d5af47 100644 --- a/server/user_session.py +++ b/server/user_session.py @@ -26,13 +26,14 @@ class UserSession(object): No properties are accessible; use getters and setters instead. Values are passed by value instead of by reference (deep copy!). """ - def __init__(self, uid, username): + def __init__(self, uid, username, settings): self._access_lock = threading.Lock() self.sid = uuid.uuid4().hex self.uid = uid self.username = username self.prolog_session = None self.python_session = None + self.settings = settings def destroy(self): """Destroys the session.""" @@ -53,6 +54,9 @@ class UserSession(object): def get_uid(self): return self.uid + def get_settings(self): + return self.settings + def get_prolog(self): with self._access_lock: if self.prolog_session is None: @@ -159,13 +163,16 @@ def get_session_by_id(sid): raise NoSuchSession('There is no session with SID {}'.format(sid)) return s -def get_or_create_session(uid, username, sid=None): +def get_or_create_session(uid, username, sid=None, lan=None): with module_access_lock: if sid is not None: s = sessions.get(sid) if s is not None: return s - s = UserSession(uid, username) + settings = {} + if lan is not None: + settings['lan'] = lan + s = UserSession(uid, username, settings) sessions[s.sid] = s return s @@ -174,12 +181,12 @@ def authenticate_and_create_session(username, password): try: cur = conn.cursor() try: - cur.execute('select id, password from codeq_user where username = %s', (username,)) + cur.execute('select id, password, gui_lang from codeq_user where username = %s', (username,)) row = cur.fetchone() if row is None: raise AuthenticationFailed('No such user: {}'.format(username)) if verify_password(password, row[1]): - return get_or_create_session(row[0], username) + return get_or_create_session(row[0], username, None, row[2]) raise AuthenticationFailed('Password mismatch') finally: cur.close() -- cgit v1.2.1