1
0
Fork 0
gnuviechadmin-historic/backend/GnuviechAdmin/SessionManager.py
Jan Dittberner 68431035d0 - use database data for login
git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@189 a67ec6bc-e5d5-0310-a910-815c51eb3124
2006-04-18 19:27:33 +00:00

90 lines
2.8 KiB
Python

"""
Session manager class for gnuviech-admin tool backend
(c) 2006 Jan Dittberner <jan@dittberner.info>
$Id$
"""
import Settings
import os, sha, time, logging, psycopg
from threading import Timer
SESSIONTIMEOUT=120 # 2 minutes
class InvalidLoginError(Exception):
"""
Exception class for invalid logins.
"""
pass
class InvalidSessionError(Exception):
"""
Exception class for invalid sessions.
"""
pass
class Session:
def __init__(self, id, login):
self.id = id
self.login = login
self._timeoutTimer = None
def settimeoutTimer(self, timeoutTimer):
self._timeoutTimer = timeoutTimer
self._timeoutTimer.start()
def gettimeoutTimer(self):
return self._timeoutTimer
class SessionManager:
def __init__(self, dbconn):
self._sessions = {}
self._dbconn = dbconn
self._hashobj = sha.new(str(time.time()))
self.logger = logging.getLogger('SessionManager')
def listSessions(self):
return self._sessions.keys()
def newSession(self, login, password):
cr = self._dbconn.cursor()
cr.execute('SELECT * FROM sysuser WHERE name=%(login)s AND md5pass=md5(%(password)s)' %
{'login': psycopg.QuotedString(login),
'password' : psycopg.QuotedString(password)})
self._dbconn.commit()
result = cr.fetchall()
if cr.rowcount == 1:
self._hashobj.update("%s,%s" % (time.time(), login))
sessionid = self._hashobj.hexdigest()
self._sessions[sessionid] = Session(sessionid, login)
self.updateSession(sessionid)
return sessionid
raise InvalidLoginError
def updateSession(self, sessionid):
self.logger.debug("update session %s" % sessionid)
try:
session = self.getSession(sessionid)
except InvalidSessionError, ev:
pass
else:
if session.gettimeoutTimer() is not None:
session.gettimeoutTimer().cancel()
session.settimeoutTimer(Timer(SESSIONTIMEOUT, self.deleteSession,
args=[sessionid]))
def getSession(self, sessionid):
if self._sessions.has_key(sessionid):
return self._sessions[sessionid]
raise InvalidSessionError()
def deleteSession(self, sessionid):
self.logger.debug("delete session %s" % sessionid)
try:
session = self.getSession(sessionid)
except InvalidSessionError:
print "invalid session"
else:
if session.gettimeoutTimer() is not None:
session.gettimeoutTimer().cancel()
del(self._sessions[sessionid])
self.logger.debug("%d sessions remaining" % len(self.listSessions()))