dd4b0021cb05b21380b028d646e9db03e61a829f
[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_admin(self):
50         if 'is_admin' in self._userdata:
51             if self._userdata['is_admin'] == '1':
52                 return True
53         return False
54
55     @is_admin.setter
56     def is_admin(self, value):
57         if value is True:
58             self._userdata['is_admin'] = '1'
59         else:
60             self._userdata['is_admin'] = '0'
61
62     @property
63     def fullname(self):
64         if 'fullname' in self._userdata:
65             return self._userdata['fullname']
66         else:
67             return self.name
68
69     @fullname.setter
70     def fullname(self, value):
71         self._userdata['fullname'] = value
72
73     @property
74     def sites(self):
75         if 'sites' in self._userdata:
76             d = []
77             for site in self._userdata['sites']:
78                 d.append(Site(site))
79         else:
80             return []
81
82     @sites.setter
83     def sites(self):
84         #TODO: implement setting sites via the user object ?
85         raise AttributeError
86
87
88 class UserSession(object):
89     def __init__(self):
90         self.user = cherrypy.session.get('user', None)
91
92     def _debug(self, fact):
93         if cherrypy.config.get('debug', False):
94             cherrypy.log(fact)
95
96     def get_user(self):
97         return User(self.user)
98
99     def remote_login(self):
100         if cherrypy.request.login:
101             return self.login(cherrypy.request.login)
102
103     def login(self, username):
104         if self.user == username:
105             return
106
107         # REMOTE_USER changed, replace user
108         cherrypy.session['user'] = username
109         cherrypy.session.save()
110
111         cherrypy.log('LOGIN SUCCESSFUL: %s', username)
112
113     def logout(self, user):
114         if user is not None:
115             if not type(user) is User:
116                 raise TypeError
117             # Completely reset user data
118             cherrypy.log.error('%s %s' % (user.name, user.fullname))
119             user.reset()
120
121         # Destroy current session in all cases
122         cherrypy.lib.sessions.expire()
123
124     def save_data(self, facility, name, data):
125         """ Save named data in the session so it can be retrieved later """
126         if facility not in cherrypy.session:
127             cherrypy.session[facility] = dict()
128         cherrypy.session[facility][name] = data
129         cherrypy.session.save()
130         self._debug('Saved session data named [%s:%s]' % (facility, name))
131
132     def get_data(self, facility, name):
133         """ Get named data in the session if available """
134         if facility not in cherrypy.session:
135             return None
136         if name not in cherrypy.session[facility]:
137             return None
138         return cherrypy.session[facility][name]
139
140     def nuke_data(self, facility, name):
141         if facility not in cherrypy.session:
142             return
143         if name not in cherrypy.session[facility]:
144             return
145         cherrypy.session[facility][name] = None
146         del cherrypy.session[facility][name]
147         cherrypy.session.save()
148         self._debug('Nuked session data named [%s:%s]' % (facility, name))