~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/user.py

  • Committer: William Grant
  • Date: 2010-02-04 01:51:17 UTC
  • Revision ID: grantw@unimelb.edu.au-20100204015117-ir9gstr8x297561n
Unbreak diffservice with Subversion >= 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2009 The University of Melbourne
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
# Author: Matt Giuca, Will Grant
 
19
 
 
20
import formencode
 
21
import formencode.validators
 
22
from genshi.filters import HTMLFormFiller
 
23
 
 
24
from ivle.webapp import ApplicationRoot
 
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
 
26
from ivle.webapp.base.xhtml import XHTMLView
 
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
 
28
from ivle.webapp.admin.publishing import root_to_user, user_url
 
29
from ivle.database import User
 
30
import ivle.database
 
31
import ivle.date
 
32
import ivle.util
 
33
 
 
34
 
 
35
class UsersView(XHTMLView):
 
36
    """A list of all IVLE users."""
 
37
    template = 'templates/users.html'
 
38
    breadcrumb_text = 'Users'
 
39
 
 
40
    def authorize(self, req):
 
41
        return req.user.admin
 
42
 
 
43
    def populate(self, req, ctx):
 
44
        ctx['req'] = req
 
45
        ctx['users'] = req.store.find(User).order_by(User.login)
 
46
 
 
47
 
 
48
# List of fields returned as part of the user JSON dictionary
 
49
# (as returned by the get_user action)
 
50
user_fields_list = (
 
51
    "login", "state", "unixid", "email", "nick", "fullname",
 
52
    "admin", "studentid", "acct_exp", "pass_exp", "last_login",
 
53
    "svn_pass"
 
54
)
 
55
 
 
56
class UserRESTView(JSONRESTView):
 
57
    """
 
58
    A REST interface to the user object.
 
59
    """
 
60
 
 
61
    @require_permission('view')
 
62
    def GET(self, req):
 
63
        # XXX Check Caps
 
64
        user = ivle.util.object_to_dict(user_fields_list, self.context)
 
65
        # Convert time stamps to nice strings
 
66
        for k in 'pass_exp', 'acct_exp', 'last_login':
 
67
            if user[k] is not None:
 
68
                user[k] = unicode(user[k])
 
69
 
 
70
        user['local_password'] = self.context.passhash is not None
 
71
        return user
 
72
 
 
73
class UserEditSchema(formencode.Schema):
 
74
    nick = formencode.validators.UnicodeString(not_empty=True)
 
75
    email = formencode.validators.Email(not_empty=False,
 
76
                                        if_missing=None)
 
77
 
 
78
class UserEditView(XHTMLView):
 
79
    """A form to change a user's details."""
 
80
    template = 'templates/user-edit.html'
 
81
    tab = 'settings'
 
82
    permission = 'edit'
 
83
 
 
84
    def filter(self, stream, ctx):
 
85
        return stream | HTMLFormFiller(data=ctx['data'])
 
86
 
 
87
    def populate(self, req, ctx):
 
88
        if req.method == 'POST':
 
89
            data = dict(req.get_fieldstorage())
 
90
            try:
 
91
                validator = UserEditSchema()
 
92
                data = validator.to_python(data, state=req)
 
93
                self.context.nick = data['nick']
 
94
                self.context.email = unicode(data['email']) if data['email'] \
 
95
                                     else None
 
96
                req.store.commit()
 
97
                req.throw_redirect(req.uri)
 
98
            except formencode.Invalid, e:
 
99
                errors = e.unpack_errors()
 
100
        else:
 
101
            data = {'nick': self.context.nick,
 
102
                    'email': self.context.email
 
103
                   }
 
104
            errors = {}
 
105
 
 
106
        ctx['format_datetime'] = ivle.date.make_date_nice
 
107
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
 
108
 
 
109
        ctx['req'] = req
 
110
        ctx['user'] = self.context
 
111
        ctx['data'] = data
 
112
        ctx['errors'] = errors
 
113
 
 
114
class UserAdminSchema(formencode.Schema):
 
115
    admin = formencode.validators.StringBoolean(if_missing=False)
 
116
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
117
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
118
                                                    if_missing=None
 
119
                                                    )
 
120
 
 
121
class UserAdminView(XHTMLView):
 
122
    """A form for admins to change more of a user's details."""
 
123
    template = 'templates/user-admin.html'
 
124
    tab = 'settings'
 
125
 
 
126
    def authorize(self, req):
 
127
        """Only allow access if the requesting user is an admin."""
 
128
        return req.user.admin
 
129
 
 
130
    def filter(self, stream, ctx):
 
131
        return stream | HTMLFormFiller(data=ctx['data'])
 
132
 
 
133
    def populate(self, req, ctx):
 
134
        if req.method == 'POST':
 
135
            data = dict(req.get_fieldstorage())
 
136
            try:
 
137
                validator = UserAdminSchema()
 
138
                data = validator.to_python(data, state=req)
 
139
 
 
140
                self.context.admin = data['admin']
 
141
                self.context.fullname = data['fullname'] \
 
142
                                        if data['fullname'] else None
 
143
                self.context.studentid = data['studentid'] \
 
144
                                         if data['studentid'] else None
 
145
                req.store.commit()
 
146
                req.throw_redirect(req.uri)
 
147
            except formencode.Invalid, e:
 
148
                errors = e.unpack_errors()
 
149
        else:
 
150
            data = {'admin': self.context.admin,
 
151
                    'fullname': self.context.fullname,
 
152
                    'studentid': self.context.studentid,
 
153
                   }
 
154
            errors = {}
 
155
 
 
156
        ctx['req'] = req
 
157
        ctx['user'] = self.context
 
158
        ctx['data'] = data
 
159
        ctx['errors'] = errors
 
160
 
 
161
class PasswordChangeView(XHTMLView):
 
162
    """A form to change a user's password, with knowledge of the old one."""
 
163
    template = 'templates/user-password-change.html'
 
164
    tab = 'settings'
 
165
    permission = 'edit'
 
166
 
 
167
    def authorize(self, req):
 
168
        """Only allow access if the requesting user holds the permission,
 
169
           and the target user has a password set. Otherwise we might be
 
170
           clobbering external authn.
 
171
        """
 
172
        return super(PasswordChangeView, self).authorize(req) and \
 
173
               self.context.passhash is not None
 
174
 
 
175
    def populate(self, req, ctx):
 
176
        error = None
 
177
        if req.method == 'POST':
 
178
            data = dict(req.get_fieldstorage())
 
179
            if data.get('old_password') is None or \
 
180
               not self.context.authenticate(data.get('old_password')):
 
181
                error = 'Incorrect password.'
 
182
            elif data.get('new_password') != data.get('new_password_again'):
 
183
                error = 'New passwords do not match.'
 
184
            elif not data.get('new_password'):
 
185
                error = 'New password cannot be empty.'
 
186
            else:
 
187
                self.context.password = data['new_password']
 
188
                req.store.commit()
 
189
                req.throw_redirect(req.uri)
 
190
 
 
191
        ctx['req'] = req
 
192
        ctx['user'] = self.context
 
193
        ctx['error'] = error
 
194
 
 
195
class PasswordResetView(XHTMLView):
 
196
    """A form to reset a user's password, without knowledge of the old one."""
 
197
    template = 'templates/user-password-reset.html'
 
198
    tab = 'settings'
 
199
 
 
200
    def authorize(self, req):
 
201
        """Only allow access if the requesting user is an admin."""
 
202
        return req.user.admin
 
203
 
 
204
    def populate(self, req, ctx):
 
205
        error = None
 
206
        if req.method == 'POST':
 
207
            data = dict(req.get_fieldstorage())
 
208
            if data.get('new_password') != data.get('new_password_again'):
 
209
                error = 'New passwords do not match.'
 
210
            elif not data.get('new_password'):
 
211
                error = 'New password cannot be empty.'
 
212
            else:
 
213
                self.context.password = data['new_password']
 
214
                req.store.commit()
 
215
                req.throw_redirect(req.uri)
 
216
 
 
217
        ctx['user'] = self.context
 
218
        ctx['error'] = error
 
219
 
 
220
class Plugin(ViewPlugin, MediaPlugin):
 
221
    """
 
222
    The Plugin class for the user plugin.
 
223
    """
 
224
 
 
225
    forward_routes = (root_to_user,)
 
226
    reverse_routes = (user_url,)
 
227
    views = [(ApplicationRoot, 'users', UsersView),
 
228
             (ivle.database.User, '+index', UserEditView),
 
229
             (ivle.database.User, '+admin', UserAdminView),
 
230
             (ivle.database.User, '+changepassword', PasswordChangeView),
 
231
             (ivle.database.User, '+resetpassword', PasswordResetView),
 
232
             (ivle.database.User, '+index', UserRESTView, 'api'),
 
233
             ]
 
234
 
 
235
    public_forward_routes = forward_routes
 
236
    public_reverse_routes = reverse_routes
 
237
 
 
238
    media = 'user-media'