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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2010-03-05 07:00:41 UTC
  • Revision ID: matt.giuca@gmail.com-20100305070041-l6z1rfojn78bhiqd
subject.py: Minor refactor in displaying SVN urls. No longer pass the svn root to the Genshi template.

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.database import User
 
25
import ivle.date
 
26
from ivle.pulldown_subj import enrol_user
 
27
from ivle.webapp import ApplicationRoot
 
28
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
 
29
from ivle.webapp.base.xhtml import XHTMLView
 
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
 
31
from ivle.webapp.admin.publishing import root_to_user, user_url
 
32
 
 
33
 
 
34
class UsersView(XHTMLView):
 
35
    """A list of all IVLE users."""
 
36
    template = 'templates/users.html'
 
37
    tab = 'users'
 
38
    breadcrumb_text = 'Users'
 
39
 
 
40
    def authorize(self, req):
 
41
        return req.user and 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
class UserEditSchema(formencode.Schema):
 
49
    nick = formencode.validators.UnicodeString(not_empty=True)
 
50
    email = formencode.validators.Email(not_empty=False,
 
51
                                        if_missing=None)
 
52
 
 
53
class UserEditView(BaseFormView):
 
54
    """A form to change a user's details."""
 
55
    template = 'templates/user-edit.html'
 
56
    tab = 'users'
 
57
    permission = 'edit'
 
58
 
 
59
    @property
 
60
    def validator(self):
 
61
        return UserEditSchema()
 
62
 
 
63
    def get_default_data(self, req):
 
64
        return {'nick': self.context.nick,
 
65
                'email': self.context.email
 
66
                }
 
67
 
 
68
    def save_object(self, req, data):
 
69
        self.context.nick = data['nick']
 
70
        self.context.email = unicode(data['email']) if data['email'] \
 
71
                             else None
 
72
        return self.context
 
73
 
 
74
    def populate(self, req, ctx):
 
75
        super(UserEditView, self).populate(req, ctx)
 
76
        ctx['format_datetime'] = ivle.date.make_date_nice
 
77
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
 
78
        ctx['svn_pass'] = req.user.svn_pass
 
79
 
 
80
 
 
81
class UserAdminSchema(formencode.Schema):
 
82
    admin = formencode.validators.StringBoolean(if_missing=False)
 
83
    disabled = formencode.validators.StringBoolean(if_missing=False)
 
84
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
85
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
86
                                                    if_missing=None
 
87
                                                    )
 
88
 
 
89
class UserAdminView(BaseFormView):
 
90
    """A form for admins to change more of a user's details."""
 
91
    template = 'templates/user-admin.html'
 
92
    tab = 'users'
 
93
 
 
94
    def authorize(self, req):
 
95
        """Only allow access if the requesting user is an admin."""
 
96
        return req.user and req.user.admin
 
97
 
 
98
    @property
 
99
    def validator(self):
 
100
        return UserAdminSchema()
 
101
 
 
102
    def get_default_data(self, req):
 
103
        return {'admin': self.context.admin,
 
104
                'disabled': self.context.state == u'disabled',
 
105
                'fullname': self.context.fullname,
 
106
                'studentid': self.context.studentid,
 
107
                }
 
108
 
 
109
    def save_object(self, req, data):
 
110
        if self.context is req.user:
 
111
            # Admin checkbox is disabled -- assume unchanged
 
112
            data['admin'] = self.context.admin
 
113
            data['disabled'] = self.context.state == u'disabled'
 
114
        else:
 
115
            self.context.admin = data['admin']
 
116
            if self.context.state in (u'enabled', u'disabled'):
 
117
                self.context.state = (u'disabled' if data['disabled']
 
118
                        else u'enabled')
 
119
        self.context.fullname = data['fullname'] \
 
120
                                if data['fullname'] else None
 
121
        self.context.studentid = data['studentid'] \
 
122
                                 if data['studentid'] else None
 
123
        return self.context
 
124
 
 
125
    def populate(self, req, ctx):
 
126
        super(UserAdminView, self).populate(req, ctx)
 
127
 
 
128
        # Disable the admin checkbox if editing oneself
 
129
        ctx['disable_admin'] = self.context is req.user
 
130
 
 
131
class PasswordChangeView(XHTMLView):
 
132
    """A form to change a user's password, with knowledge of the old one."""
 
133
    template = 'templates/user-password-change.html'
 
134
    tab = 'users'
 
135
    permission = 'edit'
 
136
 
 
137
    def authorize(self, req):
 
138
        """Only allow access if the requesting user holds the permission,
 
139
           and the target user has a password set. Otherwise we might be
 
140
           clobbering external authn.
 
141
        """
 
142
        return super(PasswordChangeView, self).authorize(req) and \
 
143
               self.context.passhash is not None
 
144
 
 
145
    def populate(self, req, ctx):
 
146
        error = None
 
147
        if req.method == 'POST':
 
148
            data = dict(req.get_fieldstorage())
 
149
            if data.get('old_password') is None or \
 
150
               not self.context.authenticate(data.get('old_password')):
 
151
                error = 'Incorrect password.'
 
152
            elif data.get('new_password') != data.get('new_password_again'):
 
153
                error = 'New passwords do not match.'
 
154
            elif not data.get('new_password'):
 
155
                error = 'New password cannot be empty.'
 
156
            else:
 
157
                self.context.password = data['new_password']
 
158
                req.store.commit()
 
159
                req.throw_redirect(req.uri)
 
160
 
 
161
        ctx['req'] = req
 
162
        ctx['user'] = self.context
 
163
        ctx['error'] = error
 
164
 
 
165
class PasswordResetView(XHTMLView):
 
166
    """A form to reset a user's password, without knowledge of the old one."""
 
167
    template = 'templates/user-password-reset.html'
 
168
    tab = 'users'
 
169
 
 
170
    def authorize(self, req):
 
171
        """Only allow access if the requesting user is an admin."""
 
172
        return req.user and req.user.admin
 
173
 
 
174
    def populate(self, req, ctx):
 
175
        error = None
 
176
        if req.method == 'POST':
 
177
            data = dict(req.get_fieldstorage())
 
178
            if data.get('new_password') != data.get('new_password_again'):
 
179
                error = 'New passwords do not match.'
 
180
            elif not data.get('new_password'):
 
181
                error = 'New password cannot be empty.'
 
182
            else:
 
183
                self.context.password = data['new_password']
 
184
                req.store.commit()
 
185
                req.throw_redirect(req.uri)
 
186
 
 
187
        ctx['user'] = self.context
 
188
        ctx['error'] = error
 
189
 
 
190
 
 
191
class UserNewSchema(formencode.Schema):
 
192
    login = URLNameValidator() # XXX: Validate uniqueness.
 
193
    admin = formencode.validators.StringBoolean(if_missing=False)
 
194
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
195
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
196
                                                    if_missing=None
 
197
                                                    )
 
198
    email = formencode.validators.Email(not_empty=False,
 
199
                                        if_missing=None)
 
200
 
 
201
 
 
202
class UserNewView(BaseFormView):
 
203
    """A form for admins to create new users."""
 
204
    template = 'templates/user-new.html'
 
205
    tab = 'users'
 
206
 
 
207
    def authorize(self, req):
 
208
        """Only allow access if the requesting user is an admin."""
 
209
        return req.user and req.user.admin
 
210
 
 
211
    @property
 
212
    def validator(self):
 
213
        return UserNewSchema()
 
214
 
 
215
    def get_default_data(self, req):
 
216
        return {}
 
217
 
 
218
    def save_object(self, req, data):
 
219
        data['nick'] = data['fullname']
 
220
        data['email'] = unicode(data['email']) if data['email'] else None
 
221
        userobj = User(**data)
 
222
        req.store.add(userobj)
 
223
        enrol_user(req.config, req.store, userobj)
 
224
 
 
225
        return userobj
 
226
 
 
227
 
 
228
class Plugin(ViewPlugin, MediaPlugin):
 
229
    """
 
230
    The Plugin class for the user plugin.
 
231
    """
 
232
 
 
233
    forward_routes = (root_to_user,)
 
234
    reverse_routes = (user_url,)
 
235
    views = [(ApplicationRoot, ('users', '+index'), UsersView),
 
236
             (ApplicationRoot, ('users', '+new'), UserNewView),
 
237
             (User, '+index', UserEditView),
 
238
             (User, '+admin', UserAdminView),
 
239
             (User, '+changepassword', PasswordChangeView),
 
240
             (User, '+resetpassword', PasswordResetView),
 
241
             ]
 
242
 
 
243
    tabs = [
 
244
        ('users', 'Users', 'Display and edit all users',
 
245
         'users.png', 'users', 90, True)
 
246
    ]
 
247
 
 
248
    public_forward_routes = forward_routes
 
249
    public_reverse_routes = reverse_routes
 
250
 
 
251
    media = 'user-media'