# -*- coding: utf-8 -*- """webhost 宿主「业务元数据与平台登录」路由:/api/meta 聚合 + 平台真实登录/登出。""" from __future__ import annotations from fastapi import APIRouter, HTTPException, Request from webhost.backend import auth as platform_auth from webhost.backend import config from webhost.backend import state as _st from webhost.backend import meta as _meta from webhost.backend.plugins import PLUGINS from webhost.backend.core.session import _require_login, _sess_account router = APIRouter() @router.get("/api/meta") def meta(request: Request) -> dict: account = _sess_account(request) # 插件清单仅对已登录账号可见(未登录不泄露插件元数据); # 已登录且能按云端权限(authorization)解析时,仅返回该员工被授权插件(§2.2 权限隔离)。 if account: allowed = _meta._cloud_plugin_ids(account) plugins = ([_meta._plugin_schema(p) for p in PLUGINS if p.id in allowed] if allowed is not None # 云端按员工返回了授权集(也可能是空集=无权限) else [_meta._plugin_schema(p) for p in PLUGINS]) # 无 token/云端不可达 → 离线兜底全量 else: plugins = [] allowed_plats = _meta._account_allowed_platforms(account) if account else None platforms = [{"id": pid, "label": m["label"], "online": platform_auth.is_online(pid), # 已登录时按账号白名单;未登录/中心机不可达默认展示 "allowed": (pid in allowed_plats) if allowed_plats is not None else True} for pid, m in _st.PLATFORMS.items()] return { # controlplane_url 供前端管理员跳转控制面;未配置时为空(前端隐藏管理入口) "controlplane": config.CONTROLPLANE_URL, "appName": "37du 自动化工作台", "dingtalkScan": bool(config.DINGTALK_APP_KEY), "platforms": platforms, "plugins": plugins, } @router.post("/api/platforms/{pid}/login") def platform_login(pid: str, request: Request) -> dict: """真实登录:需先通过统一账户登录(有会话),且该账号已获中心机平台授权。""" account = _require_login(request) plat = _st.PLATFORMS.get(pid) if not plat: raise HTTPException(404, "平台不存在") allowed = _meta._account_allowed_platforms(account) if allowed is None: raise HTTPException(503, "控制面不可达,无法校验平台权限") if pid not in allowed: raise HTTPException(403, "无该平台访问权限,请联系管理员在控制面授权") return platform_auth.login(pid) @router.post("/api/platforms/{pid}/logout") def platform_logout(pid: str, request: Request) -> dict: """登出平台:清除共享 cookies,使该平台登录态失效(需已登录统一账户)。""" _require_login(request) plat = _st.PLATFORMS.get(pid) if not plat: raise HTTPException(404, "平台不存在") return platform_auth.logout(pid)