feature/bitacora-correccion-filtro-tenant

This commit is contained in:
2026-05-06 12:12:09 -06:00
parent 86a3d703ba
commit 8c8cd0e247
4 changed files with 166 additions and 63 deletions

View File

@@ -25,6 +25,19 @@ from api.v1.modules.a76.general_catalogs.company.models import Company
router = APIRouter()
def _audit_scope_tenant_id(db: Session, company_id: int) -> int:
"""Tenant_id de la fila ``Company`` para filtrar ``audit_logs`` (alineado con lo persistido)."""
row = (
db.query(Company.tenant_id)
.filter(Company.id == company_id, Company.deleted_at.is_(None))
.first()
)
if not row:
raise HTTPException(status_code=404, detail="Company not found")
return int(row[0])
_SEGMENT_LABELS = {
"tenants": "Espacio",
"companies": "Companias",
@@ -189,16 +202,17 @@ async def get_bitacora(
"""
Bitácora por compañía. Requiere permiso ``audit_logs.view``.
"""
tenant_id = validate_access_to_resource(
validate_access_to_resource(
db,
company_id,
current_user,
required_permissions=["audit_logs.view"],
)
scope_tenant_id = _audit_scope_tenant_id(db, company_id)
query = db.query(AuditLog).filter(
AuditLog.company_id == company_id,
AuditLog.tenant_id == tenant_id,
AuditLog.tenant_id == scope_tenant_id,
)
# Filters
@@ -250,18 +264,19 @@ async def get_procedures(
"""
Lista de procedimientos para filtros (alcance compañía). Requiere ``audit_logs.view``.
"""
tenant_id = validate_access_to_resource(
validate_access_to_resource(
db,
company_id,
current_user,
required_permissions=["audit_logs.view"],
)
scope_tenant_id = _audit_scope_tenant_id(db, company_id)
results = (
db.query(AuditLog.procedure)
.filter(
AuditLog.company_id == company_id,
AuditLog.tenant_id == tenant_id,
AuditLog.tenant_id == scope_tenant_id,
)
.distinct()
.order_by(AuditLog.procedure)
@@ -279,19 +294,20 @@ async def get_audit_detail(
"""
Detalle de un registro de bitácora. Requiere ``audit_logs.view``.
"""
tenant_id = validate_access_to_resource(
validate_access_to_resource(
db,
company_id,
current_user,
required_permissions=["audit_logs.view"],
)
scope_tenant_id = _audit_scope_tenant_id(db, company_id)
log = (
db.query(AuditLog)
.filter(
AuditLog.spec_id == spec_id,
AuditLog.company_id == company_id,
AuditLog.tenant_id == tenant_id,
AuditLog.tenant_id == scope_tenant_id,
)
.first()
)
@@ -316,13 +332,14 @@ async def list_tenant_files(
if not should_ensure_s3_bucket():
raise HTTPException(status_code=400, detail="S3 storage is disabled")
tenant_id = validate_access_to_resource(
validate_access_to_resource(
db,
company_id,
current_user,
required_permissions=["audit_logs.view"],
)
tenant_prefix = _tenant_prefix(tenant_id)
scope_tenant_id = _audit_scope_tenant_id(db, company_id)
tenant_prefix = _tenant_prefix(scope_tenant_id)
rel_path = _normalize_relative_path(path)
list_prefix = f"{tenant_prefix}{rel_path}/" if rel_path else tenant_prefix
@@ -342,7 +359,7 @@ async def list_tenant_files(
continuation_token=continuation_token,
)
company_names = _companies_map(db, tenant_id)
company_names = _companies_map(db, scope_tenant_id)
folders: List[AuditFileFolderItem] = []
for prefix in data.get("prefixes", []):
rel = _relative_from_tenant_prefix(prefix, tenant_prefix)
@@ -410,13 +427,14 @@ async def download_tenant_file(
if not should_ensure_s3_bucket():
raise HTTPException(status_code=400, detail="S3 storage is disabled")
tenant_id = validate_access_to_resource(
validate_access_to_resource(
db,
company_id,
current_user,
required_permissions=["audit_logs.view"],
)
tenant_prefix = _tenant_prefix(tenant_id)
scope_tenant_id = _audit_scope_tenant_id(db, company_id)
tenant_prefix = _tenant_prefix(scope_tenant_id)
rel_path = _normalize_relative_path(path)
if not rel_path or rel_path.endswith("/"):
raise HTTPException(status_code=400, detail="A file path is required")