summaryrefslogtreecommitdiffstats
path: root/ipsilon/util/user.py
blob: 3a4962d29b4846d9aa07212af4b8576c24d06676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
#
# Copyright (C) 2013  Simo Sorce <simo@redhat.com>
#
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ipsilon.util.data import UserStore
from ipsilon.util.log import Log
import cherrypy


class Site(object):
    def __init__(self, value):
        # implement lookup of sites id for link/name
        self.link = value
        self.name = value


class User(object):
    def __init__(self, username):
        if username is None:
            self.name = None
            self._userdata = dict()
        else:
            self._userdata = self._get_user_data(username)
            self.name = username

    def _get_user_data(self, username):
        store = UserStore()
        return store.load_user_preferences(username)

    def reset(self):
        self.name = None
        self._userdata = dict()

    @property
    def is_anonymous(self):
        if self.name:
            return False
        return True

    @property
    def is_admin(self):
        if 'is_admin' in self._userdata:
            if str(self._userdata['is_admin']) == '1':
                return True
        return False

    @is_admin.setter
    def is_admin(self, value):
        if value is True:
            self._userdata['is_admin'] = '1'
        else:
            self._userdata['is_admin'] = '0'

    @property
    def fullname(self):
        if 'fullname' in self._userdata:
            return self._userdata['fullname']
        else:
            return self.name

    @fullname.setter
    def fullname(self, value):
        self._userdata['fullname'] = value

    @property
    def email(self):
        if 'email' in self._userdata:
            return self._userdata['email']
        else:
            return None

    @property
    def sites(self):
        if 'sites' in self._userdata:
            d = []
            for site in self._userdata['sites']:
                d.append(Site(site))
        else:
            return []

    @sites.setter
    def sites(self):
        # TODO: implement setting sites via the user object ?
        raise AttributeError

    def save_plugin_data(self, plugin, data):
        store = UserStore()
        store.save_plugin_data(plugin, self.name, data)

    def load_plugin_data(self, plugin):
        store = UserStore()
        return store.load_plugin_data(plugin, self.name)


class UserSession(Log):
    def __init__(self):
        self.user = self.get_data('user', 'name')
        self.userattrs = self.get_user_attrs()

    def get_user(self):
        return User(self.user)

    def remote_login(self):
        if cherrypy.request.login:
            self.login(cherrypy.request.login)
        else:
            self.nuke_data('user')

    def login(self, username, userattrs=None):
        if self.user == username:
            return

        # REMOTE_USER changed, replace user
        self.nuke_data('user')
        self.save_data('user', 'name', username)
        self.user = username

        # Save additional data provided by the login manager
        self.nuke_data('userattrs')
        if userattrs:
            self.save_user_attrs(userattrs)

        cherrypy.log('LOGIN SUCCESSFUL: %s' % username)

    def logout(self, user):
        if user is not None:
            if not type(user) is User:
                raise TypeError
            # Completely reset user data
            cherrypy.log.error('%s %s' % (user.name, user.fullname))
            user.reset()

        # Destroy current session in all cases
        cherrypy.lib.sessions.expire()

    def get_user_attrs(self):
        userattrs = dict()
        if 'userattrs' in cherrypy.session:
            userattrs = cherrypy.session['userattrs']
        return userattrs

    def save_user_attrs(self, userattrs):
        cherrypy.session['userattrs'] = userattrs
        cherrypy.session.save()
        self._debug('Saved user attrs')
        self.userattrs = userattrs

    def save_data(self, facility, name, data):
        """ Save named data in the session so it can be retrieved later """
        if facility not in cherrypy.session:
            cherrypy.session[facility] = dict()
        cherrypy.session[facility][name] = data
        cherrypy.session.save()
        self._debug('Saved session data named [%s:%s]' % (facility, name))

    def get_data(self, facility, name):
        """ Get named data in the session if available """
        if facility not in cherrypy.session:
            return None
        if name not in cherrypy.session[facility]:
            return None
        return cherrypy.session[facility][name]

    def nuke_data(self, facility, name=None):
        if facility not in cherrypy.session:
            return
        if name:
            if name not in cherrypy.session[facility]:
                return
            cherrypy.session[facility][name] = None
            del cherrypy.session[facility][name]
            self._debug('Nuked session data named [%s:%s]' % (facility, name))
        else:
            del cherrypy.session[facility]
            self._debug('Nuked session facility [%s]' % (facility,))
        cherrypy.session.save()