11from typing import Optional
22from fastapi import APIRouter , HTTPException , Query
33from 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
55from apps .system .models .system_model import UserWsModel
66from apps .system .models .user import UserModel
77from 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" )
208212async 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 ()
0 commit comments