Ver código fonte

动态鉴权调整。

baoyubo 6 meses atrás
pai
commit
8f58b45fc2

+ 3 - 2
common/security.py

@@ -13,14 +13,15 @@ from extensions import logger
 # https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/
 
 pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
-
-def valid_access_token(Authorization: str ="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiZXhwIjoyMDM5Njk2ODMzfQ.Rhd38oo_S1odjg0xnT4n31cCWCAAPXGb8y_V2XcgqzQ")->int:#= Header(..., alias="Authorization")) -> int:
+#="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiZXhwIjoyMDM5Njk2ODMzfQ.Rhd38oo_S1odjg0xnT4n31cCWCAAPXGb8y_V2XcgqzQ")->int:#
+def valid_access_token(Authorization: str = Header(..., alias="Authorization")) -> int:
 
     try:
         access_token = Authorization.removeprefix("Bearer ")
 
         token_exception = TokenException()
         payload = jwt.decode(access_token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
+        print(payload,payload.get("sub"))
         user_id: str = payload.get("sub")
         logger.info('sub user_id: {}', user_id)
     except Exception:

+ 8 - 5
routers/prod_api/auth.py

@@ -207,11 +207,14 @@ async def logout(
 ):
     logger.info("logout ok")
     request.session.clear()
-    
-    if user.is_yzy_user == 1:
-        logout_url = settings.TYRZ_LOGOUT.format(settings.TYRZ_CLIENT_ID) + quote(settings.HOME_URL+"/yjzp/")
-        logger.info(logout_url)
-    else:
+
+    try:
+        if user.is_yzy_user == 1:
+            logout_url = settings.TYRZ_LOGOUT.format(settings.TYRZ_CLIENT_ID) + quote(settings.HOME_URL+"/yjzp/")
+            logger.info(logout_url)
+        else:
+            logout_url = settings.HOME_URL + "/yjzp/"
+    except:
         logout_url = settings.HOME_URL+"/yjzp/"
     return {
         "code": 200,

+ 74 - 66
routers/prod_api/system/user/__init__.py

@@ -154,74 +154,82 @@ async def userupdate(
 
 @router.get('/getInfo')
 async def getInfo(request: Request,db: Session = Depends(get_db), user_id: int = Depends(valid_access_token)):
-    # 用户信息
-    info = db.query(SysUser).filter(SysUser.user_id == user_id).first()
-    
-    # 部门信息
-    dept_name = ""
-    dept_info = db.query(SysDept).filter(SysDept.dept_id == info.dept_id).first()
-    if dept_name is not None:
-        dept_name = dept_info.dept_name
-    
-    # 角色信息
-    roles = []
-    role_ids = db.query(SysUserRole).filter(SysUserRole.user_id == user_id).all()
-    for role in role_ids:
-        role_info = db.query(SysRole).filter(SysRole.role_id == role.role_id).first()
-        roles.append(
-            {
-                "roleId": role_info.role_id,
-                "roleName": role_info.role_name,
-                "roleKey": role_info.role_key,
-                "roleSort": role_info.role_sort,
-                "dataScope": role_info.data_scope,
-                "menuCheckStrictly": role_info.menu_check_strictly,
-                "deptCheckStrictly": role_info.dept_check_strictly,
-                "status": role_info.status,
-                "remark": role_info.remark,
-                "createTime": get_datetime_str(role_info.create_time),
-                "flag": False,
-                "superAdmin": True
-            }
-        )
+    try:
+        # 用户信息
+        info = db.query(SysUser).filter(SysUser.user_id == user_id).first()
+        if info is None:
+            return {
+            "code": 200,
+            "msg": "操作成功",
+            "data": None}
+        # 部门信息
+        dept_name = ""
+        dept_info = db.query(SysDept).filter(SysDept.dept_id == info.dept_id).first()
+        if dept_name is not None:
+            dept_name = dept_info.dept_name
+
+        # 角色信息
+        roles = []
+        role_ids = db.query(SysUserRole).filter(SysUserRole.user_id == user_id).all()
+        for role in role_ids:
+            role_info = db.query(SysRole).filter(SysRole.role_id == role.role_id).first()
+            roles.append(
+                {
+                    "roleId": role_info.role_id,
+                    "roleName": role_info.role_name,
+                    "roleKey": role_info.role_key,
+                    "roleSort": role_info.role_sort,
+                    "dataScope": role_info.data_scope,
+                    "menuCheckStrictly": role_info.menu_check_strictly,
+                    "deptCheckStrictly": role_info.dept_check_strictly,
+                    "status": role_info.status,
+                    "remark": role_info.remark,
+                    "createTime": get_datetime_str(role_info.create_time),
+                    "flag": False,
+                    "superAdmin": True
+                }
+            )
 
-    role_keys = [
-        n['roleKey']
-        for n in roles
-    ]
+        role_keys = [
+            n['roleKey']
+            for n in roles
+        ]
 
-    return {
-        "code": 200,
-        "msg": "操作成功",
-        "data": {
-            "user": {
-                "userId": info.user_id,
-                "tenantId": info.tenant_id,
-                "deptId": info.dept_id,
-                "userName": info.user_name,
-                "nickName": info.nick_name,
-                "userType": info.user_type,
-                "email": info.email,
-                "phonenumber": info.phonenumber,
-                "sex": info.sex,
-                "avatar": info.avatar,
-                "status": info.status,
-                "loginIp": info.login_ip,
-                "loginDate": get_datetime_str(info.login_date),
-                "remark": info.remark,
-                "createTime": get_datetime_str(info.create_time),
-                "deptName": dept_name,
-                "roles": roles,
-                "roleIds": None,
-                "postIds": None,
-                "roleId": None
-            },
-            "permissions": [
-                "*:*:*"
-            ],
-            "roles": role_keys
-        }
-}
+        return {
+            "code": 200,
+            "msg": "操作成功",
+            "data": {
+                "user": {
+                    "userId": info.user_id,
+                    "tenantId": info.tenant_id,
+                    "deptId": info.dept_id,
+                    "userName": info.user_name,
+                    "nickName": info.nick_name,
+                    "userType": info.user_type,
+                    "email": info.email,
+                    "phonenumber": info.phonenumber,
+                    "sex": info.sex,
+                    "avatar": info.avatar,
+                    "status": info.status,
+                    "loginIp": info.login_ip,
+                    "loginDate": get_datetime_str(info.login_date),
+                    "remark": info.remark,
+                    "createTime": get_datetime_str(info.create_time),
+                    "deptName": dept_name,
+                    "roles": roles,
+                    "roleIds": None,
+                    "postIds": None,
+                    "roleId": None
+                },
+                "permissions": [
+                    "*:*:*"
+                ],
+                "roles": role_keys
+            }
+    }
+    except Exception as e:
+        traceback.print_exc()
+        raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
 
 
 @router.get('/deptTree')