from fastapi import Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from sqlalchemy.orm import Session from app.db.session import get_db from app.core.security import verify_token from app.models.user import User, UserRole security = HTTPBearer() def get_current_user( db: Session = Depends(get_db), credentials: HTTPAuthorizationCredentials = Depends(security), ) -> User: credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) token = credentials.credentials email = verify_token(token) if email is None: raise credentials_exception user = db.query(User).filter(User.email == email).first() if user is None: raise credentials_exception return user def get_current_active_user(current_user: User = Depends(get_current_user)) -> User: if not current_user.is_active: raise HTTPException(status_code=400, detail="Inactive user") return current_user def get_current_admin_user( current_user: User = Depends(get_current_active_user), ) -> User: if current_user.role not in [UserRole.ADMIN, UserRole.SUPER_ADMIN]: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions" ) return current_user def get_current_super_admin_user( current_user: User = Depends(get_current_active_user), ) -> User: if current_user.role != UserRole.SUPER_ADMIN: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Super admin access required" ) return current_user