Files
video-gen/video-gen-api/app/dependencies.py
T
root 4375e4980c 1、生成邀请码默认过期时间设置一天
2、邀请链接内容在列表没有显示完全,复制也没有生效
3、检查用户通过邀请链接访问是否未注册需要注册登陆,如果有账户直接登陆直接进行申请,如果已经登陆直接弹窗显示是否加入具体团队,避免单用户多次提交申请
4、如果团队负责人有未处理的加入申请,弹窗通知
2026-07-07 10:53:34 +08:00

107 lines
3.0 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 app.models.base import async_session
from app.models.user import User
from app.services.auth import decode_access_token, user_must_set_password
security = HTTPBearer(auto_error=False)
async def get_db():
async with async_session() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def get_current_user_allow_password_pending(
credentials: HTTPAuthorizationCredentials | None = Depends(security),
db: AsyncSession = Depends(get_db),
) -> User:
if not credentials:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="未登录",
)
user_id = decode_access_token(credentials.credentials)
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="登录已过期",
)
# Skip captcha tokens
if user_id.startswith("captcha:"):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="无效的凭证",
)
result = await db.execute(select(User).where(User.id == user_id).limit(1))
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
async def get_current_user(
current_user: User = Depends(get_current_user_allow_password_pending),
) -> User:
if user_must_set_password(current_user):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail={
"code": "PASSWORD_REQUIRED",
"message": "请先设置登录密码",
},
)
return current_user
async def get_optional_current_user(
credentials: HTTPAuthorizationCredentials | None = Depends(security),
db: AsyncSession = Depends(get_db),
) -> User | None:
if not credentials:
return None
user_id = decode_access_token(credentials.credentials)
if not user_id:
return None
if user_id.startswith("captcha:"):
return None
result = await db.execute(select(User).where(User.id == user_id).limit(1))
user = result.scalar_one_or_none()
if not user or not user.is_active:
return None
if user_must_set_password(user):
return None
return user
async def get_admin_user(
current_user: User = Depends(get_current_user_allow_password_pending),
) -> User:
if not current_user.is_admin or current_user.user_type != "admin":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="需要管理员权限",
)
return current_user