Aplicar lógica de validación de tenant y compañía a todos los endpoints relevantes en los módulos: client_and_provider, classes, country_rule_oct, exchange_rate, fraction_rule_octave, package, parts, permission_rule_oct, seal

This commit is contained in:
2025-11-09 18:13:08 -06:00
parent 9b01632fde
commit 870bc36590
21 changed files with 689 additions and 2 deletions

View File

@@ -8,6 +8,9 @@ from jose import jwt, JWTError
from typing import Optional, Dict, Any
from .config import settings
import logging
from sqlalchemy.orm import Session
from core.database import get_core_db
from api.v1.modules.a76.user_tenant.models import UserTenant
logger = logging.getLogger(__name__)
@@ -75,16 +78,33 @@ def verify_token(token: str) -> Dict[str, Any]:
async def get_current_user(
credentials: HTTPAuthorizationCredentials = Security(security)
credentials: HTTPAuthorizationCredentials = Security(security),
db: Session = Depends(get_core_db)
) -> Dict[str, Any]:
"""
Dependency para obtener el usuario actual desde el token JWT
Enriquecido con tenant_id y company_id desde la tabla user_tenant
Uso en FastAPI:
current_user: dict = Depends(get_current_user)
"""
token = credentials.credentials
user_info = verify_token(token)
# Obtener user_id desde el token
user_id = user_info.get("sub")
if not user_id:
raise HTTPException(status_code=401, detail="User ID not found in token")
# Consultar la tabla user_tenant para obtener tenant_id y company_id
user_tenant = db.query(UserTenant).filter(UserTenant.keycloak_user_id == user_id).first()
if not user_tenant:
raise HTTPException(status_code=403, detail="User does not have access to any tenant or company")
# Enriquecer user_info con tenant_id y company_id
user_info["tenant_id"] = user_tenant.tenant_id
user_info["company_id"] = user_tenant.company_id
return user_info