def user_operations(): args = arg_parser() if args.username and args.password and not args.edit and not args.delete and not args.send and not args.list: create_user(args.username[0], args.password[0]) if args.username and args.password and args.edit: user = check_user(args.username[0]) if args.new_pass: if checked(user.hashed_password): salt = generate_salt() user.set_password(args.new_pass[0], salt) user.save_to_db(cursor) else: print("Podaj prawidłowe hasło!") else: print("Podaj nowe hasło w parametrze -n!") if args.username and args.password and args.delete: user = check_user(args.username[0]) if checked(user.hashed_password): user.delete(cursor) else: print("Podaj prawidłowe hasło!") if args.list and not args.username and not args.password: load_users = User.load_all_users(cursor) user_table = "_ID_|__Username__|__Email__\n" for user in load_users: user_table += " {} | {} | {} \n".format(user.id, user.username, user.email) print(user_table)
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 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 add_new_user(args, cursor): new_user = User.find_by_email(cursor, args.username) if not new_user: new_user = User() new_user.email = args.username new_user.username = args.username new_user.set_password(args.password, generate_salt()) new_user.save_to_db(cursor) else: raise Exception('User exists')
def change_password(new_pass): # options['new_pass'] if password_atleast_8(new_pass): dict_pass_salt = {'password': new_pass, 'salt': generate_salt()} userX.hashed_password = dict_pass_salt userX.save_to_db(curs) print('password changed') else: print( argparse.ArgumentTypeError( 'New Password too short. Eight marks required')) # !
def set_password(self, password): '''This method allows us to set password of current object user, lenght of the password must be greater or equal than 8 signs.''' while True: if len(password) >= 8: self.__hashed_password = password_hash(password, generate_salt()) break else: password = input( "Password must have 8 signs or more enter another one: ")
def change_password(self, user: User, new_password: str) -> None: password_validator(new_password) if user.check_password(new_password): raise Exception("given password is the same as the previous one") connection = try_connect_db() cursor = get_cursor(connection) salt = generate_salt() user.set_password(new_password, salt) user.save(cursor) cursor.close() connection.close() return True
def create_user(username, password): if check_user(username) is False: new_username = re.search(r'[A-Z0-9a-z._%+-]+@', username) if new_username: salt = generate_salt() new_user = User() new_user.username = new_username.group(0)[:-1] new_user.email = username new_user.set_password(password, salt) new_user.save_to_db(cursor) else: print("Przy zakładaniu konta wprowadź email jako username!") else: print(username, "już istnieje! Dodaj dodatkowe parametry")
def create_user(username, password): # options['username'] options['password'] print("User {} doesn't exist in database.".format(username)) print('Create new User:'******'Enter new user email: ') u1 = User() dict_pass_salt = {'password': password, 'salt': generate_salt()} u1.username = username u1.hashed_password = dict_pass_salt if email_check(email): u1.email = email u1.save_to_db(curs) # print(u1.id) print(' User added to database.') else: print(argparse.ArgumentTypeError('Wrong email!'))
def create_user(self, new_username: str, password: str) -> User: if not email_validator(new_username): return False password_validator(password) connection = try_connect_db() cursor = get_cursor(connection) salt = generate_salt() user = User() user.email = new_username user.set_password(password, salt) try: user.save(cursor) cursor.close() connection.close() return True except Exception as err: print( 'Data not saved to database due to the error: {}'.format(err)) cursor.close() connection.close() return False
# Jeśli ID = -1 to znaczy że obiekt jest stworzony poprzez kod i nie istnieje jego odpowiednik w bazie danych self._create_record_db(cursor) return True def load_messages_to(cls, cursor, to_id): # Wczytanie danych z bazy danych poprzez email sql = "SELECT * FROM messages WHERE to_id=%s" cursor.execute(sql, (to_id, )) record = cursor.fetchone() if record: return cls._create_object(**record) # zwrócenie obiektu return None if __name__ == '__main__': salt = generate_salt() connection = create_connection(db_name="war2") cursor = get_cursor(connection) user1 = User() user1.username = '******' user1.email = '*****@*****.**' user1.set_password('pass', salt) user1.save(cursor) user2 = User() user2.username = '******' user2.email = '*****@*****.**' user2.set_password('pass', salt) user2.save(cursor)
ret.append(loaded_user) return ret def delete(self, cursor): sql = "DELETE FROM Users WHERE id=%s" cursor.execute(sql, (self.__id,)) self.__id = -1 return True if __name__ == "__main__": hela = Users() hela.username = "******" hela.email = "*****@*****.**" hela.set_password("Helenk@2018", generate_salt()) cnx = get_connection() cursor = cnx.cursor() # stworzyc kursor, który tworzy słownik i wyświetla alternatywa dla wyświetlania jednego atrybutu poniżej a = Users.load_all_users(cursor) b = [] for user in a: b.append(user.username) print(b) ### delete object # a = Users.load_user_by_id(cursor, 2) # a.delete(cursor) cursor.close()
def test_salt(self): self.assertEqual(len(generate_salt()), 16) self.assertNotIn('?', generate_salt()) self.assertNotIn(',', generate_salt())
def set_password(self, password): salt = generate_salt() self.__hashed_password = password_hash(password, salt)
if len(password) < 8: print('Za krótkie hasło') else: if login and password and not (edit or delete): cnx = get_connection() cursor = cnx.cursor() a = models.Users.load_all_users(cursor) b = [] for user in a: b.append(user.username) if login in b: print("Użytkownik o podanym loginie już istnieje") else: new_user = models.Users() new_user.username = login new_user.set_password(password, generate_salt()) while True: try: new_user.email = input("Podaj e-mail: ") new_user.save_to_db(cursor) print("Nowy użytkownik założony") break except psycopg2.IntegrityError: print("Podany e-mail już istnieje") new_user.email = input("Podaj inny e-mail: ") elif login and password and edit and new_pass: cnx = get_connection() cursor = cnx.cursor() sql = f"select id from users where username = '******';" cursor.execute(sql) id = cursor.fetchone()[0]