def message_remove(token, message_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # find message through channel/list, then channel messages to find the correct message i = 0 while i < len(data["channels"]): j = 0 if u_id in data["channels"][i]["membersUID"]: while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if (u_id not in data["channels"][i]['ownersUID']) and \ (u_id not in data["channels"][i]["messages"][j]['u_id']): raise ValueError( "Neither the original poster of this message or an owner") del data["channels"][i]["messages"][j] storage.dump_data_store(data) return j += 1 i += 1 raise errors.AccessError("Message_id isn't in any channel you have access too") pass
def message_unpin(token, message_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # if the token owner is an owner or an admin (p_id 1 and 2) # (search through all users for permission level) # look through all channels the user is apart of #print(channels['channels'][i]['channel_id']) i = 0 while i < len(data["channels"]): j = 0 while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if u_id in data["channels"][i]["ownersUID"]: if data["channels"][i]["messages"][j]["is_pinned"] == False: raise ValueError( "This message is already pinned") data["channels"][i]["messages"][j]["is_pinned"] = False storage.dump_data_store(data) return if u_id in data["channels"][i]["membersUID"]: raise ValueError("User isn't an admin") raise errors.AccessError("User isn't a member of the channel the message is in") j += 1 i += 1 raise ValueError("message_id isn't a valid message") pass
def message_send(token, channel_id, text): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # check the message <= 1000 characters if len(text) > 1000: raise ValueError("Message is more than 1000 characters") # check if user is an admin, owner or a member of the channel channel_deats = channel_utils.channel_details(token, channel_id) if u_id not in channel_deats['all_members']: raise errors.AccessError( "The user isn't authorised to post in this channel") # generate a message_id, and post it message = message_create(u_id, text, datetime.now()) # put the message onto a channel for channel in data['channels']: if channel['channelID'] == channel_id: break channel['messages'].insert(0, message) storage.dump_data_store(data) return message['message_id']
def message_unreact(token, message_id, react_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # Valid react_id (Only 1 is currently valid) if react_id != 1: raise ValueError("not a valid react_id") # search through channel/list, then channel messages to find the correct message i = 0 #print(channels['channels'][i]['channel_id']) while i < len(data["channels"]): j = 0 if u_id in data["channels"][i]["membersUID"]: while j < len(data["channels"][i]["messages"]): if message_id == data["channels"][i]["messages"][j]["message_id"]: if data["channels"][i]["messages"][j]["reacts"][0]['is_this_user_reacted'] == False: raise ValueError( "You haven't reacted to this message with this reaction") data["channels"][i]["messages"][j]["reacts"][0]['is_this_user_reacted'] = False data["channels"][i]["messages"][j]["reacts"][0]['u_ids'].remove(u_id) storage.dump_data_store(data) return j += 1 i += 1 # check react id's raise ValueError("Not a valid message in a channel that the user is apart of")
def message_sendlater(token, channel_id, message, time_sent): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] # check the message <= 1000 characters if len(message) > 1000: raise ValueError("Message is more than 1000 characters.") # check if user is an admin, owner or a member of the channel channel_deats = channel_utils.channel_details(token, channel_id) if u_id not in channel_deats['all_members']: raise errors.AccessError( "The user isn't authorised to post in this channel.") # time in the future if time_sent < datetime.now(): raise ValueError("Time sent is in the past.") # generate a message_id, and set to post at correct time message = message_create(u_id, message, time_sent) # put the message into the channel for channel in data['channels']: if channel['channelID'] == channel_id: break channel['messages'].insert(0, message) # HOW TO DO TIME? storage.dump_data_store(data) return message['message_id']
def auth_login(email, password): """ Given a registered users' email and password and generates a valid token for the user to remain authenticated """ if not is_valid_email(email): raise ValueError("Invalid email given") data = get_auth_store() users = data["users"] valid_tokens = data["tokens"] new_token = None u_id = None for user in users: if user["email"] == email: if user["hashed_password"] == hash_password(password): u_id = user["u_id"] new_token = generate_token(u_id) valid_tokens[user["email"]] = new_token else: raise ValueError("Gave incorrect password for given email") # Couldn't find user if new_token is None: raise ValueError("Couldn't find a user with that email") storage.dump_data_store(data) return {"u_id": u_id, "token": new_token}
def standup_start(token, channel_id): data = channel_utils.get_data() # Check valid token if authentication.decode_token(token) == None: raise ValueError("Invalid token provided.") u_id = authentication.decode_token(token)["u_id"] i = 0 while i < len(data["channels"]): if data["channels"][i]["channelID"] == channel_id: # check if currently running a stand_up if data["channels"][i]["standup_time"] >= datetime.datetime.now(): raise ValueError("This channel already has a standup started") elif u_id not in data["channels"][i]["membersUID"]: raise errors.AccessError( "This user isn't a member of the channel") else: # return time + 15 mins time = datetime.datetime.now() + datetime.timedelta(minutes=15) data["channels"][i]["standup_time"] = time storage.dump_data_store(data) return time i += 1 raise ValueError("Channel ID is not a valid channel")
def update_pickle(index, new_info): #user_data = get_user_data() user_data = storage.load_data_store() # delete the input given the index del user_data['users'][index] # add the new information for that user user_data['users'].append(new_info) # write to file storage.dump_data_store(user_data)
def auth_register(email, password, name_first, name_last): """ Given a user's first and last name, email address, and password, create a new account for them and return a new token for authentication in their session. A handle is generated that is the concatentation of a lowercase-only first name and last name. If the concatenation is longer than 20 characters, it is cutoff at 20 characters. If the handle is already taken, you may modify the handle in any way you see fit to make it unique. """ data = get_auth_store() users = data["users"] valid_tokens = data["tokens"] if not is_valid_first_name(name_first): raise ValueError("Gave an invalid first name") if not is_valid_last_name(name_last): raise ValueError("Gave an invalid last name") if not is_valid_password(password): raise ValueError("Gave an invalid password") if not is_valid_email(email): raise ValueError("Gave an invalid email") if is_registered_email(email, users): raise ValueError("Gave an already taken email") u_id = _generate_uid() permission_id = SlackrPermissions.MEMBER.value if not users: permission_id = SlackrPermissions.OWNER.value token = generate_token(u_id) handle_str = _generate_handle(name_first, name_last, users) data["users"].append({ "u_id": u_id, "handle_str": handle_str, "first_name": name_first, "last_name": name_last, "permission_id": permission_id, "email": email, "hashed_password": hash_password(password) }) valid_tokens[email] = token storage.dump_data_store(data) return {"u_id": u_id, "token": token}
def admin_userpermission_change(token, u_id, permission_id): """ Changes the permission level for a user """ # check for valid users decoded_token = authentication.decode_token(token) if decoded_token is None: raise errors.AccessError("Not a valid user") # check for valid permission_id if not permission_id in set(perm.value for perm in SlackrPermissions): raise ValueError("Not a valid permission_id") admin_id = decoded_token["u_id"] data = authentication.get_auth_store() user_to_change = None # Get user and admin levels for user_index, user in enumerate(data["users"]): if user["u_id"] == admin_id: admin = user if user["u_id"] == u_id: user_to_change = user_index # check if user exists if user_to_change is None: raise ValueError("Unknown user to modify") # check if modifying their own permissions (to avoid getting stuck with no one with permissions) if admin["u_id"] == data["users"][user_to_change]["u_id"]: raise ValueError("Can't modify own permissions") # check if level is either admin or owner if admin["permission_id"] not in VALID_ADMIN_LEVELS: raise errors.AccessError("Not a valid owner or admin") # check if is another owner if modifying an owner if (admin["permission_id"] != SlackrPermissions.OWNER.value and data["users"][user_to_change]["permission_id"] == SlackrPermissions.OWNER.value): raise errors.AccessError("Admins cannot change a owner's permission") data["users"][user_to_change]["permission_id"] = permission_id storage.dump_data_store(data) return True
def auth_logout(token): """ Given an active token, invalidates the taken to log the user out. If a valid token is given, and the user is successfully logged out, it returns true, otherwise false. """ data = get_auth_store() email_to_logout = None for email, valid_token in data["tokens"].items(): if token == valid_token: email_to_logout = email if email_to_logout is not None: data["tokens"].pop(email_to_logout, None) storage.dump_data_store(data) return True return False
def auth_passwordreset_request(email): """ Given an email address, if the user is a registered user, send's them a an email containing a specific secret code, that when entered in auth_passwordreset_reset, shows that the user trying to reset the password is the one who got sent this email. The Flask app must actually send the email - this function does the rest of the work of generating the reset code """ if not is_valid_email(email): raise ValueError("Gave an invalid email to send a password reset to") data = get_auth_store() reset_code = None for user in data["users"]: if user["email"] == email: reset_code = generate_reset_code() data["reset_codes"][reset_code] = email storage.dump_data_store(data) return reset_code
def auth_passwordreset_reset(reset_code, new_password): """ Given a reset code for a user, set that user's new password to the password provided """ if not is_valid_password(new_password): raise ValueError("Invalid password given") data = get_auth_store() email_to_reset = None if reset_code in data["reset_codes"]: email_to_reset = data["reset_codes"][reset_code] if email_to_reset is None: raise ValueError("No such reset code") for user in data["users"]: if user["email"] == email_to_reset: data["reset_codes"].pop(reset_code, None) user["hashed_password"] = hash_password(new_password) storage.dump_data_store(data) return new_password