from simple_chat_api.db_handler.db_handler import DbConnector from simple_chat_api.config import hash_context class UserManager: """ Class that handles authentication with database backend """ def __init__(self, db_con: DbConnector): self.db_con = db_con def create_user(self, username: str, password: str) -> bool: """ Create a new user in the database :param username: logon name :param password: password to save :return: True if user was created """ try: self.db_con.add_user(username, password) except: return False return True def authenticate(self, username: str, password: str) -> bool: """ Authenticate against the database :param username: username to check :param password: password to check :return: True if authenticated """ user = self.db_con.get_user(username) if not user or not hash_context.verify(password, user.hash): return False return True def delete_user(self, username: str) -> bool: """ Delete a user from the database :param username: username to delete :return: True on success """ try: self.db_con.delete_user(username) except: return False return True def change_user_password(self, username: str, password: str) -> bool: """ Change the password for a user :param username: username to change :param password: new password :return: True on success """ try: self.db_con.change_password(username, hash_context.hash(password)) except: return False return True def check_user_exists(self, username: str) -> bool: """ Check if a user exists in the database :param username: logon name to look for :return: True if user exists """ if self.db_con.get_user(username): return True return False