implement UserService

This commit is contained in:
David Rodenkirchen
2024-08-19 10:38:57 +02:00
parent 49602854e2
commit 8b87d78d5d
4 changed files with 77 additions and 6 deletions
@@ -8,7 +8,7 @@ from src.ez_lan_manager.types.ConfigurationTypes import DatabaseConfiguration
logger = logging.getLogger(__name__.split(".")[-1])
class ConfigurationService:
def __init__(self, config_file_path: Path):
def __init__(self, config_file_path: Path) -> None:
try:
with open(config_file_path, "rb") as config_file:
self._config = tomllib.load(config_file)
+20 -4
View File
@@ -14,7 +14,7 @@ class DuplicationError(Exception):
pass
class DatabaseService:
def __init__(self, database_config: DatabaseConfiguration):
def __init__(self, database_config: DatabaseConfiguration) -> None:
self._database_config = database_config
try:
logger.info(
@@ -69,9 +69,9 @@ class DatabaseService:
return
return self._map_db_result_to_user(result)
def get_user_by_main(self, user_mail: str) -> Optional[User]:
def get_user_by_mail(self, user_mail: str) -> Optional[User]:
cursor = self._get_cursor()
cursor.execute("SELECT * FROM users WHERE user_mail=?", (user_mail,))
cursor.execute("SELECT * FROM users WHERE user_mail=?", (user_mail.lower(),))
result = cursor.fetchone()
if not result:
return
@@ -82,7 +82,7 @@ class DatabaseService:
try:
cursor.execute(
"INSERT INTO users (user_name, user_mail, user_password) "
"VALUES (?, ?, ?)", (user_name, user_mail, password_hash)
"VALUES (?, ?, ?)", (user_name, user_mail.lower(), password_hash)
)
self._connection.commit()
except mariadb.IntegrityError as e:
@@ -90,3 +90,19 @@ class DatabaseService:
raise DuplicationError
return self.get_user_by_name(user_name)
def update_user(self, user: User) -> User:
cursor = self._get_cursor()
try:
cursor.execute(
"UPDATE users SET user_name=?, user_mail=?, user_password=?, user_first_name=?, user_last_name=?, user_birth_date=?, "
"is_active=?, is_team_member=?, is_admin=?, balance=? WHERE (user_id=?)", (user.user_name, user.user_mail.lower(), user.user_password,
user.user_first_name, user.user_last_name, user.user_birth_day,
user.is_active, user.is_team_member, user.is_admin,
user.balance, user.user_id)
)
self._connection.commit()
except mariadb.IntegrityError as e:
logger.warning(f"Aborted duplication entry: {e}")
raise DuplicationError
return user
@@ -0,0 +1,43 @@
from hashlib import sha256
from typing import Union, Optional
from string import ascii_letters, digits
from src.ez_lan_manager.services.DatabaseService import DatabaseService
from src.ez_lan_manager.types.User import User
class NameNotAllowedError(Exception):
def __init__(self, disallowed_char: str) -> None:
self.disallowed_char = disallowed_char
class UserService:
ALLOWED_USER_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~"
def __init__(self, db_service: DatabaseService) -> None:
self._db_service = db_service
def get_user(self, accessor: Union[str, int]) -> User:
if isinstance(accessor, int):
return self._db_service.get_user_by_id(accessor)
if "@" in accessor:
return self._db_service.get_user_by_mail(accessor)
return self._db_service.get_user_by_name(accessor)
def create_user(self, user_name: str, user_mail: str, password_clear_text: str) -> User:
disallowed_char = self._check_for_disallowed_char(user_name)
if disallowed_char:
raise NameNotAllowedError(disallowed_char)
hashed_pw = sha256(password_clear_text.encode(encoding="utf-8")).hexdigest()
return self._db_service.create_user(user_name, user_mail, hashed_pw)
def update_user(self, user: User) -> User:
disallowed_char = self._check_for_disallowed_char(user.user_name)
if disallowed_char:
raise NameNotAllowedError(disallowed_char)
return self._db_service.update_user(user)
def _check_for_disallowed_char(self, name: str) -> Optional[str]:
for c in name:
if c not in self.ALLOWED_USER_NAME_SYMBOLS:
return c