def reset(request): """Once deactivated, allow for changing the password via activation key""" rdict = request.matchdict params = request.params message = '' # This is an initial request to show the activation form. username = rdict.get('username', None) activation_key = rdict.get('reset_key', None) user = ActivationMgr.get_user(username, activation_key) if user is None: # just 404 if we don't have an activation code for this user raise HTTPNotFound() if 'code' in params: # This is a posted form with the activation, attempt to unlock the # user's account. username = params.get('username', None) activation = params.get('code', None) password1 = params.get('password1', None) password2 = params.get('password2', None) new_username = params.get('new_username', None) res = ActivationMgr.activate_user(username, activation, password1) if res: # success so respond nicely AuthLog.reactivate(username, success=True, code=activation) # if there's a new username and it's not the same as our current # username, update it if new_username and new_username != username: try: user = UserMgr.get(username=username) user.username = new_username except IntegrityError, exc: message = 'There was an issue setting your new username. Please try again.' else: AuthLog.reactivate(username, success=False, code=activation) message = 'There was an issue attempting to activate this account.' if message is not '': return { 'message': message, 'user': user } else: # log the user out to have them re-login with the new password headers = forget(request) return HTTPFound(location=route_url('login', request), headers=headers)
def reset(request): """Once deactivated, allow for changing the password via activation key""" rdict = request.matchdict params = request.params # This is an initial request to show the activation form. username = rdict.get('username', None) activation_key = rdict.get('reset_key', None) user = ActivationMgr.get_user(username, activation_key) if user is None: # just 404 if we don't have an activation code for this user raise HTTPNotFound() if 'code' in params: # This is a posted form with the activation, attempt to unlock the # user's account. username = params.get('username', None) activation = params.get('code', None) password = params.get('new_password', None) new_username = params.get('new_username', None) error = None if not UserMgr.acceptable_password(password): # Set an error message to the template. error = "Come on, pick a real password please." else: res = ActivationMgr.activate_user(username, activation, password) if res: # success so respond nicely AuthLog.reactivate(username, success=True, code=activation) # if there's a new username and it's not the same as our current # username, update it if new_username and new_username != username: try: user = UserMgr.get(username=username) user.username = new_username except IntegrityError, exc: error = 'There was an issue setting your new username' else: AuthLog.reactivate(username, success=False, code=activation) error = 'There was an issue attempting to activate this account.'
def account_activate(request): """Reset a user after being suspended :param username: required to know what user we're resetting :param activation: code needed to activate :param password: new password to use for the user """ params = request.params username = params.get('username', None) activation = params.get('code', None) password = params.get('password', None) new_username = params.get('new_username', None) if username is None and activation is None and password is None: # then try to get the same fields out of a json body json_body = request.json_body username = json_body.get('username', None) activation = json_body.get('code', None) password = json_body.get('password', None) new_username = json_body.get('new_username', None) if not UserMgr.acceptable_password(password): request.response.status_int = 406 return _api_response(request, { 'error': "Come on, pick a real password please", }) res = ActivationMgr.activate_user(username, activation, password) if res: # success so respond nicely AuthLog.reactivate(username, success=True, code=activation) # if there's a new username and it's not the same as our current # username, update it if new_username and new_username != username: try: user = UserMgr.get(username=username) user.username = new_username except IntegrityError, exc: request.response.status_int = 500 return _api_response(request, { 'error': 'There was an issue setting your new username', 'exc': str(exc) }) return _api_response(request, { 'message': "Account activated, please log in.", 'username': username, })