Skip to content

Commit 91e6c46

Browse files
perf: User email check
1 parent 888f5ee commit 91e6c46

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

backend/apps/system/api/user.py

Lines changed: 10 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, check_email_exists, get_db_user, single_delete, user_ws_options
4+
from apps.system.crud.user import check_account_exists, check_email_exists, check_email_format, check_pwd_format, 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
@@ -120,6 +120,8 @@ async def create(session: SessionDep, creator: UserCreator):
120120
raise Exception(f"Account [{creator.account}] already exists!")
121121
if check_email_exists(session=session, email=creator.email):
122122
raise Exception(f"Email [{creator.email}] already exists!")
123+
if not check_email_format(creator.email):
124+
raise Exception(f"Email [{creator.email}] format is invalid!")
123125
data = creator.model_dump(exclude_unset=True)
124126
user_model = UserModel.model_validate(data)
125127
#user_model.create_time = get_timestamp()
@@ -150,6 +152,8 @@ async def update(session: SessionDep, editor: UserEditor):
150152
raise Exception(f"account cannot be changed!")
151153
if editor.email != user_model.email and check_email_exists(session=session, account=editor.email):
152154
raise Exception(f"Email [{editor.email}] already exists!")
155+
if not check_email_format(editor.email):
156+
raise Exception(f"Email [{editor.email}] format is invalid!")
153157
origin_oid: int = user_model.oid
154158
del_stmt = sqlmodel_delete(UserWsModel).where(UserWsModel.uid == editor.id)
155159
session.exec(del_stmt)
@@ -206,9 +210,12 @@ async def pwdReset(session: SessionDep, current_user: CurrentUser, id: int):
206210
@router.put("/pwd")
207211
@clear_cache(namespace=CacheNamespace.AUTH_INFO, cacheName=CacheName.USER_INFO, keyExpression="current_user.id")
208212
async def pwdUpdate(session: SessionDep, current_user: CurrentUser, editor: PwdEditor):
213+
new_pwd = editor.new_pwd
214+
if not check_pwd_format(new_pwd):
215+
raise Exception("Password format is invalid!")
209216
db_user: UserModel = get_db_user(session=session, user_id=current_user.id)
210217
if not verify_md5pwd(editor.pwd, db_user.password):
211-
raise HTTPException("pwd error")
212-
db_user.password = md5pwd(editor.new_pwd)
218+
raise Exception(f"pwd [{editor.pwd}] error")
219+
db_user.password = md5pwd(new_pwd)
213220
session.add(db_user)
214221
session.commit()

backend/apps/system/crud/user.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from common.utils.utils import SQLBotLogUtil
1111
from ..models.user import UserModel
1212
from common.core.security import verify_md5pwd
13+
import re
1314

1415
def get_db_user(*, session: Session, user_id: int) -> UserModel:
1516
db_user = session.get(UserModel, user_id)
@@ -74,4 +75,23 @@ async def clean_user_cache(id: int):
7475
def check_account_exists(*, session: Session, account: str) -> bool:
7576
session.exec(select(func.count()).select_from(UserModel).where(UserModel.account == account)).one() > 0
7677
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
78+
return session.exec(select(func.count()).select_from(UserModel).where(UserModel.email == email)).one() > 0
79+
80+
81+
# 预编译正则表达式,提高效率
82+
EMAIL_REGEX = re.compile(
83+
r"^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@"
84+
r"([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+"
85+
r"[a-zA-Z]{2,}$"
86+
)
87+
88+
PWD_REGEX = re.compile(
89+
r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)"
90+
r"(?=.*[~!@#$%^&*()_+\-={}|:\"<>?`\[\];',./])"
91+
r"[A-Za-z\d~!@#$%^&*()_+\-={}|:\"<>?`\[\];',./]{8,20}$"
92+
)
93+
def check_email_format(email: str) -> bool:
94+
return bool(EMAIL_REGEX.fullmatch(email))
95+
96+
def check_pwd_format(pwd: str) -> bool:
97+
return bool(PWD_REGEX.fullmatch(pwd))

0 commit comments

Comments
 (0)