浏览代码

统一认证平台用户信息返回角色列表

lingmin_package@163.com 3 周之前
父节点
当前提交
64600d90bc
共有 1 个文件被更改,包括 16 次插入2 次删除
  1. 16 2
      src/app/services/oauth_service.py

+ 16 - 2
src/app/services/oauth_service.py

@@ -135,7 +135,7 @@ class OAuthService:
         return token
 
     async def get_user_info(self, user_id: str, scope: list) -> Optional[Dict[str, Any]]:
-        """获取用户信息(根据scope过滤)"""
+        """获取用户信息(根据scope过滤),包含角色信息"""
         conn = get_db_connection()
         if not conn:
             return None
@@ -157,8 +157,22 @@ class OAuthService:
             if not user_data:
                 return None
 
+            # 查询用户角色(同时返回 name 和 code)
+            cursor.execute("""
+                SELECT r.name, r.code
+                FROM t_sys_role r
+                INNER JOIN t_sys_user_role ur ON r.id = ur.role_id
+                WHERE ur.user_id = %s
+                  AND ur.is_active = 1
+                  AND r.is_active = 1
+                  AND (ur.expires_at IS NULL OR ur.expires_at > NOW())
+            """, (user_id,))
+
+            role_rows = cursor.fetchall()
+            roles = [{"name": row["name"], "code": row["code"]} for row in role_rows]
+
             # 构建用户信息响应(根据scope过滤)
-            user_info = {"sub": user_data["id"]}
+            user_info = {"sub": user_data["id"], "roles": roles}
 
             if "profile" in scope:
                 user_info.update({