Files
shanxia2.0/backend/app/core/bx_auth.py
T
2026-06-07 00:40:01 +08:00

41 lines
1.1 KiB
Python

"""报销模块认证适配:门户 JWT 用户 → bx 业务上下文。"""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict
from fastapi import Depends
from app.core.menu_guard import require_admin_or_super_admin
@dataclass
class BxUserContext:
"""bx Service/Router 使用的当前用户上下文(sys_user.id 为 UUID 字符串)。"""
id: str
name: str
dept_id: str
role: str
tenant_no: str
def to_tenant_user_dict(self) -> Dict[str, Any]:
"""供 get_tenant_no 等门户工具使用。"""
return {
"user_id": self.id,
"role": self.role,
"tenant_no": self.tenant_no,
}
def get_bx_current_user(
user: dict = Depends(require_admin_or_super_admin),
) -> BxUserContext:
return BxUserContext(
id=(user.get("user_id") or "").strip(),
name=(user.get("real_name") or user.get("username") or "").strip(),
dept_id=(user.get("region_id") or "").strip(),
role=(user.get("role") or "").strip(),
tenant_no=(user.get("tenant_no") or "").strip(),
)