| 26 | | # this function gets all active sessions based on the path |
|---|
| 27 | | def _getSessions(): |
|---|
| 28 | | sessions = [] |
|---|
| 29 | | |
|---|
| 30 | | sessionLists = cherrypy.config.getAll('sessionFilter.sessionList') |
|---|
| 31 | | |
|---|
| 32 | | # loop across all paths with session listts |
|---|
| 33 | | for sessionPath, sessionList in sessionLists.iteritems(): |
|---|
| 34 | | |
|---|
| 35 | | # if it isn't a list make it one |
|---|
| 36 | | if not isinstance(sessionList,list): |
|---|
| 37 | | sessionList=[sessionList] |
|---|
| 38 | | |
|---|
| 39 | | for index in xrange(len(sessionList)): |
|---|
| 40 | | |
|---|
| 41 | | session = sessionList[index] |
|---|
| 42 | | |
|---|
| 43 | | # check if the session is a string, if it is |
|---|
| 44 | | # try and match it to a storage adaptor and replace |
|---|
| 45 | | # the string in the list with the initilized adaptor |
|---|
| 46 | | if isinstance(session, str): |
|---|
| 47 | | sessionName = session |
|---|
| 48 | | # if the session is off skip to the next session in the list |
|---|
| 49 | | if not cherrypy.config.get('sessionFilter.%s.on' % sessionName, True): |
|---|
| 50 | | continue |
|---|
| 51 | | |
|---|
| 52 | | # look up the storage type or return the default |
|---|
| 53 | | storageType = sessionconfig.retrieve('storageType', sessionName) |
|---|
| 54 | | |
|---|
| 55 | | # try to initilize a built in session |
|---|
| 56 | | try: |
|---|
| 57 | | sessionManager = _sessionTypes[storageType](sessionName) |
|---|
| 58 | | except KeyError: |
|---|
| 59 | | # the storageType is not built in |
|---|
| 60 | | try: |
|---|
| 61 | | sessionManger = cherrypy._cputil.getSpecialAttribute(storageType)(sessionName) |
|---|
| 62 | | except cherrypy.InternalError: |
|---|
| 63 | | # it is not a built in session and the adaptor has not been |
|---|
| 64 | | # set as an attribute in the CherryPy tree |
|---|
| 65 | | raise SessionBadStorageTypeError(storageType) |
|---|
| 66 | | |
|---|
| 67 | | |
|---|
| 68 | | # we need to remember the path |
|---|
| 69 | | sessionManager.path = sessionPath |
|---|
| 70 | | |
|---|
| 71 | | # the session is born clean |
|---|
| 72 | | sessionManager.lastCleanUp = time.time() |
|---|
| 73 | | |
|---|
| 74 | | # replace the entry in the session list |
|---|
| 75 | | sessionList[index] = sessionManager |
|---|
| 76 | | |
|---|
| 77 | | # put then new session list back in the config map |
|---|
| 78 | | cherrypy.config.update({sessionPath: {'sessionFilter.sessionList' : sessionList} }) |
|---|
| 79 | | else: |
|---|
| 80 | | sessionManager = session |
|---|
| 81 | | |
|---|
| 82 | | sessions.append(sessionManager) |
|---|
| 83 | | |
|---|
| 84 | | return sessions |
|---|
| 85 | | |
|---|
| | 31 | def __init__(self): |
|---|
| | 32 | """ Initilizes the session filter and creates cherrypy.sessions """ |
|---|
| | 33 | |
|---|
| | 34 | try: |
|---|
| | 35 | from threading import local |
|---|
| | 36 | except ImportError: |
|---|
| | 37 | from cherrypy._cpthreadinglocal import local |
|---|
| | 38 | |
|---|
| | 39 | # Create as sessions object for accessing session data |
|---|
| | 40 | cherrypy.sessions = local() |
|---|
| | 41 | |
|---|
| | 42 | |
|---|
| | 43 | def __newSessionManager(self, sessionName, sessionPath): |
|---|
| | 44 | """ |
|---|
| | 45 | Takes the name of a new session and its configuration path. |
|---|
| | 46 | Returns a storageAdaptor instance maching the configured storage type. |
|---|
| | 47 | If the storage type is not built in, it tries to use sessionFilter.storageAadaptors. |
|---|
| | 48 | If the storage type still can not be found, an exception is raised. |
|---|
| | 49 | """ |
|---|
| | 50 | # look up the storage type or return the default |
|---|
| | 51 | storageType = sessionconfig.retrieve('storageType', sessionName) |
|---|
| | 52 | |
|---|
| | 53 | # try to initilize a built in session |
|---|
| | 54 | try: |
|---|
| | 55 | storageAdaptor = _sessionTypes[storageType] |
|---|
| | 56 | except KeyError: |
|---|
| | 57 | # the storageType is not built in |
|---|
| | 58 | |
|---|
| | 59 | # check for custom storage adaptors |
|---|
| | 60 | adaptors = cpg.config.get('sessionFilter.storageAdaptors') |
|---|
| | 61 | try: |
|---|
| | 62 | storageAdaptor = adaptors[storageType] |
|---|
| | 63 | except cherrypy.InternalError: |
|---|
| | 64 | # we couldn't find the session |
|---|
| | 65 | raise SessionBadStorageTypeError(storageType) |
|---|
| | 66 | |
|---|
| | 67 | return storageAdaptor(sessionName, sessionPath) |
|---|
| | 68 | |
|---|
| | 69 | # this function gets all active sessions based on the path |
|---|
| | 70 | def __getSessions(self): |
|---|
| | 71 | """ |
|---|
| | 72 | Returns a list containing instances of all active sessions for the current request path. |
|---|
| | 73 | """ |
|---|
| | 74 | sessions = [] |
|---|
| | 75 | |
|---|
| | 76 | sessionLists = cherrypy.config.getAll('sessionFilter.sessionList') |
|---|
| | 77 | |
|---|
| | 78 | # loop across all paths with session listts |
|---|
| | 79 | for sessionPath, sessionList in sessionLists.iteritems(): |
|---|
| | 80 | |
|---|
| | 81 | # if it isn't a list make it one |
|---|
| | 82 | if not isinstance(sessionList,list): |
|---|
| | 83 | sessionList=[sessionList] |
|---|
| | 84 | |
|---|
| | 85 | for index in xrange(len(sessionList)): |
|---|
| | 86 | |
|---|
| | 87 | session = sessionList[index] |
|---|
| | 88 | |
|---|
| | 89 | # check if the session is a string, if it is |
|---|
| | 90 | # try and match it to a storage adaptor and replace |
|---|
| | 91 | # the string in the list with the initilized adaptor |
|---|
| | 92 | if isinstance(session, str): |
|---|
| | 93 | sessionName = session |
|---|
| | 94 | |
|---|
| | 95 | # if the session is off skip to the next session in the list |
|---|
| | 96 | if not cherrypy.config.get('sessionFilter.%s.on' % session, True): |
|---|
| | 97 | continue |
|---|
| | 98 | sessionManager = self.__newSessionManager(session, sessionPath) |
|---|
| | 99 | |
|---|
| | 100 | # now we replace the string name with the instanced storage class |
|---|
| | 101 | # thanks to references this will directly change the configMap |
|---|
| | 102 | sessionList[index] = sessionManager |
|---|
| | 103 | |
|---|
| | 104 | else: |
|---|
| | 105 | sessionManager = session |
|---|
| | 106 | |
|---|
| | 107 | sessions.append(sessionManager) |
|---|
| | 108 | |
|---|
| | 109 | return sessions |
|---|
| | 110 | |
|---|
| | 111 | |
|---|
| | 112 | |
|---|