105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models.database import get_db
|
|
from models.models import User
|
|
from .jwt_utils import decode_access_token
|
|
from .permissions import (
|
|
ACCOUNTS_WRITE,
|
|
MESSAGES_WRITE,
|
|
RULES_WRITE,
|
|
USERS_MANAGE,
|
|
WRITE_PERMISSIONS,
|
|
)
|
|
from .roles import can_write, has_permission, is_admin
|
|
|
|
bearer_scheme = HTTPBearer(auto_error=False)
|
|
|
|
|
|
async def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(bearer_scheme),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> User:
|
|
if not credentials or not credentials.credentials:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="未登录或令牌缺失")
|
|
payload = decode_access_token(credentials.credentials)
|
|
if not payload or not payload.get("sub"):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="登录已过期,请重新登录")
|
|
try:
|
|
user_id = int(payload["sub"])
|
|
except (TypeError, ValueError):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="无效令牌")
|
|
|
|
result = await db.execute(select(User).where(User.id == user_id))
|
|
user = result.scalar_one_or_none()
|
|
if not user or not user.is_active:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户不存在或已禁用")
|
|
return user
|
|
|
|
|
|
def require_permission(permission: str):
|
|
"""FastAPI dependency factory that checks a single permission code."""
|
|
|
|
async def _checker(user: User = Depends(get_current_user)) -> User:
|
|
if has_permission(user.role, permission):
|
|
return user
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail=f"缺少权限:{permission}",
|
|
)
|
|
|
|
return _checker
|
|
|
|
|
|
async def require_admin(user: User = Depends(get_current_user)) -> User:
|
|
if not is_admin(user.role):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="需要管理员权限")
|
|
return user
|
|
|
|
|
|
async def require_write(user: User = Depends(get_current_user)) -> User:
|
|
"""Any write-capable permission (accounts/messages/rules) or legacy can_write."""
|
|
if is_admin(user.role) or can_write(user.role):
|
|
return user
|
|
if any(has_permission(user.role, code) for code in WRITE_PERMISSIONS):
|
|
return user
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="当前角色只读,无法执行此操作",
|
|
)
|
|
|
|
|
|
async def require_accounts_write(user: User = Depends(get_current_user)) -> User:
|
|
if has_permission(user.role, ACCOUNTS_WRITE):
|
|
return user
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="缺少权限:accounts.write",
|
|
)
|
|
|
|
|
|
async def require_messages_write(user: User = Depends(get_current_user)) -> User:
|
|
if has_permission(user.role, MESSAGES_WRITE):
|
|
return user
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="缺少权限:messages.write",
|
|
)
|
|
|
|
|
|
async def require_rules_write(user: User = Depends(get_current_user)) -> User:
|
|
if has_permission(user.role, RULES_WRITE):
|
|
return user
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="缺少权限:rules.write",
|
|
)
|
|
|
|
|
|
async def require_user_manager(user: User = Depends(get_current_user)) -> User:
|
|
if has_permission(user.role, USERS_MANAGE):
|
|
return user
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="需要用户管理权限")
|