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

« back to all changes in this revision

Viewing changes to www/apps/userservice/__init__.py

  • Committer: William Grant
  • Date: 2009-01-20 06:00:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120060055-iuvd8hycor67acfa
ivle.rpc.decorators: Add (new package, too). Has a couple of decorators to
    apply most security policy in userservice, making it significantly shorter
    and easier to audit.
www/apps/userservice: Use the decorators to protect all actions whose existing
    policy can be easily replaced with them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
149
149
from ivle.conf import (usrmgt_host, usrmgt_port, usrmgt_magic)
150
150
import ivle.pulldown_subj
151
151
 
 
152
from ivle.rpc.decorators import require_method, require_cap
 
153
 
152
154
from ivle.auth import AuthError, authenticate
153
155
import urllib
154
156
 
183
185
        "%s is not a valid userservice action." % repr(req.path))
184
186
    func(req, fields)
185
187
 
 
188
@require_method('POST')
186
189
def handle_activate_me(req, fields):
187
190
    """Create the jail, svn, etc, for the currently logged in user (this is
188
191
    put in the queue for usermgt to do).
202
205
    their acceptance). It must only be called through a POST request.
203
206
    """
204
207
    try:
205
 
        if req.method != "POST":
206
 
            req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
207
 
            "Only POST requests are valid methods to activate_me.")
208
208
        try:
209
209
            declaration = fields.getfirst('declaration')
210
210
        except AttributeError:
274
274
create_user_fields_optional = [
275
275
    'password', 'nick', 'email', 'studentid'
276
276
]
 
277
 
 
278
@require_method('POST')
 
279
@require_cap(caps.CAP_UPDATEUSER)
277
280
def handle_create_user(req, fields):
278
281
    """Create a new user, whose state is no_agreement.
279
282
    This does not create the user's jail, just an entry in the database which
300
303
                      STRING OPTIONAL
301
304
       Return Value: the uid associated with the user. INT
302
305
    """
303
 
    if req.method != "POST":
304
 
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
305
 
            "Only POST requests are valid methods to create_user.")
306
 
    # Check if this user has CAP_UPDATEUSER
307
 
    if not req.user.hasCap(caps.CAP_UPDATEUSER):
308
 
        req.throw_error(req.HTTP_FORBIDDEN,
309
 
        "You do not have permission to create users.")
310
 
 
311
306
    # Make a dict of fields to create
312
307
    create = {}
313
308
    for f in create_user_fields_required:
339
334
    'password', 'nick', 'email', 'rolenm', 'unixid', 'fullname',
340
335
    'studentid'
341
336
]
 
337
 
 
338
@require_method('POST')
342
339
def handle_update_user(req, fields):
343
340
    """Update a user's account details.
344
341
    This can be done in a limited form by any user, on their own account,
345
342
    or with full powers by a user with CAP_UPDATEUSER on any account.
346
343
    """
347
 
    if req.method != "POST":
348
 
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
349
 
        "Only POST requests are valid methods to update_user.")
350
 
 
351
344
    # Only give full powers if this user has CAP_UPDATEUSER
352
345
    fullpowers = req.user.hasCap(caps.CAP_UPDATEUSER)
353
346
    # List of fields that may be changed
533
526
    response = cjson.encode(dict_projectsets)
534
527
    req.write(response)
535
528
 
 
529
@require_method('POST')
 
530
@require_cap(caps.CAP_MANAGEGROUPS)
536
531
def handle_create_group(req, fields):
537
532
    """Required cap: CAP_MANAGEGROUPS
538
533
    Creates a project group in a specific project set
543
538
    Returns:
544
539
        groupid
545
540
    """
546
 
    if req.method != "POST":
547
 
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
548
 
            "Only POST requests are valid methods to create_user.")
549
 
    # Check if this is allowed to manage groups
550
 
    if not req.user.hasCap(caps.CAP_MANAGEGROUPS):
551
 
        req.throw_error(req.HTTP_FORBIDDEN,
552
 
        "You do not have permission to manage groups.")
553
541
    # Get required fields
554
542
    projectsetid = fields.getfirst('projectsetid').value
555
543
    groupnm = fields.getfirst('groupnm').value
661
649
    req.content_type = "text/plain"
662
650
    req.write(response)
663
651
 
 
652
@require_method('POST')
 
653
@require_cap(caps.CAP_MANAGEGROUPS)
664
654
def handle_assign_group(req, fields):
665
655
    """ Required cap: CAP_MANAGEGROUPS
666
656
    Assigns a user to a project group
667
657
    Required:
668
658
        login, groupid
669
659
    """
670
 
    if req.method != "POST":
671
 
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
672
 
            "Only POST requests are valid methods to create_user.")
673
 
    # Check if this user is allowed to manage groups
674
 
    if not req.user.hasCap(caps.CAP_MANAGEGROUPS):
675
 
        req.throw_error(req.HTTP_FORBIDDEN,
676
 
        "You do not have permission to manage groups.")
677
660
    # Get required fields
678
661
    login = fields.getfirst('login')
679
662
    groupid = fields.getfirst('groupid')