Phase 0 Complete: Tasks 0.7-0.20

- 0.7: UI-Design-Richtlinien (docs/ui-design-guidelines.md, 535 lines)
- 0.8: Theme-Customization Backend (4 theme fields, migration 0023)
- 0.9: Theme-Customization Frontend (SettingsTheme.tsx, themeStore.ts, live preview)
- 0.10: RBAC-Audit (4 plugins secured, 53 routes with require_permission)
- 0.11: LiteLLM-Cleanup (llm_client.py migrated from httpx to litellm)
- 0.12: KI-Agent-Framework docs (plugin-development-guide.md, agent_capabilities field)
- 0.13: Heartbeat configurable (ProactiveSettings, migration 0024, frontend UI)
- 0.14: Unified Search Field-Level RBAC (resolve_permissions + filter_fields_by_permission)
- 0.15: Undo/History-System (EntityHistory model, service, routes, migration 0025, HistoryViewer)
- 0.16: Storage Backend (LocalStorage + S3Storage, DMS/attachments/mail updated)
- 0.17: Import/Export unified Contact fields (firstname, surname, email_1, phone_1)
- 0.18: .gitignore & Config-Cleanup (webui→frontend, python-jose removed, .env untracked)
- 0.19: Mail-Salt Security-Fix (per-account random salt, migration 0026)
- 0.20: AGPL replaced (PyMuPDF→pypdf, OnlyOffice→Collabora, LICENSE + THIRD_PARTY_LICENSES.md)
This commit is contained in:
Agent Zero
2026-07-23 08:42:26 +02:00
parent 3d06cb2353
commit ec81940178
65 changed files with 3061 additions and 277 deletions
+6 -12
View File
@@ -11,11 +11,9 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.audit import log_audit
from app.core.storage import get_storage_backend
from app.models.attachment import Attachment
# Storage path for uploaded files
STORAGE_PATH = os.environ.get("STORAGE_PATH", "/data/uploads")
def _attachment_to_dict(a: Attachment) -> dict[str, Any]:
"""Serialize an Attachment ORM object to dict."""
@@ -50,17 +48,13 @@ async def save_attachment(
mime_type: str,
) -> dict[str, Any]:
"""Save a file to storage and create an Attachment record."""
# Ensure storage directory exists
entity_dir = os.path.join(STORAGE_PATH, entity_type, str(entity_id))
os.makedirs(entity_dir, exist_ok=True)
# Generate unique filename
# Generate unique filename and relative storage path
unique_filename = _generate_unique_filename(filename)
file_path = os.path.join(entity_dir, unique_filename)
file_path = f"{entity_type}/{entity_id}/{unique_filename}"
# Write file to disk
with open(file_path, "wb") as f:
f.write(file_content)
# Save file via storage backend
storage = get_storage_backend()
await storage.save(file_path, file_content)
file_size = len(file_content)
+53 -3
View File
@@ -12,6 +12,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.models.contact import Contact, ContactPerson
from app.services.entity_history_service import record_history
def _compute_displayname(data: dict) -> str:
@@ -239,7 +240,15 @@ async def create_contact(
q = select(Contact).options(selectinload(Contact.contact_persons)).where(Contact.id == contact.id)
result = await db.execute(q)
contact = result.scalar_one()
return _serialize_contact_detail(contact)
serialized = _serialize_contact_detail(contact)
# Record history
await record_history(
db, tenant_id, user_id, "contact", contact.id,
action="create", snapshot_after=serialized,
)
return serialized
async def update_contact(
@@ -260,6 +269,9 @@ async def update_contact(
if not contact:
raise ValueError("Contact not found")
# Capture snapshot before update
snapshot_before = _serialize_contact_detail(contact)
# Recompute displayname if name fields changed
if any(k in data for k in ("type", "name", "firstname", "surname", "surfix")):
merged = {**_serialize_contact(contact), **data}
@@ -271,10 +283,30 @@ async def update_contact(
contact.updated_by = user_id
await db.flush()
return _serialize_contact_detail(contact)
snapshot_after = _serialize_contact_detail(contact)
# Compute changes diff
changes: dict = {}
for key, new_val in snapshot_after.items():
old_val = snapshot_before.get(key)
if old_val != new_val:
changes[key] = {"old": old_val, "new": new_val}
# Record history
await record_history(
db, tenant_id, user_id, "contact", contact.id,
action="update",
snapshot_before=snapshot_before,
snapshot_after=snapshot_after,
changes=changes or None,
)
return snapshot_after
async def delete_contact(db: AsyncSession, tenant_id: uuid.UUID, contact_id: str) -> None:
async def delete_contact(
db: AsyncSession, tenant_id: uuid.UUID, contact_id: str, user_id: uuid.UUID | None = None
) -> None:
"""Soft-delete a contact."""
q = select(Contact).where(
Contact.id == uuid.UUID(contact_id),
@@ -285,10 +317,28 @@ async def delete_contact(db: AsyncSession, tenant_id: uuid.UUID, contact_id: str
contact = result.scalar_one_or_none()
if not contact:
raise ValueError("Contact not found")
# Capture snapshot before deletion
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact_full = result2.scalar_one()
snapshot_before = _serialize_contact_detail(contact_full)
from datetime import datetime, timezone
contact.deleted_at = datetime.now(timezone.utc)
await db.flush()
# Record history
await record_history(
db, tenant_id, user_id, "contact", contact.id,
action="delete", snapshot_before=snapshot_before,
)
async def hard_delete_contact(db: AsyncSession, tenant_id: uuid.UUID, contact_id: str) -> None:
"""GDPR hard-delete a contact."""
+199
View File
@@ -0,0 +1,199 @@
"""Entity history service — record, query, restore, and undo entity snapshots."""
from __future__ import annotations
import uuid
from datetime import datetime, timezone
from typing import Any
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.entity_history import EntityHistory
async def record_history(
db: AsyncSession,
tenant_id: uuid.UUID,
user_id: uuid.UUID | None,
entity_type: str,
entity_id: uuid.UUID,
action: str,
snapshot_before: dict[str, Any] | None = None,
snapshot_after: dict[str, Any] | None = None,
changes: dict[str, Any] | None = None,
) -> EntityHistory:
"""Create a history entry for a CRUD action."""
entry = EntityHistory(
tenant_id=tenant_id,
user_id=user_id,
entity_type=entity_type,
entity_id=entity_id,
action=action,
snapshot_before=snapshot_before,
snapshot_after=snapshot_after,
changes=changes,
)
db.add(entry)
await db.flush()
return entry
async def get_entity_history(
db: AsyncSession,
tenant_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
limit: int = 50,
) -> list[EntityHistory]:
"""Get all history entries for an entity, newest first."""
q = (
select(EntityHistory)
.where(
EntityHistory.tenant_id == tenant_id,
EntityHistory.entity_type == entity_type,
EntityHistory.entity_id == entity_id,
)
.order_by(EntityHistory.created_at.desc())
.limit(limit)
)
result = await db.execute(q)
return list(result.scalars().all())
async def get_history_entry(
db: AsyncSession,
tenant_id: uuid.UUID,
history_id: uuid.UUID,
) -> EntityHistory | None:
"""Get a specific history entry by ID."""
q = select(EntityHistory).where(
EntityHistory.id == history_id,
EntityHistory.tenant_id == tenant_id,
)
result = await db.execute(q)
return result.scalar_one_or_none()
async def restore_from_history(
db: AsyncSession,
tenant_id: uuid.UUID,
history_id: uuid.UUID,
user_id: uuid.UUID,
) -> dict[str, Any]:
"""Restore an entity to a previous snapshot state.
For 'delete' actions: un-delete the entity (clear deleted_at).
For 'update' actions: revert entity fields to snapshot_before.
For 'create' actions: soft-delete the entity (undo creation).
Returns the restored data dict.
"""
entry = await get_history_entry(db, tenant_id, history_id)
if entry is None:
raise ValueError("History entry not found")
entity_type = entry.entity_type
entity_id = entry.entity_id
action = entry.action
# Import here to avoid circular imports
from app.models.contact import Contact
if entity_type == "contact":
q = select(Contact).where(
Contact.id == entity_id,
Contact.tenant_id == tenant_id,
)
result = await db.execute(q)
contact = result.scalar_one_or_none()
if action == "delete":
# Un-delete: clear deleted_at
if contact is None:
raise ValueError("Entity not found for restore")
contact.deleted_at = None
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
elif action == "update":
# Revert to snapshot_before
if contact is None:
raise ValueError("Entity not found for restore")
if entry.snapshot_before is None:
raise ValueError("No snapshot_before available for restore")
for key, value in entry.snapshot_before.items():
if hasattr(contact, key) and key not in ("id", "tenant_id", "created_at", "updated_at", "deleted_at"):
setattr(contact, key, value)
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
elif action == "create":
# Undo creation: soft-delete the entity
if contact is None:
raise ValueError("Entity not found for restore")
contact.deleted_at = datetime.now(timezone.utc)
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
raise ValueError(f"Unsupported entity type for restore: {entity_type}")
async def undo_last_action(
db: AsyncSession,
tenant_id: uuid.UUID,
user_id: uuid.UUID,
entity_type: str,
entity_id: uuid.UUID,
) -> dict[str, Any]:
"""Undo the most recent action for an entity.
Returns the restored entity data.
Raises ValueError if no history exists.
"""
q = (
select(EntityHistory)
.where(
EntityHistory.tenant_id == tenant_id,
EntityHistory.entity_type == entity_type,
EntityHistory.entity_id == entity_id,
)
.order_by(EntityHistory.created_at.desc())
.limit(1)
)
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)
+55 -40
View File
@@ -1,4 +1,8 @@
"""Import/export service — CSV import with dry-run preview, CSV/XLSX export."""
"""Import/export service — CSV import with dry-run preview, CSV/XLSX export.
Uses unified Contact model fields: firstname, surname, email_1, phone_1, mobilephone, function.
Company import creates Contact with type='company'.
"""
from __future__ import annotations
@@ -11,14 +15,14 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.audit import log_audit
from app.models.contact import Contact, ContactPerson as Company
from app.models.contact import Contact, ContactPerson
from app.services.company_service import _company_to_dict
from app.models.contact import Contact
from app.services.contact_service import _serialize_contact as _contact_to_dict
# Expected CSV columns for each entity type
# Company import creates Contact with type='company' using name field
COMPANY_COLUMNS = ["name", "industry", "phone", "email", "website", "description"]
CONTACT_COLUMNS = ["first_name", "last_name", "email", "phone", "mobile", "position", "department"]
# Contact import uses unified Contact fields
CONTACT_COLUMNS = ["firstname", "surname", "email", "phone", "mobile", "function", "department"]
def _parse_csv(content: str) -> list[dict[str, str]]:
@@ -44,9 +48,9 @@ async def import_companies(
csv_content: str,
dry_run: bool = False,
) -> dict[str, Any]:
"""Import companies from CSV. If dry_run=True, no DB changes are made.
"""Import companies from CSV as Contact with type='company'.
Returns {total, valid, invalid, errors, created (empty in dry_run)}.
Uses unified Contact model: name field for company name, email_1/phone_1 for contact info.
"""
rows = _parse_csv(csv_content)
total = len(rows)
@@ -73,29 +77,30 @@ async def import_companies(
created = []
for row in valid_rows:
company = Company(
contact = Contact(
tenant_id=tenant_id,
type="company",
name=row["name"].strip(),
industry=row.get("industry", "").strip() or None,
phone=row.get("phone", "").strip() or None,
email=row.get("email", "").strip() or None,
displayname=row["name"].strip(),
email_1=row.get("email", "").strip() or None,
phone_1=row.get("phone", "").strip() or None,
website=row.get("website", "").strip() or None,
description=row.get("description", "").strip() or None,
created_by=user_id,
updated_by=user_id,
)
db.add(company)
db.add(contact)
await db.flush()
await log_audit(
db,
tenant_id,
user_id,
"import",
"company",
company.id,
changes={"name": company.name},
"contact",
contact.id,
changes={"name": contact.name, "type": "company"},
)
created.append(_company_to_dict(company))
created.append(_contact_to_dict(contact))
return {
"total": total,
@@ -114,9 +119,10 @@ async def import_contacts(
csv_content: str,
dry_run: bool = False,
) -> dict[str, Any]:
"""Import contacts from CSV. If dry_run=True, no DB changes are made.
"""Import contacts from CSV using unified Contact model fields.
Returns {total, valid, invalid, errors, created (empty in dry_run)}.
CSV columns: firstname, surname, email, phone, mobile, function, department.
Maps to Contact fields: firstname, surname, email_1, phone_1, mobilephone, function.
"""
rows = _parse_csv(csv_content)
total = len(rows)
@@ -124,11 +130,15 @@ async def import_contacts(
errors = []
for idx, row in enumerate(rows, start=1):
row_errors = _validate_row(row, ["first_name", "last_name"])
if row_errors:
for e in row_errors:
errors.append({"row": idx, "error": e})
# Accept both old (first_name/last_name) and new (firstname/surname) column names
firstname = (row.get("firstname") or row.get("first_name") or "").strip()
surname = (row.get("surname") or row.get("last_name") or "").strip()
if not firstname and not surname:
errors.append({"row": idx, "error": "Missing required field: firstname or surname"})
else:
# Normalize row to use unified field names
row["firstname"] = firstname
row["surname"] = surname
valid_rows.append(row)
if dry_run:
@@ -145,13 +155,14 @@ async def import_contacts(
for row in valid_rows:
contact = Contact(
tenant_id=tenant_id,
first_name=row["first_name"].strip(),
last_name=row["last_name"].strip(),
email=row.get("email", "").strip() or None,
phone=row.get("phone", "").strip() or None,
mobile=row.get("mobile", "").strip() or None,
position=row.get("position", "").strip() or None,
department=row.get("department", "").strip() or None,
type="person",
firstname=row["firstname"].strip() or None,
surname=row["surname"].strip() or None,
displayname=f"{row['firstname']} {row['surname']}",
email_1=row.get("email", "").strip() or None,
phone_1=row.get("phone", "").strip() or None,
mobilephone=row.get("mobile", "").strip() or None,
function=row.get("function", "").strip() or None,
created_by=user_id,
updated_by=user_id,
)
@@ -164,7 +175,7 @@ async def import_contacts(
"import",
"contact",
contact.id,
changes={"first_name": contact.first_name, "last_name": contact.last_name},
changes={"firstname": contact.firstname, "surname": contact.surname},
)
created.append(_contact_to_dict(contact))
@@ -206,14 +217,14 @@ async def export_contacts_csv(
db: AsyncSession,
tenant_id: uuid.UUID,
) -> str:
"""Export contacts as CSV string."""
"""Export contacts as CSV string using unified Contact model fields."""
q = (
select(Contact)
.where(
Contact.tenant_id == tenant_id,
Contact.deleted_at.is_(None),
)
.order_by(Contact.last_name, Contact.first_name)
.order_by(Contact.surname, Contact.firstname)
)
result = await db.execute(q)
contacts = result.scalars().all()
@@ -221,19 +232,23 @@ async def export_contacts_csv(
output = io.StringIO()
writer = csv.writer(output)
writer.writerow(
["id", "first_name", "last_name", "email", "phone", "mobile", "position", "department"]
["id", "type", "firstname", "surname", "name", "email", "phone", "mobile", "function", "city", "postalcode", "country"]
)
for c in contacts:
writer.writerow(
[
str(c.id),
c.first_name,
c.last_name,
c.email or "",
c.phone or "",
c.mobile or "",
c.position or "",
c.department or "",
c.type or "person",
c.firstname or "",
c.surname or "",
c.name or "",
c.email_1 or "",
c.phone_1 or "",
c.mobilephone or "",
c.function or "",
c.mailing_city or "",
c.mailing_postalcode or "",
c.mailing_country or "",
]
)
return output.getvalue()
+9
View File
@@ -34,6 +34,10 @@ def _settings_to_dict(s: SystemSettings) -> dict[str, Any]:
"invoice_prefix": s.invoice_prefix,
"quote_prefix": s.quote_prefix,
"payment_terms_days": s.payment_terms_days,
"theme_primary_color": s.theme_primary_color,
"theme_accent_color": s.theme_accent_color,
"theme_font_family": s.theme_font_family,
"theme_border_radius": s.theme_border_radius,
"created_at": s.created_at.isoformat() if s.created_at else None,
"updated_at": s.updated_at.isoformat() if s.updated_at else None,
}
@@ -106,6 +110,10 @@ async def upsert_system_settings(
invoice_prefix=data.get("invoice_prefix", "RE-"),
quote_prefix=data.get("quote_prefix", "AN-"),
payment_terms_days=data.get("payment_terms_days", 14),
theme_primary_color=data.get("theme_primary_color", "#2563eb"),
theme_accent_color=data.get("theme_accent_color", "#d946ef"),
theme_font_family=data.get("theme_font_family", "Inter"),
theme_border_radius=data.get("theme_border_radius", "0.5rem"),
)
db.add(settings)
await db.flush()
@@ -122,6 +130,7 @@ async def upsert_system_settings(
"company_zip", "company_country", "tax_number", "vat_id", "iban",
"bic", "bank_name", "ceo", "trade_register",
"invoice_prefix", "quote_prefix", "payment_terms_days",
"theme_primary_color", "theme_accent_color", "theme_font_family", "theme_border_radius",
)
for field in all_fields:
if field in data and data[field] is not None: