Fixed auth user and user relation #70

This commit is contained in:
Sven Heidemann 2022-10-20 12:57:02 +02:00
parent d778bd2719
commit f35dd0b15d
7 changed files with 190 additions and 75 deletions

View File

@ -15,7 +15,6 @@ class AuthUserDTO(DtoABC):
password: str = None, password: str = None,
confirmation_id: Optional[str] = None, confirmation_id: Optional[str] = None,
auth_role: AuthRoleEnum = None, auth_role: AuthRoleEnum = None,
user_id: Optional[int] = None,
): ):
DtoABC.__init__(self) DtoABC.__init__(self)
@ -26,7 +25,6 @@ class AuthUserDTO(DtoABC):
self._password = password self._password = password
self._is_confirmed = confirmation_id is None self._is_confirmed = confirmation_id is None
self._auth_role = auth_role self._auth_role = auth_role
self._user_id = user_id
@property @property
def id(self) -> int: def id(self) -> int:
@ -80,14 +78,6 @@ class AuthUserDTO(DtoABC):
def auth_role(self, value: AuthRoleEnum): def auth_role(self, value: AuthRoleEnum):
self._auth_role = value self._auth_role = value
@property
def user_id(self) -> Optional[int]:
return self._user_id
@user_id.setter
def user_id(self, value: Optional[int]):
self._user_id = value
def from_dict(self, values: dict): def from_dict(self, values: dict):
self._id = values['id'] self._id = values['id']
self._first_name = values['firstName'] self._first_name = values['firstName']
@ -96,7 +86,6 @@ class AuthUserDTO(DtoABC):
self._password = values['password'] self._password = values['password']
self._is_confirmed = values['isConfirmed'] self._is_confirmed = values['isConfirmed']
self._auth_role = values['authRole'] self._auth_role = values['authRole']
self._user_id = values['userId']
def to_dict(self) -> dict: def to_dict(self) -> dict:
return { return {
@ -107,5 +96,4 @@ class AuthUserDTO(DtoABC):
'password': self._password, 'password': self._password,
'isConfirmed': self._is_confirmed, 'isConfirmed': self._is_confirmed,
'authRole': self._auth_role.value, 'authRole': self._auth_role.value,
'userId': self._user_id,
} }

View File

@ -12,6 +12,7 @@ from bot_api.model.discord.server_dto import ServerDTO
from bot_api.model.discord.server_filtered_result_dto import ServerFilteredResultDTO from bot_api.model.discord.server_filtered_result_dto import ServerFilteredResultDTO
from bot_api.model.error_dto import ErrorDTO from bot_api.model.error_dto import ErrorDTO
from bot_api.transformer.server_transformer import ServerTransformer from bot_api.transformer.server_transformer import ServerTransformer
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_role_enum import AuthRoleEnum
@ -25,11 +26,13 @@ class DiscordService:
bot: DiscordBotServiceABC, bot: DiscordBotServiceABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
auth: AuthServiceABC, auth: AuthServiceABC,
auth_users: AuthUserRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
): ):
self._bot = bot self._bot = bot
self._servers = servers self._servers = servers
self._auth = auth self._auth = auth
self._auth_users = auth_users
self._users = users self._users = users
def _to_dto(self, x: Server) -> Optional[ServerDTO]: def _to_dto(self, x: Server) -> Optional[ServerDTO]:
@ -59,12 +62,12 @@ class DiscordService:
raise ServiceException(ServiceErrorCode.InvalidData, 'Token invalid') raise ServiceException(ServiceErrorCode.InvalidData, 'Token invalid')
role = AuthRoleEnum(token['role']) role = AuthRoleEnum(token['role'])
if role == AuthRoleEnum.admin:
servers = self._servers.get_servers() servers = self._servers.get_servers()
else: if role != AuthRoleEnum.admin:
user = await self._auth.find_auth_user_by_email_async(token['email']) auth_user = self._auth_users.find_auth_user_by_email(token['email'])
user_from_db = self._users.find_user_by_id(0 if user.user_id is None else user.user_id) if auth_user is not None:
servers = self._servers.get_servers().where(lambda x: user_from_db is not None and x.server_id == user_from_db.server.server_id) user_ids = auth_user.users.select(lambda x: x.server is not None and x.server.server_id)
servers = servers.where(lambda x: x.server_id in user_ids)
servers = List(ServerDTO, servers) servers = List(ServerDTO, servers)
return servers.select(self._to_dto).where(lambda x: x.name != '') return servers.select(self._to_dto).where(lambda x: x.name != '')
@ -78,9 +81,10 @@ class DiscordService:
filtered_result = self._servers.get_filtered_servers(criteria) filtered_result = self._servers.get_filtered_servers(criteria)
# filter out servers, where the user not exists # filter out servers, where the user not exists
if role != AuthRoleEnum.admin: if role != AuthRoleEnum.admin:
user = await self._auth.find_auth_user_by_email_async(token['email']) auth_user = self._auth_users.find_auth_user_by_email(token['email'])
user_from_db = self._users.find_user_by_id(0 if user.user_id is None else user.user_id) if auth_user is not None:
filtered_result.result = filtered_result.result.where(lambda x: user_from_db is not None and x.server_id == user_from_db.server.server_id) user_ids = auth_user.users.select(lambda x: x.server is not None and x.server.server_id)
filtered_result.result = filtered_result.result.where(lambda x: x.server_id in user_ids)
servers: List = filtered_result.result.select(self._to_dto).where(lambda x: x.name != '') servers: List = filtered_result.result.select(self._to_dto).where(lambda x: x.name != '')
result = List(ServerDTO, servers) result = List(ServerDTO, servers)

View File

@ -23,7 +23,7 @@ class AuthUserTransformer(TransformerABC):
datetime.now(tz=timezone.utc), datetime.now(tz=timezone.utc),
AuthRoleEnum.normal if dto.auth_role is None else AuthRoleEnum(dto.auth_role), AuthRoleEnum.normal if dto.auth_role is None else AuthRoleEnum(dto.auth_role),
dto.user_id, dto.user_id,
id=0 if dto.id is None else dto.id auth_user_id=0 if dto.id is None else dto.id
) )
@staticmethod @staticmethod
@ -35,6 +35,5 @@ class AuthUserTransformer(TransformerABC):
db.email, db.email,
'' if password is None else password, '' if password is None else password,
db.confirmation_id, db.confirmation_id,
db.auth_role, db.auth_role
db.user_id
) )

View File

@ -30,14 +30,29 @@ class ApiMigration(MigrationABC):
`OAuthId` VARCHAR(255) DEFAULT NULL, `OAuthId` VARCHAR(255) DEFAULT NULL,
`RefreshTokenExpiryTime` DATETIME(6) NOT NULL, `RefreshTokenExpiryTime` DATETIME(6) NOT NULL,
`AuthRole` INT NOT NULL DEFAULT '0', `AuthRole` INT NOT NULL DEFAULT '0',
`CreatedOn` DATETIME(6) NOT NULL,
`LastModifiedOn` DATETIME(6) NOT NULL,
PRIMARY KEY(`Id`)
);
""")
)
self._cursor.execute(
str(f"""
CREATE TABLE IF NOT EXISTS `AuthUserUsersRelations`(
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`AuthUserId` BIGINT DEFAULT NULL,
`UserId` BIGINT DEFAULT NULL, `UserId` BIGINT DEFAULT NULL,
`CreatedOn` DATETIME(6) NOT NULL, `CreatedOn` DATETIME(6) NOT NULL,
`LastModifiedOn` DATETIME(6) NOT NULL, `LastModifiedOn` DATETIME(6) NOT NULL,
PRIMARY KEY(`Id`), PRIMARY KEY(`Id`),
FOREIGN KEY (`AuthUserId`) REFERENCES `AuthUsers`(`Id`),
FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`) FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`)
) );
""") """)
) )
def downgrade(self): def downgrade(self):
self._cursor.execute('DROP TABLE `AuthUsers`;') self._cursor.execute('DROP TABLE `AuthUsers`;')
self._cursor.execute('DROP TABLE `AuthUserUsersRelations`;')

View File

@ -2,9 +2,11 @@ import uuid
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
from cpl_core.database import TableABC from cpl_core.database import TableABC
from cpl_query.extension import List
from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_role_enum import AuthRoleEnum
from bot_data.model.server import Server from bot_data.model.server import Server
from bot_data.model.user import User
class AuthUser(TableABC): class AuthUser(TableABC):
@ -22,12 +24,12 @@ class AuthUser(TableABC):
oauth_id: Optional[str], oauth_id: Optional[str],
refresh_token_expire_time: datetime, refresh_token_expire_time: datetime,
auth_role: AuthRoleEnum, auth_role: AuthRoleEnum,
user_id: Optional[int],
created_at: datetime = None, created_at: datetime = None,
modified_at: datetime = None, modified_at: datetime = None,
id=0 auth_user_id=0,
users: List[User] = None
): ):
self._auth_user_id = id self._auth_user_id = auth_user_id
self._first_name = first_name self._first_name = first_name
self._last_name = last_name self._last_name = last_name
self._email = email self._email = email
@ -39,8 +41,10 @@ class AuthUser(TableABC):
self._forgot_password_id = forgot_password_id self._forgot_password_id = forgot_password_id
self._refresh_token_expire_time = refresh_token_expire_time self._refresh_token_expire_time = refresh_token_expire_time
if users is None:
self._users = List(User)
self._auth_role_id = auth_role self._auth_role_id = auth_role
self._user_id = user_id
TableABC.__init__(self) TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at self._created_at = created_at if created_at is not None else self._created_at
@ -139,12 +143,12 @@ class AuthUser(TableABC):
self._auth_role_id = value self._auth_role_id = value
@property @property
def user_id(self) -> Optional[int]: def users(self) -> List[User]:
return self._user_id return self._users
@user_id.setter @users.setter
def user_id(self, value: Optional[int]): def users(self, value: List[User]):
self._user_id = value self._users = value
@staticmethod @staticmethod
def get_select_all_string() -> str: def get_select_all_string() -> str:
@ -180,6 +184,13 @@ class AuthUser(TableABC):
WHERE `ForgotPasswordId` = '{id}'; WHERE `ForgotPasswordId` = '{id}';
""") """)
def get_select_user_id_from_relations(self) -> str:
return str(f"""
SELECT `UserId`
FROM `AuthUserUsersRelations`
WHERE `AuthUserId` = {self._auth_user_id};
""")
@property @property
def insert_string(self) -> str: def insert_string(self) -> str:
return str(f""" return str(f"""
@ -196,7 +207,6 @@ class AuthUser(TableABC):
`OAuthId`, `OAuthId`,
`RefreshTokenExpiryTime`, `RefreshTokenExpiryTime`,
`AuthRole`, `AuthRole`,
`UserId`,
`CreatedOn`, `CreatedOn`,
`LastModifiedOn` `LastModifiedOn`
) VALUES ( ) VALUES (
@ -212,7 +222,6 @@ class AuthUser(TableABC):
'{"NULL" if self._oauth_id is None else self._oauth_id}', '{"NULL" if self._oauth_id is None else self._oauth_id}',
'{self._refresh_token_expire_time}', '{self._refresh_token_expire_time}',
{self._auth_role_id.value}, {self._auth_role_id.value},
{"NULL" if self._user_id is None else self._user_id},
'{self._created_at}', '{self._created_at}',
'{self._modified_at}' '{self._modified_at}'
) )
@ -233,7 +242,6 @@ class AuthUser(TableABC):
`OAuthId` = '{"NULL" if self._oauth_id is None else self._oauth_id}', `OAuthId` = '{"NULL" if self._oauth_id is None else self._oauth_id}',
`RefreshTokenExpiryTime` = '{self._refresh_token_expire_time}', `RefreshTokenExpiryTime` = '{self._refresh_token_expire_time}',
`AuthRole` = {self._auth_role_id.value}, `AuthRole` = {self._auth_role_id.value},
`UserId` = {"NULL" if self._user_id is None else self._user_id},
`LastModifiedOn` = '{self._modified_at}' `LastModifiedOn` = '{self._modified_at}'
WHERE `AuthUsers`.`Id` = {self._auth_user_id}; WHERE `AuthUsers`.`Id` = {self._auth_user_id};
""") """)

View File

@ -0,0 +1,87 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
from bot_data.model.auth_user import AuthUser
from bot_data.model.user import User
from bot_data.model.server import Server
class AuthUserUsersRelation(TableABC):
def __init__(self, auth_user: AuthUser, user: User, created_at: datetime = None, modified_at: datetime = None):
self._auth_user = auth_user
self._user = user
TableABC.__init__(self)
self._created_at = created_at if created_at is not None else self._created_at
self._modified_at = modified_at if modified_at is not None else self._modified_at
@property
def auth_user(self) -> AuthUser:
return self._auth_user
@auth_user.setter
def auth_user(self, value: AuthUser):
self._auth_user = value
@property
def user(self) -> User:
return self._user
@user.setter
def user(self, value: User):
self._user = value
@staticmethod
def get_select_all_string() -> str:
return str(f"""
SELECT * FROM `AuthUserUsersRelations`;
""")
@staticmethod
def get_select_by_auth_user_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AuthUserUsersRelations`
WHERE `AuthUserId` = {id};
""")
@staticmethod
def get_select_by_user_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `AuthUserUsersRelations`
WHERE `UserId` = {id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `AuthUserUsersRelations` (
`AuthUserId`, `UserId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._auth_user.id},
{self._user.user_id},
'{self._created_at}',
'{self._modified_at}'
);
""")
@property
def udpate_string(self) -> str:
return str(f"""
UPDATE `AuthUserUsersRelations`
SET `AuthUserId` = '{self._auth_user.id}',,
`UserId` = '{self._user.user_id}'
`LastModifiedAt` = '{self._modified_at}'
WHERE `AuthUserId` = {self._auth_user.id}
AND `UserId` = {self._user.user_id};
""")
@property
def delete_string(self) -> str:
return str(f"""
DELETE FROM `AuthUserUsersRelations`
WHERE `AuthUserId` = {self._auth_user.id}
AND `UserId` = {self._user.user_id};
""")

View File

@ -6,16 +6,19 @@ from cpl_query.extension import List
from bot_api.filter.auth_user_select_criteria import AuthUserSelectCriteria from bot_api.filter.auth_user_select_criteria import AuthUserSelectCriteria
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.filtered_result import FilteredResult from bot_data.filtered_result import FilteredResult
from bot_data.model.auth_role_enum import AuthRoleEnum from bot_data.model.auth_role_enum import AuthRoleEnum
from bot_data.model.auth_user import AuthUser from bot_data.model.auth_user import AuthUser
from bot_data.model.user import User
class AuthUserRepositoryService(AuthUserRepositoryABC): class AuthUserRepositoryService(AuthUserRepositoryABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC): def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC, users: UserRepositoryABC):
self._logger = logger self._logger = logger
self._context = db_context self._context = db_context
self._users = users
AuthUserRepositoryABC.__init__(self) AuthUserRepositoryABC.__init__(self)
@ -26,23 +29,34 @@ class AuthUserRepositoryService(AuthUserRepositoryABC):
return value return value
def _user_from_result(self, result: tuple) -> AuthUser: def _user_from_result(self, au_result: tuple) -> AuthUser:
return AuthUser( auth_user = AuthUser(
self._get_value_from_result(result[1]), self._get_value_from_result(au_result[1]),
self._get_value_from_result(result[2]), self._get_value_from_result(au_result[2]),
self._get_value_from_result(result[3]), self._get_value_from_result(au_result[3]),
self._get_value_from_result(result[4]), self._get_value_from_result(au_result[4]),
self._get_value_from_result(result[5]), self._get_value_from_result(au_result[5]),
self._get_value_from_result(result[6]), self._get_value_from_result(au_result[6]),
self._get_value_from_result(result[7]), self._get_value_from_result(au_result[7]),
self._get_value_from_result(result[8]), self._get_value_from_result(au_result[8]),
self._get_value_from_result(result[9]), self._get_value_from_result(au_result[9]),
self._get_value_from_result(result[10]), self._get_value_from_result(au_result[10]),
AuthRoleEnum(self._get_value_from_result(result[11])), AuthRoleEnum(self._get_value_from_result(au_result[11])),
self._get_value_from_result(result[12]), auth_user_id=self._get_value_from_result(au_result[0])
id=self._get_value_from_result(result[0])
) )
self._logger.trace(__name__, f'Send SQL command: {auth_user.get_select_user_id_from_relations()}')
results = self._context.select(auth_user.get_select_user_id_from_relations())
for result in results:
user_id = self._get_value_from_result(result[0])
if user_id is None:
continue
user = self._users.get_user_by_id(user_id)
auth_user.users.append(user)
return auth_user
def get_all_auth_users(self) -> List[AuthUser]: def get_all_auth_users(self) -> List[AuthUser]:
users = List(AuthUser) users = List(AuthUser)
self._logger.trace(__name__, f'Send SQL command: {AuthUser.get_select_all_string()}') self._logger.trace(__name__, f'Send SQL command: {AuthUser.get_select_all_string()}')