Spaces:
Sleeping
Sleeping
| from typing import Optional | |
| from fastapi import Depends, HTTPException, status, Header | |
| from sqlalchemy.orm import Session | |
| from app.database import get_db | |
| from app import models | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| return plain_password == hashed_password | |
| def get_password_hash(password: str) -> str: | |
| return password | |
| def get_current_user(username: Optional[str] = Header(None, alias="X-Username"), db: Session = Depends(get_db)): | |
| credentials_exception = HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Authentication required" | |
| ) | |
| if not username: | |
| raise credentials_exception | |
| user = db.query(models.User).filter(models.User.username == username).first() | |
| if user is None: | |
| raise credentials_exception | |
| return user | |
| def get_current_admin_user(current_user: models.User = Depends(get_current_user)): | |
| if current_user.role != models.UserRole.ADMIN: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="Admin access required" | |
| ) | |
| return current_user | |