From 0805f02f1f38f6033052659b68adf2bd6245929c Mon Sep 17 00:00:00 2001 From: Aqr-K <95741669+Aqr-K@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:15:35 +0800 Subject: [PATCH] feat(user): Add username modification --- app/api/endpoints/user.py | 35 ++++++++++++++++++++++++++++++----- app/db/models/user.py | 12 ++++++++++++ app/schemas/user.py | 2 ++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/api/endpoints/user.py b/app/api/endpoints/user.py index ae295eaa7..d1d6d6f02 100644 --- a/app/api/endpoints/user.py +++ b/app/api/endpoints/user.py @@ -54,7 +54,7 @@ def create_user( def update_user( *, db: Session = Depends(get_db), - user_in: schemas.UserCreate, + user_in: schemas.UserUpdate, _: User = Depends(get_current_active_superuser), ) -> Any: """ @@ -69,7 +69,15 @@ def update_user( message="密码需要同时包含字母、数字、特殊字符中的至少两项,且长度大于6位") user_info["hashed_password"] = get_password_hash(user_info["password"]) user_info.pop("password") - user = User.get_by_name(db, name=user_info["name"]) + user = User.get_by_id(db, user_id=user_info["id"]) + user_name = user_info.get("name") + if not user_name: + return schemas.Response(success=False, message="用户名不能为空") + # 新用户名去重 + users = User.list(db) + for u in users: + if u.name == user_name and u.id != user_info["id"]: + return schemas.Response(success=False, message="用户名已被使用") if not user: return schemas.Response(success=False, message="用户不存在") user.update(db, user_info) @@ -165,15 +173,32 @@ def set_config(key: str, value: Union[list, dict, bool, int, str] = None, return schemas.Response(success=True) -@router.delete("/{user_name}", summary="删除用户", response_model=schemas.Response) -def delete_user( +@router.delete("/id/{user_id}", summary="删除用户", response_model=schemas.Response) +def delete_user_from_user_id( + *, + db: Session = Depends(get_db), + user_id: int, + current_user: User = Depends(get_current_active_superuser), +) -> Any: + """ + 通过唯一ID删除用户 + """ + user = current_user.get_by_id(db, user_id=user_id) + if not user: + return schemas.Response(success=False, message="用户不存在") + user.delete_by_id(db, user_id) + return schemas.Response(success=True) + + +@router.delete("/name/{user_name}", summary="删除用户", response_model=schemas.Response) +def delete_user_from_user_id( *, db: Session = Depends(get_db), user_name: str, current_user: User = Depends(get_current_active_superuser), ) -> Any: """ - 删除用户 + 通过用户名删除用户 """ user = current_user.get_by_name(db, name=user_name) if not user: diff --git a/app/db/models/user.py b/app/db/models/user.py index 0e7b79fae..2a6fb7486 100644 --- a/app/db/models/user.py +++ b/app/db/models/user.py @@ -54,6 +54,11 @@ def authenticate(db: Session, name: str, password: str, def get_by_name(db: Session, name: str): return db.query(User).filter(User.name == name).first() + @staticmethod + @db_query + def get_by_id(db: Session, user_id: int): + return db.query(User).filter(User.id == user_id).first() + @db_update def delete_by_name(self, db: Session, name: str): user = self.get_by_name(db, name) @@ -61,6 +66,13 @@ def delete_by_name(self, db: Session, name: str): user.delete(db, user.id) return True + @db_update + def delete_by_id(self, db: Session, user_id: int): + user = self.get_by_id(db, user_id) + if user: + user.delete(db, user.id) + return True + @db_update def update_otp_by_name(self, db: Session, name: str, otp: bool, secret: str): user = self.get_by_name(db, name) diff --git a/app/schemas/user.py b/app/schemas/user.py index 813ad005d..b366d2d24 100644 --- a/app/schemas/user.py +++ b/app/schemas/user.py @@ -36,7 +36,9 @@ class UserCreate(UserBase): # Properties to receive via API on update class UserUpdate(UserBase): + id: int name: str + email: Optional[str] = None password: Optional[str] = None settings: Optional[dict] = {}