Skip to content

Commit 75d8505

Browse files
committed
Merge branch 'main' of https://github.com/dataease/SQLBot
2 parents f10cd22 + ed82855 commit 75d8505

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

backend/apps/system/api/user.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional
22
from fastapi import APIRouter, HTTPException, Query
33
from sqlmodel import func, or_, select, delete as sqlmodel_delete
4-
from apps.system.crud.user import check_account_exists, get_db_user, single_delete, user_ws_options
4+
from apps.system.crud.user import check_account_exists, check_email_exists, get_db_user, single_delete, user_ws_options
55
from apps.system.models.system_model import UserWsModel
66
from apps.system.models.user import UserModel
77
from apps.system.schemas.auth import CacheName, CacheNamespace
@@ -116,9 +116,10 @@ async def query(session: SessionDep, trans: Trans, id: int) -> UserEditor:
116116

117117
@router.post("")
118118
async def create(session: SessionDep, creator: UserCreator):
119-
count = check_account_exists(session=session, account=creator.account)
120-
if count > 0:
119+
if check_account_exists(session=session, account=creator.account):
121120
raise Exception(f"Account [{creator.account}] already exists!")
121+
if check_email_exists(session=session, email=creator.email):
122+
raise Exception(f"Email [{creator.email}] already exists!")
122123
data = creator.model_dump(exclude_unset=True)
123124
user_model = UserModel.model_validate(data)
124125
#user_model.create_time = get_timestamp()
@@ -147,6 +148,8 @@ async def update(session: SessionDep, editor: UserEditor):
147148
raise Exception(f"User with id [{editor.id}] not found!")
148149
if editor.account != user_model.account:
149150
raise Exception(f"account cannot be changed!")
151+
if editor.email != user_model.email and check_email_exists(session=session, account=editor.email):
152+
raise Exception(f"Email [{editor.email}] already exists!")
150153
origin_oid: int = user_model.oid
151154
del_stmt = sqlmodel_delete(UserWsModel).where(UserWsModel.uid == editor.id)
152155
session.exec(del_stmt)

backend/apps/system/crud/user.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ async def clean_user_cache(id: int):
7272

7373

7474
def check_account_exists(*, session: Session, account: str) -> bool:
75-
return session.exec(select(func.count()).select_from(UserModel).where(UserModel.account == account)).one()
75+
session.exec(select(func.count()).select_from(UserModel).where(UserModel.account == account)).one() > 0
76+
def check_email_exists(*, session: Session, email: str) -> bool:
77+
return session.exec(select(func.count()).select_from(UserModel).where(UserModel.email == email)).one() > 0

0 commit comments

Comments
 (0)