Files
37duAutomation/webhost/backend/apis/admin.py
T
puddy cb04790d28 refactor: 重构项目目录结构与代码组织
1.  重构controlplane与webhost后端的包结构,按领域拆分模块到core/apis子包
2.  移动并整理工具脚本到tools子目录,统一管理运维脚本
3.  重构插件运行时、调度、作业管理等核心模块到core子包
4.  更新AGENTS.md文档,明确各代码形态的组织规范
5.  清理重复的旧文件与冗余代码
2026-09-14 12:34:54 +08:00

99 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""webhost 宿主「管理台基础」路由:统一账户登录 / 当前账号 / 登出 / 改密 / 概览。"""
from __future__ import annotations
import time
import uuid
from fastapi import APIRouter, HTTPException, Request, Response
from pydantic import BaseModel
from webhost.backend import config
from webhost.backend.core.cpapi import _cp_call, _cp_headers, _sync_licensing
from webhost.backend.core.session import (SESSION_COOKIE, _SESS, _SESS_LOCK,
_sess_account, _sess_persist)
router = APIRouter()
class _LoginIn(BaseModel):
account: str
password: str
class _PwdIn(BaseModel):
account: str
old_password: str
new_password: str
@router.get("/api/admin/me")
def admin_me(request: Request) -> dict:
"""当前登录账号是否管理员(会话 cookie)。
账号一律以**壳本机会话**为准(登录即有效);is_admin/部门 尽量从云端补齐,
云端未授信 X-Admin-User(未配 CONTROLPLANE_TOKEN)或不可达时,仅降级 is_admin=False,
不影响员工正常进入插件页(前端以 account 非空判登录)。
"""
account = _sess_account(request)
if not account:
return {"account": "", "is_admin": False, "dept": "", "nickname": ""}
try:
r = _cp_call("GET", "/api/admin/me", headers=_cp_headers(request))
if r.get("account") == account:
return r
except HTTPException:
pass
return {"account": account, "is_admin": False, "dept": "", "nickname": ""}
@router.post("/api/admin/login")
def admin_login(b: _LoginIn, response: Response) -> dict:
"""统一账户登录:校验账号+密码通过后,写会话 cookie 记为当前登录账号。"""
try:
r = _cp_call("POST", "/api/admin/auth",
headers={"X-Admin-User": b.account.strip(),
**({"X-API-Token": config.CONTROLPLANE_TOKEN} if config.CONTROLPLANE_TOKEN else {}),
"Content-Type": "application/json"},
json={"account": b.account, "password": b.password})
except HTTPException as e:
raise e
if not r.get("ok"):
return r
token = uuid.uuid4().hex
with _SESS_LOCK:
_SESS[token] = (b.account.strip(), time.time())
_sess_persist()
response.set_cookie(SESSION_COOKIE, token, httponly=True, samesite="lax")
# 登录成功即联网同步该账号的离线授权票据 + 管控清单 + 中心机公钥(供离线运行)
if config.CONTROLPLANE_URL:
try:
_sync_licensing(b.account.strip())
except Exception as _exc: # noqa: BLE001 同步失败不影响登录本身
import logging
logging.getLogger(__name__).warning("登录后同步离线授权票据失败:%s", _exc)
return {**r, "account": b.account.strip()}
@router.post("/api/admin/logout")
def admin_logout(request: Request, response: Response) -> dict:
"""登出:清除会话 cookie 与内存会话。"""
token = request.cookies.get(SESSION_COOKIE, "")
with _SESS_LOCK:
_SESS.pop(token, None)
_sess_persist()
response.delete_cookie(SESSION_COOKIE)
return {"ok": True}
@router.post("/api/admin/password")
def admin_password(b: _PwdIn, request: Request) -> dict:
return _cp_call("POST", "/api/admin/password",
headers={**_cp_headers(request), "Content-Type": "application/json"},
json=b.model_dump())
@router.get("/api/admin/overview")
def admin_overview(request: Request) -> dict:
"""管理台一次性拉取:全部插件 + 账号 + 管理员。管理入口已在中心机控制面,底座侧置空兜底。"""
return {"plugins": [], "accounts": [], "admins": []}