core (memory): added optional profile checking in Sessions:
if profile is set in newSession, getProfile must be used instead of __getitem__, and it is checked to insure session is not used by the wrong profile
This commit is contained in:
parent
1550d06922
commit
3f236bbb32
1 changed files with 18 additions and 3 deletions
|
|
@ -50,16 +50,18 @@ class Sessions(object):
|
|||
self._sessions = dict()
|
||||
self.timeout = timeout or Sessions.DEFAULT_TIMEOUT
|
||||
|
||||
def newSession(self, session_data=None):
|
||||
def newSession(self, session_data=None, profile=None):
|
||||
""" Create a new session
|
||||
@param session_data: mutable data to use, default to a dict
|
||||
@param profile: if set, the session is owned by the profile,
|
||||
and profileGet must be used instead of __getitem__
|
||||
@return: session_id, session_data
|
||||
"""
|
||||
session_id = str(uuid4())
|
||||
timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
|
||||
if session_data is None:
|
||||
session_data = {}
|
||||
self._sessions[session_id] = (timer, session_data)
|
||||
self._sessions[session_id] = (timer, session_data) if profile is None else (timer, session_data, profile)
|
||||
return session_id, session_data
|
||||
|
||||
def _purgeSession(self, session_id):
|
||||
|
|
@ -72,8 +74,21 @@ class Sessions(object):
|
|||
def __contains__(self, session_id):
|
||||
return session_id in self._sessions
|
||||
|
||||
def profileGet(self, session_id, profile):
|
||||
try:
|
||||
timer, session_data, profile_set = self._sessions[session_id]
|
||||
except ValueError:
|
||||
raise exceptions.InternalError("You need to use __getitem__ when profile is not set")
|
||||
if profile_set != profile:
|
||||
raise exceptions.InternalError("current profile differ from set profile !")
|
||||
timer.reset(self.timeout)
|
||||
return session_data
|
||||
|
||||
def __getitem__(self, session_id):
|
||||
timer, session_data = self._sessions[session_id]
|
||||
try:
|
||||
timer, session_data = self._sessions[session_id]
|
||||
except ValueError:
|
||||
raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set")
|
||||
timer.reset(self.timeout)
|
||||
return session_data
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue