sprint2+3: remaining services visibility filter + search provider permission-aware + dashboard route
Check Cross-Plugin Imports / check (push) Has been cancelled

This commit is contained in:
Agent Zero
2026-07-29 02:05:14 +02:00
parent 52a5c347de
commit 517e1b6d8b
9 changed files with 370 additions and 78 deletions
+28 -3
View File
@@ -9,6 +9,7 @@ from typing import Any
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.visibility import apply_visibility_filter, check_single_entity_access
from app.models.entity_history import EntityHistory
@@ -33,6 +34,7 @@ async def record_history(
snapshot_before=snapshot_before,
snapshot_after=snapshot_after,
changes=changes,
owner_id=user_id,
)
db.add(entry)
await db.flush()
@@ -45,6 +47,8 @@ async def get_entity_history(
entity_type: str,
entity_id: uuid.UUID,
limit: int = 50,
user_id: uuid.UUID | None = None,
is_system_admin: bool = False,
) -> list[EntityHistory]:
"""Get all history entries for an entity, newest first."""
q = (
@@ -57,6 +61,10 @@ async def get_entity_history(
.order_by(EntityHistory.created_at.desc())
.limit(limit)
)
if user_id and not is_system_admin:
q = await apply_visibility_filter(
db, q, "entity_history", EntityHistory, user_id, tenant_id, is_system_admin
)
result = await db.execute(q)
return list(result.scalars().all())
@@ -65,6 +73,8 @@ async def get_history_entry(
db: AsyncSession,
tenant_id: uuid.UUID,
history_id: uuid.UUID,
user_id: uuid.UUID | None = None,
is_system_admin: bool = False,
) -> EntityHistory | None:
"""Get a specific history entry by ID."""
q = select(EntityHistory).where(
@@ -72,7 +82,16 @@ async def get_history_entry(
EntityHistory.tenant_id == tenant_id,
)
result = await db.execute(q)
return result.scalar_one_or_none()
entry = result.scalar_one_or_none()
if entry is None:
return None
if user_id and not is_system_admin:
has_access = await check_single_entity_access(
db, "entity_history", entry.id, user_id, tenant_id, "read", is_system_admin
)
if not has_access:
raise PermissionError("No access")
return entry
async def restore_from_history(
@@ -80,6 +99,7 @@ async def restore_from_history(
tenant_id: uuid.UUID,
history_id: uuid.UUID,
user_id: uuid.UUID,
is_system_admin: bool = False,
) -> dict[str, Any]:
"""Restore an entity to a previous snapshot state.
@@ -89,7 +109,7 @@ async def restore_from_history(
Returns the restored data dict.
"""
entry = await get_history_entry(db, tenant_id, history_id)
entry = await get_history_entry(db, tenant_id, history_id, user_id, is_system_admin)
if entry is None:
raise ValueError("History entry not found")
@@ -175,6 +195,7 @@ async def undo_last_action(
user_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
is_system_admin: bool = False,
) -> dict[str, Any]:
"""Undo the most recent action for an entity.
@@ -191,9 +212,13 @@ async def undo_last_action(
.order_by(EntityHistory.created_at.desc())
.limit(1)
)
if not is_system_admin:
q = await apply_visibility_filter(
db, q, "entity_history", EntityHistory, user_id, tenant_id, is_system_admin
)
result = await db.execute(q)
entry = result.scalar_one_or_none()
if entry is None:
raise ValueError("No history found for this entity")
return await restore_from_history(db, tenant_id, entry.id, user_id)
return await restore_from_history(db, tenant_id, entry.id, user_id, is_system_admin)