def change_user_password(email, password, new_password): user = User.get_user_by_email(email) if user and check_password(password, user.hash_password) and len(new_password) > 8: user.set_password(new_password) user.save_to_db() raise NotImplemented
def send(): form = SendForm(request.form) if request.method == 'POST' and form.validate(): conn = get_connection() if conn: cursor = conn.cursor() sender_id = form.sender.data receiver_id = form.receiver.data sender = User.load_user_by_id(cursor, sender_id) if check_password(form.password.data, sender.hashed_password): message = Message() message.to_id = receiver_id message.from_id = sender_id message.text = form.message.data message.creation_date = datetime.today().strftime( "%Y-%m-%d %H:%M:%S") message.save_to_db(cursor) conn.commit() else: flash("Błąd hasła!") cursor.close() conn.close() return redirect('/user/{}'.format(sender_id)) return render_template('send_message.html', form=form)
def delete_me(args, cursor): user_to_delete = User.find_by_email(cursor, args.username) if user_to_delete and check_password(args.password, user_to_delete.hashed_password): user_to_delete.delete(cursor) else: raise Exception('Invalid login or password')
def solution(options): if options.username and options.password: err_msg = "" all_users = mysql_connection(User.load_all_users) test = [ True for user in all_users if options.username in user.username ] # register new user if not options.edit and not options.newpass and not options.delete: if test: err_msg += "Login has to be unique " if len(options.password) < 8: err_msg += "Password must be at least 8 characters long " if err_msg: print(err_msg) else: new_user = User() new_user.username = options.username new_user.set_email() new_user.set_password(options.password, None) # print(new_user.__dict__) mysql_connection(new_user.save_to_db) # handle existing user else: if not test: err_msg += "Login is incorrect " else: existing_user = mysql_connection(User.load_user_by_login, login=options.username) # check if password is correct if check_password(options.password, existing_user.hashed_password): # delete user if options.delete: mysql_connection(existing_user.delete) # edit user - incorrect data elif options.edit and not options.newpass: err_msg += "New password is required" print(err_msg) # edit user elif options.edit and options.newpass: if len(options.newpass) < 8: err_msg += "Password must be at least 8 characters long" print(err_msg) else: existing_user.set_password(options.newpass, None) mysql_connection(existing_user.save_to_db) else: err_msg += "Password is incorrect " print(err_msg) elif options.list: for user in mysql_connection(User.load_all_users): print(user.username, user.email, user.id)
def change_user_password(args, cursor): user_to_edit = User.find_by_email(cursor, args.username) if user_to_edit and check_password(args.password, user_to_edit.hashed_password): user_to_edit.set_password(args.new_pass, generate_salt()) user_to_edit.save_to_db(cursor) else: raise Exception('Złe hasło lub użyszkodnik nie istnieje')
def checked(hashed): args = arg_parser() pswd_to_check = args.password[0] check = check_password(pswd_to_check, hashed) if check: return True else: return False
def change_user_password(args, cursor): user_to_edit = User.find_by_email(cursor, args.username) if user_to_edit and check_password(args.password, user_to_edit.hashed_password): user_to_edit.set_password(args.new_pass, generate_salt()) user_to_edit.save_to_db(cursor) else: raise Exception('Invalid password or user doesn\'t exist')
def check_pass(username, password): u = m.User.load_user_by_name(cursor, username) if u is None: print('no such a user') return 'no such a user' if not cl.check_password(password, u._hashed_password): print('wrong password') return 'wrong password' return 0
def validate(database_connection, email, password): cursor = database_connection.cursor() cursor.execute("SELECT password FROM Users WHERE email=%s;", [email]) result = False if cursor.rowcount > 0: hashed_password = cursor.fetchone()[0] result = check_password(password, hashed_password) cursor.close() return result
def delete_user(cur, username, password): user = User.load_user_by_username(cur, username) if not user: print("User does not exist!") elif check_password(password, user.hashed_password): user.delete(cur) print("User deleted.") else: print("Incorrect password!")
def load_user_by_name_and_pass(cursor, user_name, user_pass): sql = "SELECT id, username, email, hashed_password FROM users WHERE username=%s" cursor.execute(sql, (user_name,)) # (user_id, ) - bo tworzymy krotkę data = cursor.fetchone() if data and check_password(user_pass, data[3]): loaded_user = User() loaded_user.__id = data[0] loaded_user.username = data[1] loaded_user.email = data[2] return loaded_user
def check_and_load_user(cursor, username, password): user_id = User.get_id(username, cursor) if user_id != -1: user = User.load_user_by_id(cursor, user_id) if check_password(password, user.hashed_password): return user else: return None else: return None
def edit_user(username, password, new_password): user = m.User.load_user_by_name(cursor, username) print('edit user check password ', cl.check_password(password, user.hashed_password)) if user is None: print('no such a user') return if not cl.check_password(password, user.hashed_password): print('password incorrect!, cannot edit') return if len(new_password) < 8: print('new password too short') return # that's the error here: # hashed_pass = cl.hash_password(args.password, None) # user.hashed_password = hashed_password # that's the correct version: user.hashed_password = new_password user.safe_to_db(cursor)
def delete_user(username, password): user = m.User.load_user_by_name(cursor, username) print('hashed password ', user) if user is None: print('no such a user to delete') return if not cl.check_password(password, user._hashed_password): print('password incorrect! can"t delete') return user.delete(cursor)
def delete_user(email, password): cnx = connection() cursor = cnx.cursor() user = User.get_user_by_email(cursor, email) if user and check_password(password, user.hashed_password): user.delete(cursor) cnx.commit() print("użytkownik usunięty") else: print("błąd usuwania") cursor.close() cnx.close()
def validate_data(self, username, password, cursor): '''Validation data by passing username and password, sql query checks if there is a user with password passed as an argument, if yes method returns True, otherwise-False''' sql = """ SELECT hashed_password from users WHERE username = %s """ cursor.execute(sql, (username, )) passw = cursor.fetchone() if passw != None: return check_password(password, passw[0]) return False
def change_user_password(email, password, new_password): cnx = connection() cursor = cnx.cursor() user = User.get_user_by_email(cursor, email) if user and check_password(password, user.hashed_password) and len(new_password) > 8: user.set_password(new_password) user.save_to_db(cursor) cnx.commit() print('hasło zmienione') else: print('błąd zmiany hasła') cursor.close() cnx.close()
def list_messages(cursor, username, password): user = User.load_user_by_name(cursor, username) if not user: print("User does not exist.") elif check_password(password, user.hashed_password): messages = Messages.load_all_messages(cursor, user.id) for message in messages: sender = User.load_user_by_id(cursor, message.from_id) print( f"From: {sender.username}\n{message.text}\nDate: {message.creation_date}\n-----------------------------------------------------------------------------" ) else: print("Incorrect password.")
def edit_user(cur, username, password, new_pass): user = User.load_user_by_username(cur, username) if not user: print("User does not exist") elif check_password(password, user.hashed_password): if len(new_pass) < 8: print("Password is to short") else: user.hashed_password = new_pass user.save_to_db(cur) print("Password changed") else: print("incorect password")
def edit_user(cursor, username, password, new_pass): user = User.load_user_by_name(cursor, username) if not user: print("User does not exist.") elif check_password(password, user.hashed_password): if len(new_pass) < 8: print("Password is too short, it should be at least 8 characters.") else: user.hashed_password = new_pass user.save_to_db(cursor) print("Password changed.") else: print("Incorrect password.")
def edit_user(cur, username, password, new_pass): user = User.load_user_by_username(cur, username) if not user: print("User does not exist!") elif check_password(password, user.hashed_password): if len(new_pass) < 8: print( "Password is tho short. It should have minimum 8 characters.") else: user.hashed_password = new_pass user.save_to_db(cur) print("Password changed.") else: print("Incorrect password")
def logging_user(user, password): """ Validates given password with user password saved in DB :param user: User class object :param password: password user in log in, string type. Passed through parser :return: True if password matches user password, otherwise False. Also False if user is None """ if not user: print('No such user in database') return False elif not check_password(password, user.hashed_password): return False return True
def delete_user(cur, username, password, delete): try: del_user = User.load_user_by_username(cur, username) if del_user is None: raise NoData else: if check_password(password, del_user.hashed_password) and delete is True: del_user.delete(cur) print(f'"{username}" has been deleted') else: raise IncorrectPasswordError except NoData: print(f'no user "{username}"') except IncorrectPasswordError as e: print(e)
def delete_user(email, password): """ Usunięcie użytkownika :param email: email :param password: hasło :return: None """ try: cnx = create_connection() cursor = cnx.cursor() user = User.get_user_by_email(cursor, email) if user and check_password(password, user.hashed_password): user.delete(cursor) cnx.commit() cursor.close() cnx.close() except OperationalError: print("Problem z połączeniem do bazy danych")
def send_message(cur, username, password, to, send): try: user = User.load_user_by_username(cur, username) if user is None: print(f'no user "{username}"') else: if check_password(password, user.hashed_password): recipient = User.load_user_by_username(cur, to) if recipient is None: print(f'recipient "{to}" does not exist') else: msg = Message(user.id, recipient.id, send) msg.safe_to_db(cur) print('Message send') else: raise IncorrectPasswordError except IncorrectPasswordError as e: print(e)
def solution(options): if options.username and options.password: err_msg = "" all_users = mysql_connection(User.load_all_users) test = [True for user in all_users if options.username in user.username] if not test: err_msg += "Login is incorrect " print(err_msg) else: usr = mysql_connection(User.load_user_by_login, login=options.username) if check_password(options.password, usr.hashed_password): # send message -t, -s if options.to and options.send: # check if to_usr exists in db to_usr = mysql_connection(User.load_user_by_login, login=options.to) if not to_usr: err_msg += "Unknown recipient " print(err_msg) return False if options.send == "": err_msg += "Message can't be empty " print(err_msg) return False # set new message msg = Message() msg.set_date() msg.from_usr = usr.id msg.to_usr = to_usr.id msg.text = options.send mysql_connection(msg.save_to_db) print("Message sent") # list message to user -l elif options.list: for msg in mysql_connection(Message.load_all_msg_for_user, usr.id): print("to: %s \ndate: %s \nmessage: %s " % (usr.username, msg.date, msg.text)) else: err_msg += "Password is incorrect " print(err_msg)
def send_message(cursor, username, password, to_user, content): user = User.load_user_by_name(cursor, username) if not user: print("User does not exist.") elif check_password(password, user.hashed_password): user2 = User.load_user_by_name(cursor, to_user) if not user2: print("That user does not exist.") else: if len(content) < 255: message = Messages(from_id=user.id, to_id=user2.id, text=content) message.save_to_db(cursor) print("Message send.") else: print("Your message is too long, maximum 254 characters.") else: print("Incorrect password.")
def change_user_password(email, password, new_password): """ Zmiana hasła użytkownika :param email: email :param password: stare hasło :param new_password: nowe hasło :return: """ try: cnx = create_connection() cursor = cnx.cursor() user = User.get_user_by_email(cursor, email) if user and check_password( password, user.hashed_password) and len(new_password) > 8: user.set_password(new_password) user.save_to_db(cursor) cnx.commit() cursor.close() cnx.close() except OperationalError: print("Problem z połączeniem do bazy danych")
def edit_user(cur, username, password, edit, new_pass): try: edited_user = User.load_user_by_username(cur, username) if edited_user is None: raise NoData else: if check_password(password, edited_user.hashed_password) and edit is True: if len(new_pass) >= 8: edited_user.hashed_password = new_pass edited_user.safe_to_db(cur) print('password has been changed') else: raise TooShortPasswordError else: raise IncorrectPasswordError except NoData: print(f'no user "{username}"') except IncorrectPasswordError as e: print(e) except TooShortPasswordError as er: print(er)
def messages_list(cur, lst, username, password): if lst is True: try: user = User.load_user_by_username(cur, username) if user is None: raise NoData else: if check_password(password, user.hashed_password): messages_ = Message.load_all_messages(cur, user.id) if len(messages_) == 0: print("no messages") else: print(f"{username}'s INBOX:\n") for msg in messages_: print( f'message from: {User.load_user_by_id(cur, msg.from_id).username}\n' f'sent on: {msg.creation_date:%y-%m-%d %H:%M:%S}\ncontent: {msg.text}\n' ) else: raise IncorrectPasswordError except NoData: print(f'no user "{username}"') except IncorrectPasswordError as e: print(e)