~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-02-11 05:54:45 UTC
  • Revision ID: matt.giuca@gmail.com-20100211055445-151qrs4xczzl5rns
Docs: Completed Tour of IVLE (finished Admin section). Apologies for the mess on the previous commit -- committed an unfinished document.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import formencode.validators
22
22
from genshi.filters import HTMLFormFiller
23
23
 
 
24
from ivle.webapp import ApplicationRoot
24
25
from ivle.webapp.base.rest import JSONRESTView, require_permission
25
26
from ivle.webapp.base.xhtml import XHTMLView
26
27
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
27
 
from ivle.webapp.errors import NotFound, Unauthorized
 
28
from ivle.webapp.admin.publishing import root_to_user, user_url
 
29
from ivle.database import User
28
30
import ivle.database
29
31
import ivle.date
30
32
import ivle.util
31
33
 
 
34
 
 
35
class UsersView(XHTMLView):
 
36
    """A list of all IVLE users."""
 
37
    template = 'templates/users.html'
 
38
    tab = 'users'
 
39
    breadcrumb_text = 'Users'
 
40
 
 
41
    def authorize(self, req):
 
42
        return req.user and req.user.admin
 
43
 
 
44
    def populate(self, req, ctx):
 
45
        ctx['req'] = req
 
46
        ctx['users'] = req.store.find(User).order_by(User.login)
 
47
 
 
48
 
32
49
# List of fields returned as part of the user JSON dictionary
33
50
# (as returned by the get_user action)
34
51
user_fields_list = (
41
58
    """
42
59
    A REST interface to the user object.
43
60
    """
44
 
    def __init__(self, req, login):
45
 
        super(UserRESTView, self).__init__(self, req, login)
46
 
        self.context = ivle.database.User.get_by_login(req.store, login)
47
 
        if self.context is None:
48
 
            raise NotFound()
49
61
 
50
62
    @require_permission('view')
51
63
    def GET(self, req):
67
79
class UserEditView(XHTMLView):
68
80
    """A form to change a user's details."""
69
81
    template = 'templates/user-edit.html'
70
 
    tab = 'settings'
 
82
    tab = 'users'
71
83
    permission = 'edit'
72
84
 
73
 
    def __init__(self, req, login):
74
 
        self.context = ivle.database.User.get_by_login(req.store, login)
75
 
        if self.context is None:
76
 
            raise NotFound()
77
 
 
78
85
    def filter(self, stream, ctx):
79
86
        return stream | HTMLFormFiller(data=ctx['data'])
80
87
 
107
114
 
108
115
class UserAdminSchema(formencode.Schema):
109
116
    admin = formencode.validators.StringBoolean(if_missing=False)
 
117
    disabled = formencode.validators.StringBoolean(if_missing=False)
110
118
    fullname = formencode.validators.UnicodeString(not_empty=True)
111
119
    studentid = formencode.validators.UnicodeString(not_empty=False,
112
120
                                                    if_missing=None
115
123
class UserAdminView(XHTMLView):
116
124
    """A form for admins to change more of a user's details."""
117
125
    template = 'templates/user-admin.html'
118
 
    tab = 'settings'
119
 
 
120
 
    def __init__(self, req, login):
121
 
        self.context = ivle.database.User.get_by_login(req.store, login)
122
 
        if self.context is None:
123
 
            raise NotFound()
 
126
    tab = 'users'
124
127
 
125
128
    def authorize(self, req):
126
129
        """Only allow access if the requesting user is an admin."""
127
 
        return req.user.admin
 
130
        return req.user and req.user.admin
128
131
 
129
132
    def filter(self, stream, ctx):
130
133
        return stream | HTMLFormFiller(data=ctx['data'])
136
139
                validator = UserAdminSchema()
137
140
                data = validator.to_python(data, state=req)
138
141
 
139
 
                self.context.admin = data['admin']
 
142
                if self.context is req.user:
 
143
                    # Admin checkbox is disabled -- assume unchanged
 
144
                    data['admin'] = self.context.admin
 
145
                    data['disabled'] = self.context.state == u'disabled'
 
146
                else:
 
147
                    self.context.admin = data['admin']
 
148
                    if self.context.state in (u'enabled', u'disabled'):
 
149
                        self.context.state = (u'disabled' if data['disabled']
 
150
                                else u'enabled')
140
151
                self.context.fullname = data['fullname'] \
141
152
                                        if data['fullname'] else None
142
153
                self.context.studentid = data['studentid'] \
147
158
                errors = e.unpack_errors()
148
159
        else:
149
160
            data = {'admin': self.context.admin,
 
161
                    'disabled': self.context.state == u'disabled',
150
162
                    'fullname': self.context.fullname,
151
163
                    'studentid': self.context.studentid,
152
164
                   }
154
166
 
155
167
        ctx['req'] = req
156
168
        ctx['user'] = self.context
 
169
        # Disable the Admin checkbox if editing oneself
 
170
        ctx['disable_admin'] = self.context is req.user
157
171
        ctx['data'] = data
158
172
        ctx['errors'] = errors
159
173
 
160
174
class PasswordChangeView(XHTMLView):
161
175
    """A form to change a user's password, with knowledge of the old one."""
162
176
    template = 'templates/user-password-change.html'
163
 
    tab = 'settings'
 
177
    tab = 'users'
164
178
    permission = 'edit'
165
179
 
166
 
    def __init__(self, req, login):
167
 
        self.context = ivle.database.User.get_by_login(req.store, login)
168
 
        if self.context is None:
169
 
            raise NotFound()
170
 
 
171
180
    def authorize(self, req):
172
181
        """Only allow access if the requesting user holds the permission,
173
182
           and the target user has a password set. Otherwise we might be
199
208
class PasswordResetView(XHTMLView):
200
209
    """A form to reset a user's password, without knowledge of the old one."""
201
210
    template = 'templates/user-password-reset.html'
202
 
    tab = 'settings'
203
 
 
204
 
    def __init__(self, req, login):
205
 
        self.context = ivle.database.User.get_by_login(req.store, login)
206
 
        if self.context is None:
207
 
            raise NotFound()
 
211
    tab = 'users'
208
212
 
209
213
    def authorize(self, req):
210
214
        """Only allow access if the requesting user is an admin."""
211
 
        return req.user.admin
 
215
        return req.user and req.user.admin
212
216
 
213
217
    def populate(self, req, ctx):
214
218
        error = None
230
234
    """
231
235
    The Plugin class for the user plugin.
232
236
    """
233
 
    # Magic attribute: urls
234
 
    # Sequence of pairs/triples of
235
 
    # (regex str, handler class, kwargs dict)
236
 
    # The kwargs dict is passed to the __init__ of the view object
237
 
    urls = [
238
 
        ('~:login/+edit', UserEditView),
239
 
        ('~:login/+admin', UserAdminView),
240
 
        ('~:login/+changepassword', PasswordChangeView),
241
 
        ('~:login/+resetpassword', PasswordResetView),
242
 
        ('api/~:login', UserRESTView),
 
237
 
 
238
    forward_routes = (root_to_user,)
 
239
    reverse_routes = (user_url,)
 
240
    views = [(ApplicationRoot, 'users', UsersView),
 
241
             (ivle.database.User, '+index', UserEditView),
 
242
             (ivle.database.User, '+admin', UserAdminView),
 
243
             (ivle.database.User, '+changepassword', PasswordChangeView),
 
244
             (ivle.database.User, '+resetpassword', PasswordResetView),
 
245
             (ivle.database.User, '+index', UserRESTView, 'api'),
 
246
             ]
 
247
 
 
248
    tabs = [
 
249
        ('users', 'Users', 'Display and edit all users',
 
250
         'users.png', 'users', 0, True)
243
251
    ]
244
252
 
 
253
    public_forward_routes = forward_routes
 
254
    public_reverse_routes = reverse_routes
 
255
 
245
256
    media = 'user-media'