From 4ef428c62057adabd87711a38278b7894332e63b Mon Sep 17 00:00:00 2001 From: MiLla <18826902282@163.com> Date: Wed, 20 Nov 2024 22:04:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86=E5=AF=B9?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E7=A7=9F=E6=88=B7=E7=AE=A1=E7=90=86=E6=9D=83=E9=99=90?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/manage_project.py | 18 ++++----- api/manage_tanant.py | 94 ++++++++++++++++++++++--------------------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/api/manage_project.py b/api/manage_project.py index 46fb788..804faa4 100644 --- a/api/manage_project.py +++ b/api/manage_project.py @@ -57,10 +57,10 @@ async def get_project(response: Response, session: SessionDep, current_user: Use ] } -#新增与修改项目 + +# 新增与修改项目 @router.post("/api/s1/project") async def create_project(data: dict, session: SessionDep, current_user: User = Depends(get_current_user)): - if current_user.role != 1: raise HTTPException(status_code=403, detail="Only Tenant admin users can add or update projects.") @@ -112,7 +112,7 @@ async def create_project(data: dict, session: SessionDep, current_user: User = D else: # 新增项目 exist_project = session.exec(select(Project).where(Project.name == name)).first() - print(exist_project) #测试用 + print(exist_project) # 测试用 if exist_project: raise HTTPException(status_code=404, detail="Project already exists") @@ -128,7 +128,7 @@ async def create_project(data: dict, session: SessionDep, current_user: User = D # 处理项目和用户的关联 # 先清除现有的关联 # 生成删除语句并执行 - print(project_id) #测试用 + print(project_id) # 测试用 stmt = delete(ProjectUserLink).where(ProjectUserLink.project_id == project.id) session.execute(stmt) session.commit() # 提交事务 @@ -154,10 +154,10 @@ async def create_project(data: dict, session: SessionDep, current_user: User = D "information": project, } -#删除项目 -@router.delete("/api/s1/project") -async def delete_project(data: dict, session: SessionDep,current_user: User = Depends(get_current_user)): +# 删除项目 +@router.delete("/api/s1/project") +async def delete_project(data: dict, session: SessionDep, current_user: User = Depends(get_current_user)): if current_user.role != 1: raise HTTPException(status_code=403, detail="Only Tenant admin users can delete projects.") @@ -175,7 +175,7 @@ async def delete_project(data: dict, session: SessionDep,current_user: User = De select(Project).where(Project.name == project_name)).first() if not project: - raise HTTPException(status_code=404,detail="Project not found") + raise HTTPException(status_code=404, detail="Project not found") # 删除与项目相关的用户链接 # 先清除现有的关联 @@ -186,4 +186,4 @@ async def delete_project(data: dict, session: SessionDep,current_user: User = De session.delete(project) session.commit() - return {"detail": "Project deleted successfully"} \ No newline at end of file + return {"detail": "Project deleted successfully"} diff --git a/api/manage_tanant.py b/api/manage_tanant.py index 49cda8a..ad4240b 100644 --- a/api/manage_tanant.py +++ b/api/manage_tanant.py @@ -23,7 +23,11 @@ from dependencies import SessionDep # 假设 SessionDep 是数据库会话的 #列举所有租户 @router.get("/api/s1/tenant") -async def get_tenant(response: Response, session: SessionDep): +async def get_tenant(response: Response, session: SessionDep, current_user: User = Depends(get_current_user)): + + if current_user.role != 0: + raise HTTPException(status_code=403, detail="Only Superadmin can list all tenants.") + tenants = session.query(Tenant).all() # 获取所有租户 if not tenants: raise HTTPException(status_code=404, detail="No tenants found") @@ -49,44 +53,21 @@ async def get_tenant(response: Response, session: SessionDep): # 新增和修改租户 @router.post("/api/s1/tenant") -async def create_or_update_tenant(data: dict, session: SessionDep): - name = data["name"] - username = data["username"] - password = data.get("password", "") # 默认为空字符串 +async def create_or_update_tenant(data: dict, session: SessionDep, current_user: User = Depends(get_current_user)): + if current_user.role != 0: + raise HTTPException(status_code=403, detail="Only Superadmin can add or update tenants.") + + name = data.get("name") + username = data.get("username") + password = data.get("password") # 验证是否缺少必要参数 - if not name or not username: - raise HTTPException(status_code=400, detail="Need more name/username") + if not name: + raise HTTPException(status_code=400, detail="Need more name") - # 查找用户 - user_query = select(User).where(User.username == username) - existing_user = session.exec(user_query).first() - - # 如果密码为空,更新租户信息 - if password == "": - print("密码为空") #测试用 - # 如果用户不存在,返回错误 - if not existing_user: - raise HTTPException(status_code=404, detail="User not found") - else: - # 如果找到了对应的 User - # 使用 user.tenant_id 查找对应的 Tenant - tenant = session.get(Tenant, existing_user.tenant_id) - # 如果 Tenant 存在,更新 Tenant 的 name 字段 - if tenant: - tenant.name = name - session.commit() # 提交更新 - else: - raise HTTPException(status_code=404, detail="Tenant not found") - return {"message": "Tenant and User update successfully"} - else: - print("密码不为空") #测试用 - # 如果密码不为空,执行创建新租户和用户的操作 - if existing_user: - # 如果用户已存在,返回错误 - raise HTTPException(status_code=409, detail="User already exists") - - # 检查租户是否已存在 + if username: + # 如果 username 不为空,判断为新建租户 + # 检查租户名是否已存在 tenant_query = select(Tenant).where(Tenant.name == name) existing_tenant = session.exec(tenant_query).first() @@ -94,11 +75,7 @@ async def create_or_update_tenant(data: dict, session: SessionDep): raise HTTPException(status_code=409, detail="Tenant name already exists") # 创建新租户 - tenant = Tenant( - name=name, - username=username, - password=password, # 实际使用时应加密密码 - ) + tenant = Tenant(name=name) session.add(tenant) session.commit() session.refresh(tenant) @@ -106,21 +83,48 @@ async def create_or_update_tenant(data: dict, session: SessionDep): # 创建新用户 user = User( username=username, - password=password, # 同样需要加密密码 + password=password, # 记得加密密码 role=1, # 默认role为1 - tenant_id = tenant.id, + tenant_id=tenant.id, ) session.add(user) # 提交事务 session.commit() - session.refresh(tenant) return {"message": "Tenant and User added successfully"} + else: + # 如果 username 为空,执行更新操作 + # 根据租户名称查找 Tenant + tenant_query = select(Tenant).where(Tenant.name == name) + tenant = session.exec(tenant_query).first() + + # 如果找不到对应的租户,抛出错误 + if not tenant: + raise HTTPException(status_code=404, detail="Tenant not found") + + # 找到租户后,根据 tenant_id 查找该租户下的所有用户 + user_query = select(User).where(User.tenant_id == tenant.id) + + user = session.exec(user_query).first() + + #如果找不到对应的用户,抛出错误 + if not user: + raise HTTPException(status_code=404, detail="User not found") + user.password = password + session.add(user) + session.commit() + print(user) #测试用 + return {"message": "Tenant and User update successfully"} + #删除租户 @router.delete("/api/s1/tenant") -async def delete_tenant(data: dict, session: SessionDep): +async def delete_tenant(data: dict, session: SessionDep, current_user: User = Depends(get_current_user)): + + if current_user.role != 0: + raise HTTPException(status_code=403, detail="Only Superadmin can delete tenants.") + tenant_name = data.get("name") if not tenant_name: