3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from ipsilon.util.data import Store
25 def __init__(self, value):
26 # implement lookup of sites id for link/name
32 def __init__(self, username):
35 self._userdata = dict()
37 self._userdata = self._get_user_data(username)
40 def _get_user_data(self, username):
42 return store.get_user_preferences(username)
46 self._userdata = dict()
49 def is_anonymous(self):
56 if 'is_admin' in self._userdata:
57 if self._userdata['is_admin'] == '1':
62 def is_admin(self, value):
64 self._userdata['is_admin'] = '1'
66 self._userdata['is_admin'] = '0'
70 if 'fullname' in self._userdata:
71 return self._userdata['fullname']
76 def fullname(self, value):
77 self._userdata['fullname'] = value
81 if 'email' in self._userdata:
82 return self._userdata['email']
88 if 'sites' in self._userdata:
90 for site in self._userdata['sites']:
97 # TODO: implement setting sites via the user object ?
101 class UserSession(object):
103 self.user = self.get_data('user', 'name')
105 def _debug(self, fact):
106 if cherrypy.config.get('debug', False):
110 return User(self.user)
112 def remote_login(self):
113 if cherrypy.request.login:
114 self.login(cherrypy.request.login)
116 self.nuke_data('user')
118 def login(self, username):
119 if self.user == username:
122 # REMOTE_USER changed, replace user
123 self.nuke_data('user')
124 self.save_data('user', 'name', username)
127 cherrypy.log('LOGIN SUCCESSFUL: %s' % username)
129 def logout(self, user):
131 if not type(user) is User:
133 # Completely reset user data
134 cherrypy.log.error('%s %s' % (user.name, user.fullname))
137 # Destroy current session in all cases
138 cherrypy.lib.sessions.expire()
140 def save_data(self, facility, name, data):
141 """ Save named data in the session so it can be retrieved later """
142 if facility not in cherrypy.session:
143 cherrypy.session[facility] = dict()
144 cherrypy.session[facility][name] = data
145 cherrypy.session.save()
146 self._debug('Saved session data named [%s:%s]' % (facility, name))
148 def get_data(self, facility, name):
149 """ Get named data in the session if available """
150 if facility not in cherrypy.session:
152 if name not in cherrypy.session[facility]:
154 return cherrypy.session[facility][name]
156 def nuke_data(self, facility, name=None):
157 if facility not in cherrypy.session:
160 if name not in cherrypy.session[facility]:
162 cherrypy.session[facility][name] = None
163 del cherrypy.session[facility][name]
164 self._debug('Nuked session data named [%s:%s]' % (facility, name))
166 del cherrypy.session[facility]
167 self._debug('Nuked session facility [%s]' % (facility,))
168 cherrypy.session.save()