- Enhanced TenantMiddleware to validate tenant information from JWT tokens. - Added LicenseValidationMiddleware to check tenant licenses before processing requests. - Updated security utilities to extract tenant information from tokens and validate company access. - Introduced CompanyStore to manage active company state and handle company switching in the frontend. - Modified API routes to include company_id in requests for better resource management. - Improved logging and error handling throughout the middleware and API layers. - Updated frontend components to reflect changes in company management and selection. - Added new API route for fetching user's companies with proper authentication handling.
94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
"""
|
|
Routes for managing Seal entries.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user
|
|
from .dto import SealCreateDTO, SealResponseDTO
|
|
from .services import SealService
|
|
|
|
router = APIRouter(prefix="/seals", tags=["Seal"])
|
|
|
|
|
|
@router.get("/", response_model=List[SealResponseDTO])
|
|
async def list_seals(
|
|
db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""
|
|
List all Seal entries.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(status_code=403, detail="Access denied: Tenant or Company not found")
|
|
|
|
return db.query(SealService).all()
|
|
|
|
|
|
@router.get("/{seal}", response_model=SealResponseDTO)
|
|
async def read_seal(
|
|
seal: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get a specific Seal by its seal.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(status_code=403, detail="Access denied: Tenant or Company not found")
|
|
|
|
seal = SealService.get_seal_by_id(db, seal)
|
|
if not seal:
|
|
raise HTTPException(status_code=404, detail="Seal not found")
|
|
return seal
|
|
|
|
|
|
@router.post("/", response_model=SealResponseDTO, status_code=status.HTTP_201_CREATED)
|
|
async def create_seal(
|
|
seal_data: SealCreateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Create a new Seal entry.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(status_code=403, detail="Access denied: Tenant or Company not found")
|
|
|
|
return SealService.create_seal(db, seal_data)
|
|
|
|
|
|
@router.delete("/{seal}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_seal(
|
|
seal: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Delete a Seal by its seal.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(status_code=403, detail="Access denied: Tenant or Company not found")
|
|
|
|
seal = SealService.delete_seal(db, seal)
|
|
if not seal:
|
|
raise HTTPException(status_code=404, detail="Seal not found")
|