Create a user facility in the session
[cascardo/ipsilon.git] / ipsilon / util / user.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
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.
11 #
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.
16 #
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/>.
19
20 from ipsilon.util.data import Store
21 import cherrypy
22
23
24 class Site(object):
25     def __init__(self, value):
26         # implement lookup of sites id for link/name
27         self.link = value
28         self.name = value
29
30
31 class User(object):
32     def __init__(self, username):
33         if username is None:
34             self.name = None
35             self._userdata = dict()
36         else:
37             self._userdata = self._get_user_data(username)
38             self.name = username
39
40     def _get_user_data(self, username):
41         store = Store()
42         return store.get_user_preferences(username)
43
44     def reset(self):
45         self.name = None
46         self._userdata = dict()
47
48     @property
49     def is_anonymous(self):
50         if self.name:
51             return False
52         return True
53
54     @property
55     def is_admin(self):
56         if 'is_admin' in self._userdata:
57             if self._userdata['is_admin'] == '1':
58                 return True
59         return False
60
61     @is_admin.setter
62     def is_admin(self, value):
63         if value is True:
64             self._userdata['is_admin'] = '1'
65         else:
66             self._userdata['is_admin'] = '0'
67
68     @property
69     def fullname(self):
70         if 'fullname' in self._userdata:
71             return self._userdata['fullname']
72         else:
73             return self.name
74
75     @fullname.setter
76     def fullname(self, value):
77         self._userdata['fullname'] = value
78
79     @property
80     def sites(self):
81         if 'sites' in self._userdata:
82             d = []
83             for site in self._userdata['sites']:
84                 d.append(Site(site))
85         else:
86             return []
87
88     @sites.setter
89     def sites(self):
90         #TODO: implement setting sites via the user object ?
91         raise AttributeError
92
93
94 class UserSession(object):
95     def __init__(self):
96         self.user = self.get_data('user', 'name')
97
98     def _debug(self, fact):
99         if cherrypy.config.get('debug', False):
100             cherrypy.log(fact)
101
102     def get_user(self):
103         return User(self.user)
104
105     def remote_login(self):
106         if cherrypy.request.login:
107             return self.login(cherrypy.request.login)
108
109     def login(self, username):
110         if self.user == username:
111             return
112
113         # REMOTE_USER changed, replace user
114         self.nuke_data('user')
115         self.save_data('user', 'name', username)
116
117         cherrypy.log('LOGIN SUCCESSFUL: %s', username)
118
119     def logout(self, user):
120         if user is not None:
121             if not type(user) is User:
122                 raise TypeError
123             # Completely reset user data
124             cherrypy.log.error('%s %s' % (user.name, user.fullname))
125             user.reset()
126
127         # Destroy current session in all cases
128         cherrypy.lib.sessions.expire()
129
130     def save_data(self, facility, name, data):
131         """ Save named data in the session so it can be retrieved later """
132         if facility not in cherrypy.session:
133             cherrypy.session[facility] = dict()
134         cherrypy.session[facility][name] = data
135         cherrypy.session.save()
136         self._debug('Saved session data named [%s:%s]' % (facility, name))
137
138     def get_data(self, facility, name):
139         """ Get named data in the session if available """
140         if facility not in cherrypy.session:
141             return None
142         if name not in cherrypy.session[facility]:
143             return None
144         return cherrypy.session[facility][name]
145
146     def nuke_data(self, facility, name=None):
147         if facility not in cherrypy.session:
148             return
149         if name:
150             if name not in cherrypy.session[facility]:
151                 return
152             cherrypy.session[facility][name] = None
153             del cherrypy.session[facility][name]
154             self._debug('Nuked session data named [%s:%s]' % (facility, name))
155         else:
156             del cherrypy.session[facility]
157             self._debug('Nuked session facility [%s]' % (facility,))
158         cherrypy.session.save()