ultimate_tictactoe_server/session_manager.py

95 lines
4.3 KiB
Python
Raw Permalink Normal View History

2019-03-14 00:01:02 +01:00
from database_connection import DatabaseConnection, SQLInjectionError, get_sql_time
from user import User
import datetime
import uuid
2019-04-01 16:13:44 +02:00
from tools import debug
2019-03-14 00:01:02 +01:00
class SessionManager(object):
2019-04-04 13:44:48 +02:00
def __init__(self, session_lifespan_timedelta, temp_session_lifespan_timedelta):
2019-03-14 00:01:02 +01:00
self.session_lifespan_timedelta = session_lifespan_timedelta
2019-04-04 13:44:48 +02:00
self.temp_session_lifespan_timedelta = temp_session_lifespan_timedelta
2019-03-14 00:01:02 +01:00
def get_session_by_id(self, session_id):
2019-03-27 17:23:04 +01:00
query = "SELECT * FROM sessions WHERE id=%s"
return DatabaseConnection.global_single_query(query, (session_id))
2019-03-14 00:01:02 +01:00
def touch_session(self, session_id):
2019-03-27 17:23:04 +01:00
query = "UPDATE sessions SET last_seen=%s WHERE id=%s"
2019-04-04 13:44:48 +02:00
DatabaseConnection.global_single_execution(
query, (datetime.datetime.now(), session_id))
2019-03-14 00:01:02 +01:00
def get_session_by_registered_user(self, user_name):
2019-03-27 17:23:04 +01:00
query = "SELECT * FROM sessions WHERE registered_user=%s"
return DatabaseConnection.global_single_query(query, (user_name))
2019-03-14 00:01:02 +01:00
def get_session_by_temp_user(self, user_name):
2019-03-27 17:23:04 +01:00
query = "SELECT * FROM sessions WHERE temp_user=%s"
return DatabaseConnection.global_single_query(query, (user_name))
2019-03-14 00:01:02 +01:00
def create_session_for_registered_user(self, user_name):
new_id = uuid.uuid4().hex
# check if already existent (but should not be the case)
2019-03-27 17:23:04 +01:00
if len(DatabaseConnection.global_single_query("SELECT id FROM sessions WHERE id=%s", (str(new_id)))) > 0:
2019-03-14 00:01:02 +01:00
# okay, next try:
2019-03-21 12:06:22 +01:00
return self.create_session_for_registered_user(user_name)
2019-03-27 17:23:04 +01:00
2019-03-21 12:06:22 +01:00
# delete other active sessions:
2019-03-27 17:23:04 +01:00
query = "DELETE FROM sessions WHERE registered_user=%s"
DatabaseConnection.global_single_execution(query, (user_name))
query = "INSERT INTO sessions (id, registered_user, temp_user, last_seen) VALUES ( %s, %s, NULL, %s)"
2019-04-04 13:44:48 +02:00
DatabaseConnection.global_single_execution(
query, (new_id, user_name, datetime.datetime.now()))
2019-03-14 00:01:02 +01:00
return new_id
def create_session_for_temp_user(self, user_name):
new_id = uuid.uuid4().hex
# check if already existent (but should not be the case)
2019-03-27 17:23:04 +01:00
if len(DatabaseConnection.global_single_query("SELECT id FROM sessions WHERE id=%s", (new_id))) > 0:
2019-03-14 00:01:02 +01:00
# okay, next try:
2019-03-21 12:06:22 +01:00
return self.create_session_for_registered_user(user_name)
2019-03-27 17:23:04 +01:00
2019-03-21 12:06:22 +01:00
# delete other active sessions:
2019-03-27 17:23:04 +01:00
query = "DELETE FROM sessions WHERE temp_user=%s"
DatabaseConnection.global_single_execution(query, (user_name))
query = "INSERT INTO sessions (id, registered_user, temp_user, last_seen) VALUES ( %s, NULL, %s, %s)"
2019-04-04 13:44:48 +02:00
DatabaseConnection.global_single_execution(
query, (new_id, user_name, datetime.datetime.now()))
2019-03-14 00:01:02 +01:00
return new_id
def delete_session(self, session_id):
2019-03-27 17:23:04 +01:00
query = "DELETE FROM sessions WHERE id=%s"
DatabaseConnection.global_single_execution(query, (session_id))
2019-03-14 00:01:02 +01:00
2019-04-04 13:44:48 +02:00
def revoke_inactive_temporary_sessions(self):
revoke_time = datetime.datetime.now() - self.temp_session_lifespan_timedelta
query = "SELECT * from sessions WHERE last_seen < %s AND temp_user IS NOT NULL"
revoked_temp_sessions = DatabaseConnection.global_single_query(
query, (get_sql_time(revoke_time)))
query = "DELETE FROM sessions WHERE last_seen < %s AND temp_user IS NOT NULL"
DatabaseConnection.global_single_execution(
query, (get_sql_time(revoke_time)))
for session in revoked_temp_sessions:
# remove from matches:
query = "DELETE FROM matches WHERE user_a=%s OR user_b=%s"
DatabaseConnection.global_single_execution(
query, (session['temp_user'], session['temp_user']))
debug("delete revoked sessions: " + str(revoked_temp_sessions))
2019-03-14 00:01:02 +01:00
def revoke_inactive_sessions(self):
revoke_time = datetime.datetime.now() - self.session_lifespan_timedelta
2019-03-27 17:23:04 +01:00
query = "SELECT * from sessions WHERE last_seen < %s"
2019-04-04 13:44:48 +02:00
revoked_sessions = DatabaseConnection.global_single_query(
query, (get_sql_time(revoke_time)))
2019-03-27 17:23:04 +01:00
query = "DELETE FROM sessions WHERE last_seen < %s"
2019-04-04 13:44:48 +02:00
DatabaseConnection.global_single_execution(
query, (get_sql_time(revoke_time)))
2019-04-01 16:13:44 +02:00
debug("delete revoked sessions: " + str(revoked_sessions))