Example #1
0
def handle_activate_me(req, fields):
    """Create the jail, svn, etc, for the currently logged in user (this is
    put in the queue for usermgt to do).
    This will block until usermgt returns, which could take seconds to minutes
    in the extreme. Therefore, it is designed to be called by Ajax, with a
    nice "Please wait" message on the frontend.

    This will signal that the user has accepted the terms of the license
    agreement, and will result in the user's database status being set to
    "enabled". (Note that it will be set to "pending" for the duration of the
    handling).

    As such, it takes a single POST field, "declaration", which
    must have the value, "I accept the IVLE Terms of Service".
    (Otherwise users could navigate to /userservice/createme without
    "accepting" the terms - at least this way requires them to acknowledge
    their acceptance). It must only be called through a POST request.
    """

    user = get_user_details(req)

    try:
        declaration = fields.getfirst('declaration')
    except AttributeError:
        declaration = None      # Will fail next test
    if declaration != USER_DECLARATION:
        raise BadRequest()

    # Make sure the user's status is "no_agreement", and set status to
    # pending, within the one transaction. This ensures we only do this
    # one time.
    try:
        # Check that the user's status is "no_agreement".
        # (Both to avoid redundant calls, and to stop disabled users from
        # re-enabling their accounts).
        if user.state != "no_agreement":
            raise BadRequest("You have already agreed to the terms.")
        # Write state "pending" to ensure we don't try this again
        user.state = u"pending"
    except:
        req.store.rollback()
        raise
    req.store.commit()

    # Get the arguments for usermgt.activate_user from the session
    # (The user must have already logged in to use this app)
    args = {
        "login": user.login,
    }
    msg = {'activate_user': args}

    # Release our lock on the db so usrmgt can write
    req.store.rollback()

    # Try and contact the usrmgt server
    try:
        response = chat.chat(req.config['usrmgt']['host'],
                             req.config['usrmgt']['port'],
                             msg,
                             req.config['usrmgt']['magic'],
                            )
    except ValueError:
        # Gave back rubbish - set the response to failure
        response = {'response': 'usrmgt-failure'}

    # Get the staus of the users request
    try:
        status = response['response']
    except KeyError:
        status = 'failure'

    if status == 'okay':
        user.state = u"enabled"
    else:
        # Reset the user back to no agreement
        user.state = u"no_agreement"

    # Write the response
    req.content_type = "text/plain"
    req.write(json.dumps(response))
Example #2
0
 def authorize(self, req):
     # XXX: activate_me isn't called by a valid user, so is special for now.
     if req.path == 'activate_me' and get_user_details(req) is not None:
         return True
     return req.user is not None