Files
leocrm/app/routes/permission_templates.py
T
Agent Zero abbe7a18fc fix(audit): P0-P3 audit fixes — 838 ruff errors → 0, 30 F821 bugs fixed, 118 files changed
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner
- P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var
- P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup
- P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns
- P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs)
- P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import
- P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default
- P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed
- P2: 28 frontend TODOs (hardcoded constants, deprecated notification API)
- P3: dead code, duplicates, deprecated imports, private attr, __import__ inline
- P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n)
- ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix)
- F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String)
- Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
2026-08-16 01:17:18 +02:00

115 lines
3.9 KiB
Python

"""Permission template routes — CRUD API for reusable permission presets."""
from __future__ import annotations
import uuid
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import get_db
from app.deps import require_permission
from app.schemas.permission_template import (
PermissionTemplateApply,
PermissionTemplateCreate,
PermissionTemplateUpdate,
)
from app.services import permission_template_service
router = APIRouter(prefix="/api/v1/permission-templates", tags=["permission-templates"])
@router.get("")
async def list_templates(
entity_type: str | None = None,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(require_permission("permissions:templates:read")),
):
"""List all permission templates for the current tenant."""
tenant_id = uuid.UUID(current_user["tenant_id"])
items = await permission_template_service.list_templates(db, tenant_id, entity_type)
return {"items": items, "total": len(items)}
@router.post("", status_code=status.HTTP_201_CREATED)
async def create_template(
body: PermissionTemplateCreate,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(require_permission("permissions:templates:write")),
):
"""Create a new permission template."""
tenant_id = uuid.UUID(current_user["tenant_id"])
try:
return await permission_template_service.create_template(
db,
tenant_id,
name=body.name,
entity_type=body.entity_type,
level=body.level,
trigger_condition=body.trigger_condition,
auto_share_with=body.auto_share_with,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
@router.put("/{template_id}")
async def update_template(
template_id: str,
body: PermissionTemplateUpdate,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(require_permission("permissions:templates:write")),
):
"""Update an existing permission template."""
tenant_id = uuid.UUID(current_user["tenant_id"])
try:
return await permission_template_service.update_template(
db,
tenant_id,
template_id,
name=body.name,
entity_type=body.entity_type,
level=body.level,
trigger_condition=body.trigger_condition,
auto_share_with=body.auto_share_with,
)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
@router.delete("/{template_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_template(
template_id: str,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(require_permission("permissions:templates:write")),
):
"""Delete a permission template."""
tenant_id = uuid.UUID(current_user["tenant_id"])
try:
await permission_template_service.delete_template(db, tenant_id, template_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e)) from e
@router.post("/apply", status_code=status.HTTP_201_CREATED)
async def apply_template(
body: PermissionTemplateApply,
db: AsyncSession = Depends(get_db),
current_user: dict = Depends(require_permission("permissions:templates:write")),
):
"""Apply a permission template to an entity, creating entity_permissions."""
tenant_id = uuid.UUID(current_user["tenant_id"])
user_id = uuid.UUID(current_user["user_id"])
try:
result = await permission_template_service.apply_template(
db,
tenant_id,
body.entity_type,
body.entity_id,
template_id=body.template_id,
created_by=user_id,
)
return {"applied": result, "count": len(result)}
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e)) from e