def test_update_user(user1): """ Updates attribute of user. Returns nothing """ user_info = data.get_user_info(user1["u_id"]) data.update_user(user_info,{"name_last":"Hang"}) assert data.get_user_with({"name_last": "Hang"}) == user_info #data.users[1] other.clear()
def post(self): secret = self.request.get('secret') nickname = self.request.get('nickname') user = users.get_current_user() uid = user.user_id() try: data.get_user_by_id(uid) data.update_user(uid, secret, nickname) except data.UserException: data.add_user(uid, user.email(), secret, nickname) self.redirect('/list')
def generate_reset_code(user): """ Generates a reset code to email to a user Parameters: user(dict): Dictionary containing information about the user Returns A hashed code that will allow the user to reset their password """ number = random.randrange(100000, 1000000) code = user['name_first'][0] + str(number) + user['name_first'][-1] hashed_code = hashlib.sha256(code.encode()).hexdigest() data.update_user(user, {'reset_code' : hashed_code}) return hashed_code
def update_users(db, account): for message in account.inbox.unread(): if message.subject == 'update settings': try: config = json.loads(message.body) config['username'] = message.author.name data.update_user(config, db) subreddit_notifications.log(config) message.mark_read() except: subreddit_notifications.log( "An error occured: Invalid Configuration") message.reply("An error occured: Invalid Configuration") message.mark_read() else: message.mark_read()
def auth_login(email, password): """ Attempts to log user in by checking whether eamil and password match Parameters: email(string): Email given by user password(string): Password given by user Returns: Dictionary with user"s token and u_id (if successful) """ # Convert email to lowercase new_email = email.lower() # Checks to determine whether email and password are correct validation.check_correct_email(new_email) # Check if supplied password matches the email validation.check_correct_password(new_email, password) # Everything is valid # User has definitely registered. Password is correct # There is at least one user in data.data["users"] user = data.get_user_with({ "email" : new_email }) # Update global state. # Adds user to data["logged_in"]. data.login_user(user["u_id"]) # Gives user a new random number for token validation. data.update_user(data.get_user_info(user["u_id"]), {"session_secret" : random.randrange(100000), "reset_code" : None}) payload = { "u_id" : user["u_id"], "session_secret" : user["session_secret"] } token = str(jwt.encode(payload, data.get_jwt_secret(), algorithm = "HS256")) return { "u_id" : user["u_id"], "token": token[2:-1] }
def user_profile_setemail(token, email): """ Check whether use is valid and for a valid user, update their handle Parameters: token(string): An authorisation hash email(string): New email Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid email validation.check_valid_email(email.lower()) #Everything valid, proceed with changing email user = data.get_user_info(u_id) data.update_user(user, {"email": email.lower()}) return {}
def user_profile_setname(token, name_first, name_last): """ Check whether use is valid and for a valid user, update their name Parameters: token(string): An authorisation hash name_first(string): new first name name_last(stirng): new last name Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid name validation.check_valid_name(name_first, name_last) #Everything valid, proceed with changing name user = data.get_user_info(u_id) data.update_user(user, {"name_first": name_first, "name_last": name_last}) return {}
def auth_logout(token): """ Logs the user out after checking that they are originally logged in Parameters: token(string): An authorisation hash Returns: Dictionary with a boolean that depends on whether user can be successfully logged out """ # Check if token is valid. try: u_id = validation.check_valid_token(token) except AccessError: return {"is_success" : False} # Check if user is active (logged in). data.logout_user(u_id) data.update_user(data.get_user_info(u_id), {"session_secret" : ""}) return {"is_success" : True}
def auth_passwordreset_reset(reset_code, new_password): """ Given the correct code, will reset user's password Parameters: reset_code(str): A hash used for resetting a user's password new_password(str): User's new password Returns: An empty dictionary """ # Check reset_code is valid (belongs to a user) user = validation.check_valid_reset_code(reset_code) # Check new password is valid validation.check_valid_password(new_password) data.update_user(user, { 'reset_code' : None, 'password' : hashlib.sha256(new_password.encode()).hexdigest() }) return {}
def user_profile_sethandle(token, handle_str): """ Check whether use is valid and for a valid user, update their handle Parameters: token(string): An authorisation hash handle_str(string): New Handle Returns: Nothing """ #Check for valid token u_id = validation.check_valid_token(token) #Check for valid handle validation.check_valid_handle(handle_str) #Check for existing handle validation.check_existing_handle(handle_str) #Everything valid, proceed with changing handle user = data.get_user_info(u_id) data.update_user(user, {"handle_str": handle_str}) return {}