-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_manager.py
88 lines (65 loc) · 2.7 KB
/
session_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from setuptools.package_index import Credential
__author__ = 'pde'
from models import User, Session
from flask import abort, jsonify
from database import db_session
from application_cache import ApplicationCache
import json
import datetime
class SessionManager():
def user_has_active_session(userid, workstation):
user = db_session.query(User).filter_by(id=userid).first()
if user is None:
return False
sessions = db_session.query(Session).filter_by(Session.iduser == user.id, Session.active == 1)
if len(list(sessions)) > 0:
return True
return False
def user_has_active_session_other_workstation(userid, workstation):
user = db_session.query(User).filter_by(id=userid).first()
if user is None:
return False
sessions = db_session.query(Session).filter_by(Session.iduser == user.id, Session.active == 1, Session.workstation != workstation)
if len(list(sessions)) > 0:
return True
return False
def get_user_active_sessions(userid):
return db_session.query(Session).filter(Session.iduser == userid, Session.active == 1)
def create_session(hash, workstation):
user = db_session.query(User).filter_by(hash=hash).first()
if user is None:
return None
current_sessions = SessionManager.get_user_active_sessions(user.id)
for session in current_sessions:
session.active = 0
db_session.commit()
new_session = Session(user.id)
db_session.add(new_session)
db_session.commit()
return new_session
def get_session(sessiontoken):
session = db_session.query(Session).filter_by(sessiontoken=sessiontoken)
if len(list(session)) == 0:
session = None
if session[0].active == 0 or session[0].logouttime is not None:
session = None
return session
def logout_session(session_token):
session = db_session.query(Session).filter_by(sessiontoken=session_token)
if len(list(session)) == 0:
session = None
if session.active == 0 or session.logouttime is not None:
session = None
session[0].logouttime = datetime.datetime.now()
session[0].active = 0
db_session.commit()
def create_user(credentials, hash):
credentialsObj = json.loads(credentials)
user = db_session.query(User).filter_by(email=credentialsObj['email'])
if len(list(user)) > 0:
return None
user = User(credentialsObj['email'],hash)
db_session.add(user)
db_session.commit()
session = SessionManager.create_session(hash)
return session