~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: 2012-06-28 01:52:02 UTC
  • Revision ID: me@williamgrant.id.au-20120628015202-f6ru7o367gt6nvgz
Hah

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.database import User
 
25
import ivle.date
 
26
from ivle.pulldown_subj import enrol_user
24
27
from ivle.webapp import ApplicationRoot
25
 
from ivle.webapp.base.rest import JSONRESTView, require_permission
 
28
from ivle.webapp.base.forms import BaseFormView, URLNameValidator
26
29
from ivle.webapp.base.xhtml import XHTMLView
27
30
from ivle.webapp.base.plugins import ViewPlugin, MediaPlugin
28
31
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
32
 
34
33
 
35
34
class UsersView(XHTMLView):
39
38
    breadcrumb_text = 'Users'
40
39
 
41
40
    def authorize(self, req):
42
 
        return req.user.admin
 
41
        return req.user and req.user.admin
43
42
 
44
43
    def populate(self, req, ctx):
45
44
        ctx['req'] = req
46
45
        ctx['users'] = req.store.find(User).order_by(User.login)
47
46
 
48
47
 
49
 
# List of fields returned as part of the user JSON dictionary
50
 
# (as returned by the get_user action)
51
 
user_fields_list = (
52
 
    "login", "state", "unixid", "email", "nick", "fullname",
53
 
    "admin", "studentid", "acct_exp", "pass_exp", "last_login",
54
 
    "svn_pass"
55
 
)
56
 
 
57
 
class UserRESTView(JSONRESTView):
58
 
    """
59
 
    A REST interface to the user object.
60
 
    """
61
 
 
62
 
    @require_permission('view')
63
 
    def GET(self, req):
64
 
        # XXX Check Caps
65
 
        user = ivle.util.object_to_dict(user_fields_list, self.context)
66
 
        # Convert time stamps to nice strings
67
 
        for k in 'pass_exp', 'acct_exp', 'last_login':
68
 
            if user[k] is not None:
69
 
                user[k] = unicode(user[k])
70
 
 
71
 
        user['local_password'] = self.context.passhash is not None
72
 
        return user
73
 
 
74
48
class UserEditSchema(formencode.Schema):
75
49
    nick = formencode.validators.UnicodeString(not_empty=True)
76
50
    email = formencode.validators.Email(not_empty=False,
77
51
                                        if_missing=None)
78
52
 
79
 
class UserEditView(XHTMLView):
 
53
class UserEditView(BaseFormView):
80
54
    """A form to change a user's details."""
81
55
    template = 'templates/user-edit.html'
82
56
    tab = 'users'
83
57
    permission = 'edit'
84
58
 
85
 
    def filter(self, stream, ctx):
86
 
        return stream | HTMLFormFiller(data=ctx['data'])
 
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
87
73
 
88
74
    def populate(self, req, ctx):
89
 
        if req.method == 'POST':
90
 
            data = dict(req.get_fieldstorage())
91
 
            try:
92
 
                validator = UserEditSchema()
93
 
                data = validator.to_python(data, state=req)
94
 
                self.context.nick = data['nick']
95
 
                self.context.email = unicode(data['email']) if data['email'] \
96
 
                                     else None
97
 
                req.store.commit()
98
 
                req.throw_redirect(req.uri)
99
 
            except formencode.Invalid, e:
100
 
                errors = e.unpack_errors()
101
 
        else:
102
 
            data = {'nick': self.context.nick,
103
 
                    'email': self.context.email
104
 
                   }
105
 
            errors = {}
106
 
 
 
75
        super(UserEditView, self).populate(req, ctx)
107
76
        ctx['format_datetime'] = ivle.date.make_date_nice
108
77
        ctx['format_datetime_short'] = ivle.date.format_datetime_for_paragraph
 
78
        ctx['svn_url'] = req.user.get_svn_url(req.config)
 
79
        ctx['svn_pass'] = req.user.svn_pass
109
80
 
110
 
        ctx['req'] = req
111
 
        ctx['user'] = self.context
112
 
        ctx['data'] = data
113
 
        ctx['errors'] = errors
114
81
 
115
82
class UserAdminSchema(formencode.Schema):
116
83
    admin = formencode.validators.StringBoolean(if_missing=False)
 
84
    disabled = formencode.validators.StringBoolean(if_missing=False)
117
85
    fullname = formencode.validators.UnicodeString(not_empty=True)
118
86
    studentid = formencode.validators.UnicodeString(not_empty=False,
119
87
                                                    if_missing=None
120
88
                                                    )
121
89
 
122
 
class UserAdminView(XHTMLView):
 
90
class UserAdminView(BaseFormView):
123
91
    """A form for admins to change more of a user's details."""
124
92
    template = 'templates/user-admin.html'
125
93
    tab = 'users'
126
94
 
127
95
    def authorize(self, req):
128
96
        """Only allow access if the requesting user is an admin."""
129
 
        return req.user.admin
130
 
 
131
 
    def filter(self, stream, ctx):
132
 
        return stream | HTMLFormFiller(data=ctx['data'])
 
97
        return req.user and req.user.admin
 
98
 
 
99
    @property
 
100
    def validator(self):
 
101
        return UserAdminSchema()
 
102
 
 
103
    def get_default_data(self, req):
 
104
        return {'admin': self.context.admin,
 
105
                'disabled': self.context.state == u'disabled',
 
106
                'fullname': self.context.fullname,
 
107
                'studentid': self.context.studentid,
 
108
                }
 
109
 
 
110
    def save_object(self, req, data):
 
111
        if self.context is req.user:
 
112
            # Admin checkbox is disabled -- assume unchanged
 
113
            data['admin'] = self.context.admin
 
114
            data['disabled'] = self.context.state == u'disabled'
 
115
        else:
 
116
            self.context.admin = data['admin']
 
117
            if self.context.state in (u'enabled', u'disabled'):
 
118
                self.context.state = (u'disabled' if data['disabled']
 
119
                        else u'enabled')
 
120
        self.context.fullname = data['fullname'] \
 
121
                                if data['fullname'] else None
 
122
        self.context.studentid = data['studentid'] \
 
123
                                 if data['studentid'] else None
 
124
        return self.context
133
125
 
134
126
    def populate(self, req, ctx):
135
 
        if req.method == 'POST':
136
 
            data = dict(req.get_fieldstorage())
137
 
            try:
138
 
                validator = UserAdminSchema()
139
 
                data = validator.to_python(data, state=req)
140
 
 
141
 
                self.context.admin = data['admin']
142
 
                self.context.fullname = data['fullname'] \
143
 
                                        if data['fullname'] else None
144
 
                self.context.studentid = data['studentid'] \
145
 
                                         if data['studentid'] else None
146
 
                req.store.commit()
147
 
                req.throw_redirect(req.uri)
148
 
            except formencode.Invalid, e:
149
 
                errors = e.unpack_errors()
150
 
        else:
151
 
            data = {'admin': self.context.admin,
152
 
                    'fullname': self.context.fullname,
153
 
                    'studentid': self.context.studentid,
154
 
                   }
155
 
            errors = {}
156
 
 
157
 
        ctx['req'] = req
158
 
        ctx['user'] = self.context
159
 
        ctx['data'] = data
160
 
        ctx['errors'] = errors
 
127
        super(UserAdminView, self).populate(req, ctx)
 
128
 
 
129
        # Disable the admin checkbox if editing oneself
 
130
        ctx['disable_admin'] = self.context is req.user
161
131
 
162
132
class PasswordChangeView(XHTMLView):
163
133
    """A form to change a user's password, with knowledge of the old one."""
200
170
 
201
171
    def authorize(self, req):
202
172
        """Only allow access if the requesting user is an admin."""
203
 
        return req.user.admin
 
173
        return req.user and req.user.admin
204
174
 
205
175
    def populate(self, req, ctx):
206
176
        error = None
218
188
        ctx['user'] = self.context
219
189
        ctx['error'] = error
220
190
 
 
191
 
 
192
class UserNewSchema(formencode.Schema):
 
193
    login = URLNameValidator() # XXX: Validate uniqueness.
 
194
    admin = formencode.validators.StringBoolean(if_missing=False)
 
195
    fullname = formencode.validators.UnicodeString(not_empty=True)
 
196
    studentid = formencode.validators.UnicodeString(not_empty=False,
 
197
                                                    if_missing=None
 
198
                                                    )
 
199
    email = formencode.validators.Email(not_empty=False,
 
200
                                        if_missing=None)
 
201
 
 
202
 
 
203
class UserNewView(BaseFormView):
 
204
    """A form for admins to create new users."""
 
205
    template = 'templates/user-new.html'
 
206
    tab = 'users'
 
207
 
 
208
    def authorize(self, req):
 
209
        """Only allow access if the requesting user is an admin."""
 
210
        return req.user and req.user.admin
 
211
 
 
212
    @property
 
213
    def validator(self):
 
214
        return UserNewSchema()
 
215
 
 
216
    def get_default_data(self, req):
 
217
        return {}
 
218
 
 
219
    def save_object(self, req, data):
 
220
        data['nick'] = data['fullname']
 
221
        data['email'] = unicode(data['email']) if data['email'] else None
 
222
        userobj = User(**data)
 
223
        req.store.add(userobj)
 
224
        enrol_user(req.config, req.store, userobj)
 
225
 
 
226
        return userobj
 
227
 
 
228
 
221
229
class Plugin(ViewPlugin, MediaPlugin):
222
230
    """
223
231
    The Plugin class for the user plugin.
225
233
 
226
234
    forward_routes = (root_to_user,)
227
235
    reverse_routes = (user_url,)
228
 
    views = [(ApplicationRoot, 'users', UsersView),
229
 
             (ivle.database.User, '+index', UserEditView),
230
 
             (ivle.database.User, '+admin', UserAdminView),
231
 
             (ivle.database.User, '+changepassword', PasswordChangeView),
232
 
             (ivle.database.User, '+resetpassword', PasswordResetView),
233
 
             (ivle.database.User, '+index', UserRESTView, 'api'),
 
236
    views = [(ApplicationRoot, ('users', '+index'), UsersView),
 
237
             (ApplicationRoot, ('users', '+new'), UserNewView),
 
238
             (User, '+index', UserEditView),
 
239
             (User, '+admin', UserAdminView),
 
240
             (User, '+changepassword', PasswordChangeView),
 
241
             (User, '+resetpassword', PasswordResetView),
234
242
             ]
235
243
 
236
244
    tabs = [
237
245
        ('users', 'Users', 'Display and edit all users',
238
 
         'users.png', 'users', 0, True)
 
246
         'users.png', 'users', 90, True)
239
247
    ]
240
248
 
241
249
    public_forward_routes = forward_routes