28 lines
972 B
Python
28 lines
972 B
Python
"""
|
||
通用依赖项模块,包含常用的FastAPI依赖项函数
|
||
"""
|
||
|
||
from fastapi import Depends, HTTPException, status
|
||
from fastapi.security import OAuth2PasswordBearer
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.models.database import get_db
|
||
from app.services.user import UserService
|
||
from app.schemas.user import UserResponse
|
||
|
||
# OAuth2密码Bearer
|
||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/users/login")
|
||
|
||
|
||
async def get_current_active_user(db: Session = Depends(get_db), token: str = Depends(oauth2_scheme)):
|
||
"""获取当前活跃用户"""
|
||
user = UserService.get_current_user(db, token)
|
||
if not user:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
detail="Could not validate credentials",
|
||
headers={"WWW-Authenticate": "Bearer"},
|
||
)
|
||
if hasattr(user, 'status') and user.status != "active":
|
||
raise HTTPException(status_code=400, detail="Inactive user")
|
||
return user |