2022-12-08 20:54:47 -05:00
|
|
|
"""
|
2023-02-01 07:29:06 -05:00
|
|
|
User repository with database CRUD operations
|
2022-12-08 20:54:47 -05:00
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Concordia CERC group
|
|
|
|
Project Coder Peter Yefi peteryefi@gmail.com
|
|
|
|
"""
|
2023-05-18 12:29:28 -04:00
|
|
|
import datetime
|
2023-05-19 13:15:40 -04:00
|
|
|
import logging
|
|
|
|
|
2022-12-08 20:54:47 -05:00
|
|
|
from sqlalchemy import select
|
2023-05-19 13:15:40 -04:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2023-05-18 12:29:28 -04:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
from hub.helpers.auth import Auth
|
|
|
|
from hub.persistence.repository import Repository
|
|
|
|
from hub.persistence.models import User as Model, Application as ApplicationModel, UserRoles
|
2022-12-08 20:54:47 -05:00
|
|
|
|
|
|
|
|
2023-02-01 06:05:12 -05:00
|
|
|
class User(Repository):
|
2023-05-18 12:29:28 -04:00
|
|
|
"""
|
|
|
|
User class
|
|
|
|
"""
|
2022-12-08 20:54:47 -05:00
|
|
|
_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)
|
2022-12-08 20:54:47 -05:00
|
|
|
return cls._instance
|
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def insert(self, name: str, password: str, role: UserRoles, application_id: int):
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
|
|
|
Inserts a new user
|
2023-05-17 17:10:30 -04:00
|
|
|
:param name: username
|
2023-02-01 07:29:06 -05:00
|
|
|
:param password: user password
|
|
|
|
:param role: user rol [Admin or Hub_Reader]
|
|
|
|
:param application_id: user application id
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: Identity id
|
2023-02-01 07:29:06 -05:00
|
|
|
"""
|
2023-05-19 13:15:40 -04:00
|
|
|
try:
|
|
|
|
user = self.get_by_name_and_application(name, application_id)
|
|
|
|
if user is not None:
|
|
|
|
raise SQLAlchemyError(f'A user named {user.name} already exists for that application')
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
user = Model(name=name, password=Auth.hash_password(password), role=role, application_id=application_id)
|
|
|
|
self.session.add(user)
|
|
|
|
self.session.flush()
|
|
|
|
self.session.commit()
|
|
|
|
self.session.refresh(user)
|
|
|
|
return user.id
|
|
|
|
except SQLAlchemyError as err:
|
|
|
|
logging.error('An error occurred while creating user %s', err)
|
|
|
|
raise SQLAlchemyError from err
|
2022-12-08 20:54:47 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def update(self, user_id: int, name: str, password: str, role: UserRoles):
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
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
|
2023-05-19 13:15:40 -04:00
|
|
|
:return: None
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
})
|
2022-12-08 20:54:47 -05:00
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while updating user: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2022-12-08 20:54:47 -05:00
|
|
|
|
2023-02-01 07:29:06 -05:00
|
|
|
def delete(self, user_id: int):
|
|
|
|
"""
|
|
|
|
Deletes a user with the id
|
|
|
|
:param user_id: the user id
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.session.query(Model).filter(Model.id == user_id).delete()
|
|
|
|
self.session.commit()
|
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching user: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
2023-02-01 07:29:06 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def get_by_name_and_application(self, name: str, application_id: int) -> Model:
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
Fetch user based on the email address
|
2023-05-17 17:10:30 -04:00
|
|
|
:param name: Username
|
2023-02-01 06:05:12 -05:00
|
|
|
:param application_id: User application name
|
2023-03-13 15:01:55 -04:00
|
|
|
:return: User matching the search criteria or None
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-03-13 15:01:55 -04:00
|
|
|
user = self.session.execute(
|
2023-02-02 13:00:58 -05:00
|
|
|
select(Model).where(Model.name == name, Model.application_id == application_id)
|
2023-03-13 15:01:55 -04:00
|
|
|
).first()
|
2023-05-19 13:15:40 -04:00
|
|
|
return user[0]
|
2022-12-08 20:54:47 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching user by name and application: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
|
|
|
except TypeError as err:
|
|
|
|
logging.error('Error while fetching user, empty result %s', err)
|
|
|
|
raise TypeError from err
|
2022-12-08 20:54:47 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def get_by_name_application_id_and_password(self, name: str, password: str, application_id: int) -> Model:
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
2023-05-19 13:15:40 -04:00
|
|
|
Fetch user based on the name, password and application id
|
2023-05-17 17:10:30 -04:00
|
|
|
:param name: Username
|
2023-02-01 06:05:12 -05:00
|
|
|
:param password: User password
|
2023-05-17 17:10:30 -04:00
|
|
|
:param application_id: Application id
|
2023-03-13 14:41:25 -04:00
|
|
|
:return: User
|
2022-12-08 20:54:47 -05:00
|
|
|
"""
|
|
|
|
try:
|
2023-02-01 06:05:12 -05:00
|
|
|
user = self.session.execute(
|
2023-02-02 13:00:58 -05:00
|
|
|
select(Model).where(Model.name == name, Model.application_id == application_id)
|
2023-02-01 06:05:12 -05:00
|
|
|
).first()
|
2022-12-08 20:54:47 -05:00
|
|
|
if user:
|
|
|
|
if Auth.check_password(password, user[0].password):
|
2023-02-21 10:03:37 -05:00
|
|
|
return user[0]
|
2022-12-08 20:54:47 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-19 13:15:40 -04:00
|
|
|
logging.error('Error while fetching user by name: %s', err)
|
|
|
|
raise SQLAlchemyError from err
|
|
|
|
raise ValueError('Unauthorized')
|
2023-02-21 09:41:26 -05:00
|
|
|
|
2023-05-19 13:15:40 -04:00
|
|
|
def get_by_name_application_uuid_and_password(self, name: str, password: str, application_uuid: str) -> Model:
|
2023-02-21 09:41:26 -05:00
|
|
|
"""
|
|
|
|
Fetch user based on the email and password
|
2023-05-17 17:10:30 -04:00
|
|
|
:param name: Username
|
2023-02-21 09:41:26 -05:00
|
|
|
:param password: User password
|
|
|
|
:param application_uuid: Application uuid
|
2023-03-13 14:41:25 -04:00
|
|
|
:return: User
|
2023-02-21 09:41:26 -05:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
application = self.session.execute(
|
|
|
|
select(ApplicationModel).where(ApplicationModel.application_uuid == application_uuid)
|
|
|
|
).first()
|
2023-05-19 13:15:40 -04:00
|
|
|
return self.get_by_name_application_id_and_password(name, password, application[0].id)
|
2023-02-21 09:41:26 -05:00
|
|
|
except SQLAlchemyError as err:
|
2023-05-18 12:29:28 -04:00
|
|
|
logging.error('Error while fetching user by name: %s', err)
|
2023-05-19 13:15:40 -04:00
|
|
|
raise SQLAlchemyError from err
|
|
|
|
except ValueError as err:
|
|
|
|
raise ValueError from err
|