From 5b4bf45ad275faa9003275877e71af0575ee2117 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Thu, 28 Dec 2023 23:24:51 -0800 Subject: [PATCH] fix: delete auth with user --- backend/apps/web/models/auths.py | 16 ++++++++++++++++ backend/apps/web/models/users.py | 4 ++-- backend/apps/web/routers/users.py | 6 ++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/backend/apps/web/models/auths.py b/backend/apps/web/models/auths.py index c066dbef..1bd4b4cb 100644 --- a/backend/apps/web/models/auths.py +++ b/backend/apps/web/models/auths.py @@ -109,5 +109,21 @@ class AuthsTable: except: return None + def delete_auth_by_id(self, id: str) -> Optional[UserModel]: + try: + # Delete User + result = Users.delete_user_by_id(id) + + if result: + # Delete Auth + query = Auth.delete().where(Auth.id == id) + query.execute() # Remove the rows, return number of rows removed. + + return True + else: + return False + except: + return False + Auths = AuthsTable(DB) diff --git a/backend/apps/web/models/users.py b/backend/apps/web/models/users.py index 4b790ab2..b7df92eb 100644 --- a/backend/apps/web/models/users.py +++ b/backend/apps/web/models/users.py @@ -8,7 +8,7 @@ from utils.utils import decode_token from utils.misc import get_gravatar_url from apps.web.internal.db import DB -from apps.web.models.chats import Chat +from apps.web.models.chats import Chats #################### @@ -115,7 +115,7 @@ class UsersTable: def delete_user_by_id(self, id: str) -> bool: try: # Delete User Chats - result = Chat.delete_chats_by_user_id(id) + result = Chats.delete_chats_by_user_id(id) if result: # Delete User diff --git a/backend/apps/web/routers/users.py b/backend/apps/web/routers/users.py index 2b116828..fd0d2d6f 100644 --- a/backend/apps/web/routers/users.py +++ b/backend/apps/web/routers/users.py @@ -9,6 +9,8 @@ import time import uuid from apps.web.models.users import UserModel, UserRoleUpdateForm, Users +from apps.web.models.auths import Auths + from utils.utils import ( get_password_hash, @@ -76,7 +78,7 @@ async def update_user_role(form_data: UserRoleUpdateForm, cred=Depends(bearer_sc ############################ -# DeleteUser +# DeleteUserById ############################ @@ -88,7 +90,7 @@ async def delete_user_by_id(user_id: str, cred=Depends(bearer_scheme)): if user: if user.role == "admin": if user.id != user_id: - result = Users.delete_user_by_id(user_id) + result = Auths.delete_auth_by_id(user_id) if result: return True