95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import Depends, Header, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.config import get_settings
|
|
from app.database import get_db
|
|
from app.models import Group, User, UserDepartmentAccess
|
|
from app.security import decode_access_token
|
|
|
|
|
|
settings = get_settings()
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.api_prefix}/auth/login")
|
|
|
|
|
|
def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)) -> User:
|
|
username = decode_access_token(token)
|
|
if not username:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
|
user = db.query(User).filter(User.username == username, User.is_active.is_(True)).first()
|
|
if not user:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found")
|
|
return user
|
|
|
|
|
|
def is_super_admin(user: User) -> bool:
|
|
return user.role == "super_admin"
|
|
|
|
|
|
def is_admin(user: User) -> bool:
|
|
return user.role in {"super_admin", "admin"}
|
|
|
|
|
|
def accessible_department_ids(db: Session, user: User) -> list[int] | None:
|
|
department_ids = set()
|
|
if user.primary_department_id is not None:
|
|
department_ids.add(user.primary_department_id)
|
|
department_ids.update(
|
|
item.department_id
|
|
for item in db.query(UserDepartmentAccess).filter(UserDepartmentAccess.user_id == user.id).all()
|
|
if item.department_id is not None
|
|
)
|
|
return sorted(department_ids)
|
|
|
|
|
|
def accessible_group_ids(db: Session, user: User) -> list[int] | None:
|
|
department_ids = accessible_department_ids(db, user) or [-1]
|
|
return [item[0] for item in db.query(Group.id).filter(Group.department_id.in_(department_ids)).all()]
|
|
|
|
|
|
def require_super_admin(user: User = Depends(get_current_user)) -> User:
|
|
if not is_super_admin(user):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Super admin only")
|
|
return user
|
|
|
|
|
|
def require_admin(user: User = Depends(get_current_user)) -> User:
|
|
if not is_admin(user):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin only")
|
|
return user
|
|
|
|
|
|
def ensure_department_access(db: Session, user: User, department_id: int) -> None:
|
|
if department_id not in (accessible_department_ids(db, user) or []):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
|
|
|
|
|
|
def ensure_group_access(db: Session, user: User, group_id: int) -> None:
|
|
group = db.query(Group.id, Group.department_id).filter(Group.id == group_id).first()
|
|
if not group:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Knowledge base not found.")
|
|
if group.department_id not in (accessible_department_ids(db, user) or []):
|
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
|
|
|
|
|
|
def require_integration_key(
|
|
x_integration_key: str | None = Header(default=None, alias="X-Integration-Key"),
|
|
authorization: str | None = Header(default=None),
|
|
) -> str:
|
|
configured_key = settings.integration_api_key
|
|
if not configured_key:
|
|
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Integration API is not configured.")
|
|
|
|
token = x_integration_key
|
|
if not token and authorization:
|
|
scheme, _, credentials = authorization.partition(" ")
|
|
if scheme.lower() == "bearer" and credentials:
|
|
token = credentials
|
|
|
|
if token != configured_key:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid integration key")
|
|
|
|
return token
|