summer_course_2024/hub/persistence/repositories/user.py

113 lines
3.8 KiB
Python
Raw Normal View History

"""
City repository with database CRUD operations
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
2023-02-01 06:05:12 -05:00
from hub.persistence import Repository
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import select
2023-02-01 06:05:12 -05:00
from hub.persistence.models import User as Model
2023-01-24 10:51:50 -05:00
from hub.persistence.models import UserRoles
from hub.helpers.auth import Auth
from typing import Union, Dict
2023-01-24 10:51:50 -05:00
from hub.hub_logger import logger
import datetime
2023-02-01 06:05:12 -05:00
class User(Repository):
_instance = None
def __init__(self, db_name: str, dotenv_path: str, app_env: str):
super().__init__(db_name, dotenv_path, app_env)
def __new__(cls, db_name, dotenv_path, app_env):
"""
Implemented for a singleton pattern
"""
if cls._instance is None:
2023-02-01 06:05:12 -05:00
cls._instance = super(User, cls).__new__(cls)
return cls._instance
2023-02-01 06:05:12 -05:00
def insert(self, name: str, password: str, role: UserRoles, application_id: int) -> Union[Model, Dict]:
2023-01-31 13:11:39 -05:00
user = self.get_by_name_and_application(name, application_id)
if user is None:
try:
2023-02-01 06:05:12 -05:00
user = Model(name=name, password=Auth.hash_password(password), role=role, application_id=application_id)
2023-01-31 13:11:39 -05:00
self.session.add(user)
self.session.flush()
self.session.commit()
return user
except SQLAlchemyError as err:
logger.error(f'An error occurred while creating user: {err}')
else:
2023-02-01 06:05:12 -05:00
return {'message': f'user {name} already exists for that application'}
2023-02-01 06:05:12 -05:00
def update(self, user_id: int, name: str, password: str, role: UserRoles) -> Union[Dict, None]:
"""
Updates a user
:param user_id: the id of the user to be updated
:param name: the name of the user
:param password: the password of the user
:param role: the role of the user
:return:
"""
try:
2023-02-01 06:05:12 -05:00
self.session.query(Model).filter(Model.id == user_id).update({
'name': name,
'password': Auth.hash_password(password),
'role': role,
'updated': datetime.datetime.utcnow()
})
self.session.commit()
except SQLAlchemyError as err:
2022-12-14 17:49:29 -05:00
logger.error(f'Error while updating user: {err}')
return {'err_msg': 'Error occurred while updated user'}
2023-02-01 06:05:12 -05:00
def get_by_name_and_application(self, name: str, application_id: int) -> [Model]:
"""
Fetch user based on the email address
2023-02-01 06:05:12 -05:00
:param name: User name
:param application_id: User application name
:return: [User] matching the search criteria
"""
try:
2023-02-01 06:05:12 -05:00
return self.session.execute(
select(Model).where(Model.name == name and Model.application_id == application_id)
).first()
except SQLAlchemyError as err:
2023-02-01 06:05:12 -05:00
logger.error(f'Error while fetching user by name and application: {err}')
def delete_user(self, user_id: int):
"""
Deletes a user with the id
:param user_id: the user id
:return: None
"""
try:
2023-02-01 06:05:12 -05:00
self.session.query(Model).filter(Model.id == user_id).delete()
self.session.commit()
except SQLAlchemyError as err:
2022-12-14 17:49:29 -05:00
logger.error(f'Error while fetching user: {err}')
2023-02-01 06:05:12 -05:00
def get_user_by_name_application_and_password(self, name: str, password: str, application_id: int) -> [Model]:
"""
Fetch user based on the email and password
2023-02-01 06:05:12 -05:00
:param name: User name
:param password: User password
:param application_id: User password
:return: [User] with the provided email and password
"""
try:
2023-02-01 06:05:12 -05:00
user = self.session.execute(
select(Model).where(Model.name == name and Model.application_id == application_id)
).first()
if user:
if Auth.check_password(password, user[0].password):
return user
2023-02-01 06:05:12 -05:00
return {'message': 'invalid login information'}
except SQLAlchemyError as err:
2022-12-14 17:49:29 -05:00
logger.error(f'Error while fetching user by email: {err}')