From 639c307ccd557d43e46c6f5cfa913a41d5d53550 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 23 Feb 2014 18:36:40 -0500 Subject: [PATCH] Improve handing of session data Add functions to store data in an organized way so that multiple plugins can store data w/o stomping on each other. Signed-off-by: Simo Sorce --- ipsilon/util/user.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/ipsilon/util/user.py b/ipsilon/util/user.py index 4f7df91..dd4b002 100755 --- a/ipsilon/util/user.py +++ b/ipsilon/util/user.py @@ -89,6 +89,10 @@ class UserSession(object): def __init__(self): self.user = cherrypy.session.get('user', None) + def _debug(self, fact): + if cherrypy.config.get('debug', False): + cherrypy.log(fact) + def get_user(self): return User(self.user) @@ -100,8 +104,7 @@ class UserSession(object): if self.user == username: return - # REMOTE_USER changed, destroy old session and regenerate new - cherrypy.session.regenerate() + # REMOTE_USER changed, replace user cherrypy.session['user'] = username cherrypy.session.save() @@ -117,3 +120,29 @@ class UserSession(object): # Destroy current session in all cases cherrypy.lib.sessions.expire() + + def save_data(self, facility, name, data): + """ Save named data in the session so it can be retrieved later """ + if facility not in cherrypy.session: + cherrypy.session[facility] = dict() + cherrypy.session[facility][name] = data + cherrypy.session.save() + self._debug('Saved session data named [%s:%s]' % (facility, name)) + + def get_data(self, facility, name): + """ Get named data in the session if available """ + if facility not in cherrypy.session: + return None + if name not in cherrypy.session[facility]: + return None + return cherrypy.session[facility][name] + + def nuke_data(self, facility, name): + if facility not in cherrypy.session: + return + if name not in cherrypy.session[facility]: + return + cherrypy.session[facility][name] = None + del cherrypy.session[facility][name] + cherrypy.session.save() + self._debug('Nuked session data named [%s:%s]' % (facility, name)) -- 2.20.1