- 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
3.1 KiB
Python
94 lines
3.1 KiB
Python
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 PermissionRuleOctCreateDTO, PermissionRuleOctResponseDTO
|
|
from .services import PermissionRuleOctService
|
|
|
|
router = APIRouter(prefix="/permission-rule-oct", tags=["PermissionRuleOct"])
|
|
|
|
|
|
@router.get("/", response_model=List[PermissionRuleOctResponseDTO])
|
|
async def list_permissions(
|
|
db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""
|
|
List all PermissionRuleOct 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(PermissionRuleOctService).all()
|
|
|
|
|
|
@router.get("/{permission}", response_model=PermissionRuleOctResponseDTO)
|
|
async def read_permission(
|
|
permission: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get a specific PermissionRuleOct by its permission.
|
|
"""
|
|
# 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")
|
|
|
|
permission = PermissionRuleOctService.get_permission_by_id(db, permission)
|
|
if not permission:
|
|
raise HTTPException(status_code=404, detail="PermissionRuleOct not found")
|
|
return permission
|
|
|
|
|
|
@router.post(
|
|
"/",
|
|
response_model=PermissionRuleOctResponseDTO,
|
|
status_code=status.HTTP_201_CREATED,
|
|
)
|
|
async def create_permission(
|
|
permission_data: PermissionRuleOctCreateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Create a new PermissionRuleOct 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 PermissionRuleOctService.create_permission(db, permission_data)
|
|
|
|
|
|
@router.delete("/{permission}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_permission(
|
|
permission: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Delete a PermissionRuleOct by its permission.
|
|
"""
|
|
# 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")
|
|
|
|
permission = PermissionRuleOctService.delete_permission(db, permission)
|
|
if not permission:
|
|
raise HTTPException(status_code=404, detail="PermissionRuleOct not found")
|