feat: 后端支持托管到 Metrix,启动自动安装并接入平台登录
This commit is contained in:
+22
-3
@@ -2,8 +2,9 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from fastapi import Header, HTTPException
|
||||
from fastapi import Header, HTTPException, Request
|
||||
|
||||
from app.auth.platform import platform_identity
|
||||
from app.security import read_token
|
||||
|
||||
|
||||
@@ -15,13 +16,31 @@ class Actor:
|
||||
name: str
|
||||
|
||||
|
||||
def current_actor(authorization: str | None = Header(default=None)) -> Actor:
|
||||
def current_actor(request: Request, authorization: str | None = Header(default=None)) -> Actor:
|
||||
if not authorization or not authorization.lower().startswith("bearer "):
|
||||
raise HTTPException(status_code=401, detail="请先登录")
|
||||
body = read_token(authorization.split(" ", 1)[1].strip())
|
||||
if body is None:
|
||||
raise HTTPException(status_code=401, detail="登录已失效")
|
||||
return Actor(str(body.get("kind") or ""), str(body.get("id") or ""), str(body.get("role") or "user"), str(body.get("name") or ""))
|
||||
actor = Actor(str(body.get("kind") or ""), str(body.get("id") or ""), str(body.get("role") or "user"), str(body.get("name") or ""))
|
||||
if actor.kind == "platform":
|
||||
_check_platform(request, actor)
|
||||
return actor
|
||||
|
||||
|
||||
def _check_platform(request: Request, actor: Actor) -> None:
|
||||
"""平台账号的令牌跟随平台登录:换人、退出或角色变化后即失效。"""
|
||||
identity = platform_identity(request.headers)
|
||||
if identity is None or identity.user_id != actor.id or identity.role != actor.role:
|
||||
raise HTTPException(status_code=401, detail="平台登录已变化,请重新进入")
|
||||
if actor.role == "user":
|
||||
from app.db import session_scope
|
||||
from app.models import AppSetting
|
||||
|
||||
with session_scope() as session:
|
||||
setting = session.get(AppSetting, 1)
|
||||
if setting is not None and setting.login_mode != "local":
|
||||
raise HTTPException(status_code=401, detail="当前使用大数据平台账号登录")
|
||||
|
||||
|
||||
def require_admin(actor: Actor) -> None:
|
||||
|
||||
Reference in New Issue
Block a user