feat: manage tenant appearance defaults
This commit is contained in:
@@ -18,6 +18,7 @@ from govoplan_core.core.access import (
|
||||
TenantContextSwitcher,
|
||||
)
|
||||
from govoplan_core.core.change_sequence import ChangeSequenceEntry, decode_sequence_watermark, encode_sequence_watermark, record_change, sequence_watermark_is_expired
|
||||
from govoplan_core.core.appearance import APPEARANCE_SETTINGS_KEY, appearance_settings, resolve_effective_appearance, update_appearance_settings
|
||||
from govoplan_core.core.module_entitlements import MODULE_ENTITLEMENTS_KEY
|
||||
from govoplan_core.core.navigation import (
|
||||
navigation_preferences_from_settings,
|
||||
@@ -80,7 +81,7 @@ TENANT_SETTINGS_COLLECTION = "tenancy.tenant_settings"
|
||||
TENANT_SETTINGS_RESOURCE = "tenant_settings_section"
|
||||
ADMIN_MODULE_ID = "admin"
|
||||
ADMIN_SYSTEM_SETTINGS_COLLECTION = "admin.system_settings"
|
||||
TENANT_SETTINGS_SECTIONS = ("identity", "locale", "languages", "navigation", "settings")
|
||||
TENANT_SETTINGS_SECTIONS = ("identity", "locale", "languages", "navigation", "appearance", "settings")
|
||||
TENANT_NON_STATUS_UPDATE_FIELDS = {
|
||||
"name",
|
||||
"description",
|
||||
@@ -256,6 +257,13 @@ def _tenant_settings_item(session: Session, tenant: Tenant) -> TenantSettingsIte
|
||||
system_enabled = system_enabled_language_codes(system_settings.settings, default_locale=system_settings.default_locale)
|
||||
enabled = tenant_enabled_language_codes(tenant.settings, system_enabled, default_locale=tenant.default_locale)
|
||||
navigation = navigation_preferences_from_settings(tenant.settings)
|
||||
system_palette, system_locked = appearance_settings(system_settings.settings)
|
||||
tenant_palette, tenant_locked = appearance_settings(tenant.settings)
|
||||
effective_appearance = resolve_effective_appearance(
|
||||
system_settings=system_settings.settings,
|
||||
tenant_settings=tenant.settings,
|
||||
user_settings={},
|
||||
)
|
||||
return TenantSettingsItem(
|
||||
id=tenant.id,
|
||||
slug=tenant.slug,
|
||||
@@ -265,6 +273,12 @@ def _tenant_settings_item(session: Session, tenant: Tenant) -> TenantSettingsIte
|
||||
system_enabled_language_codes=system_enabled,
|
||||
enabled_language_codes=enabled,
|
||||
navigation=navigation.as_dict() if navigation is not None else None,
|
||||
appearance_palette=tenant_palette,
|
||||
appearance_palette_locked=tenant_locked,
|
||||
system_appearance_palette=system_palette or "default",
|
||||
system_appearance_palette_locked=system_locked,
|
||||
effective_appearance_palette=effective_appearance.palette,
|
||||
effective_appearance_source=effective_appearance.source,
|
||||
settings=tenant.settings or {},
|
||||
)
|
||||
|
||||
@@ -280,6 +294,14 @@ def _tenant_settings_sections(item: TenantSettingsItem) -> dict[str, Any]:
|
||||
"enabled_language_codes": payload["enabled_language_codes"],
|
||||
},
|
||||
"navigation": payload["navigation"],
|
||||
"appearance": {
|
||||
"appearance_palette": payload["appearance_palette"],
|
||||
"appearance_palette_locked": payload["appearance_palette_locked"],
|
||||
"system_appearance_palette": payload["system_appearance_palette"],
|
||||
"system_appearance_palette_locked": payload["system_appearance_palette_locked"],
|
||||
"effective_appearance_palette": payload["effective_appearance_palette"],
|
||||
"effective_appearance_source": payload["effective_appearance_source"],
|
||||
},
|
||||
"settings": payload["settings"],
|
||||
}
|
||||
|
||||
@@ -628,7 +650,7 @@ def create_tenant(
|
||||
name=payload.name.strip(),
|
||||
description=payload.description.strip() if payload.description else None,
|
||||
default_locale=payload.default_locale.strip() or system_defaults.default_locale,
|
||||
settings=payload.settings,
|
||||
settings={key: value for key, value in payload.settings.items() if key != APPEARANCE_SETTINGS_KEY},
|
||||
allow_custom_groups=payload.allow_custom_groups,
|
||||
allow_custom_roles=payload.allow_custom_roles,
|
||||
allow_api_keys=payload.allow_api_keys,
|
||||
@@ -678,11 +700,10 @@ def _apply_tenant_content_updates(tenant: Tenant, payload: TenantUpdateRequest)
|
||||
if payload.settings is not None:
|
||||
current_settings = dict(tenant.settings or {})
|
||||
next_settings = dict(payload.settings)
|
||||
next_settings.pop(MODULE_ENTITLEMENTS_KEY, None)
|
||||
if MODULE_ENTITLEMENTS_KEY in current_settings:
|
||||
next_settings[MODULE_ENTITLEMENTS_KEY] = current_settings[
|
||||
MODULE_ENTITLEMENTS_KEY
|
||||
]
|
||||
for reserved_key in (MODULE_ENTITLEMENTS_KEY, APPEARANCE_SETTINGS_KEY):
|
||||
next_settings.pop(reserved_key, None)
|
||||
if reserved_key in current_settings:
|
||||
next_settings[reserved_key] = current_settings[reserved_key]
|
||||
tenant.settings = next_settings
|
||||
|
||||
|
||||
@@ -945,8 +966,8 @@ def get_tenant_settings_delta(
|
||||
return _full_tenant_settings_delta_response(session, tenant)
|
||||
changed = set()
|
||||
for entry in entries:
|
||||
if entry.module_id == ADMIN_MODULE_ID and entry.collection == ADMIN_SYSTEM_SETTINGS_COLLECTION and entry.resource_id == "languages":
|
||||
changed.add("languages")
|
||||
if entry.module_id == ADMIN_MODULE_ID and entry.collection == ADMIN_SYSTEM_SETTINGS_COLLECTION and entry.resource_id in {"languages", "appearance"}:
|
||||
changed.add(entry.resource_id)
|
||||
elif entry.resource_type == TENANT_SETTINGS_RESOURCE:
|
||||
changed.add(entry.resource_id)
|
||||
changed_sections = [section for section in TENANT_SETTINGS_SECTIONS if section in changed]
|
||||
@@ -974,6 +995,28 @@ def update_tenant_settings(
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Tenant not found")
|
||||
before_sections = _tenant_settings_sections(_tenant_settings_item(session, tenant))
|
||||
system_settings = get_system_settings(session)
|
||||
tenant_palette, tenant_locked = appearance_settings(tenant.settings)
|
||||
system_palette, system_locked = appearance_settings(system_settings.settings)
|
||||
appearance_palette_changed = (
|
||||
"appearance_palette" in payload.model_fields_set
|
||||
and payload.appearance_palette != tenant_palette
|
||||
)
|
||||
appearance_lock_changed = (
|
||||
"appearance_palette_locked" in payload.model_fields_set
|
||||
and payload.appearance_palette_locked != tenant_locked
|
||||
)
|
||||
if system_locked and (appearance_palette_changed or appearance_lock_changed):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
|
||||
detail=f"The system appearance policy locks palette {system_palette or 'default'}.",
|
||||
)
|
||||
if (
|
||||
appearance_lock_changed or (tenant_locked and appearance_palette_changed)
|
||||
) and not has_scope(principal, "admin:policies:write"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
|
||||
detail="Changing the tenant appearance lock or its locked value requires admin:policies:write.",
|
||||
)
|
||||
system_enabled = system_enabled_language_codes(system_settings.settings, default_locale=system_settings.default_locale)
|
||||
current_i18n = i18n_settings(tenant.settings)
|
||||
raw_enabled = payload.enabled_language_codes if "enabled_language_codes" in payload.model_fields_set else current_i18n.get("enabled_language_codes")
|
||||
@@ -985,6 +1028,13 @@ def update_tenant_settings(
|
||||
)
|
||||
tenant.default_locale = payload.default_locale.strip() or enabled[0]
|
||||
tenant.settings = update_i18n_settings(tenant.settings, enabled_language_codes=enabled)
|
||||
if {"appearance_palette", "appearance_palette_locked"}.intersection(payload.model_fields_set):
|
||||
current_palette, current_locked = appearance_settings(tenant.settings)
|
||||
tenant.settings = update_appearance_settings(
|
||||
tenant.settings,
|
||||
default_palette=payload.appearance_palette if "appearance_palette" in payload.model_fields_set else current_palette,
|
||||
palette_locked=payload.appearance_palette_locked if payload.appearance_palette_locked is not None else current_locked,
|
||||
)
|
||||
if "navigation" in payload.model_fields_set:
|
||||
tenant.settings = update_navigation_preferences(
|
||||
tenant.settings,
|
||||
@@ -1002,6 +1052,9 @@ def update_tenant_settings(
|
||||
"default_locale": tenant.default_locale,
|
||||
"enabled_language_codes": enabled,
|
||||
"navigation_updated": "navigation" in payload.model_fields_set,
|
||||
"appearance_updated": bool({"appearance_palette", "appearance_palette_locked"}.intersection(payload.model_fields_set)),
|
||||
"appearance_palette": appearance_settings(tenant.settings)[0],
|
||||
"appearance_palette_locked": appearance_settings(tenant.settings)[1],
|
||||
},
|
||||
)
|
||||
after_sections = _tenant_settings_sections(_tenant_settings_item(session, tenant))
|
||||
|
||||
@@ -138,6 +138,12 @@ class TenantSettingsItem(BaseModel):
|
||||
system_enabled_language_codes: list[str] = Field(default_factory=list)
|
||||
enabled_language_codes: list[str] = Field(default_factory=list)
|
||||
navigation: NavigationPreferencesPayload | None = None
|
||||
appearance_palette: Literal["default", "civic_blue", "forest", "plum"] | None = None
|
||||
appearance_palette_locked: bool = False
|
||||
system_appearance_palette: Literal["default", "civic_blue", "forest", "plum"] = "default"
|
||||
system_appearance_palette_locked: bool = False
|
||||
effective_appearance_palette: Literal["default", "civic_blue", "forest", "plum"] = "default"
|
||||
effective_appearance_source: Literal["tenant", "system", "tenant_lock", "system_lock"] = "system"
|
||||
settings: dict[str, Any] = Field(default_factory=dict)
|
||||
|
||||
|
||||
@@ -157,3 +163,5 @@ class TenantSettingsUpdateRequest(BaseModel):
|
||||
default_locale: str = Field(min_length=1, max_length=20)
|
||||
enabled_language_codes: list[str] | None = None
|
||||
navigation: NavigationPreferencesPayload | None = None
|
||||
appearance_palette: Literal["default", "civic_blue", "forest", "plum"] | None = None
|
||||
appearance_palette_locked: bool | None = None
|
||||
|
||||
@@ -60,7 +60,7 @@ manifest = ModuleManifest(
|
||||
id="tenancy.lifecycle-and-settings",
|
||||
title="Administer tenant lifecycle and settings",
|
||||
summary="Tenancy adds explicit tenant creation, activation, context resolution, and tenant-owned settings over Core's shared scope storage.",
|
||||
body="A tenant is a concrete administrative and data boundary. Tenant lifecycle changes must preserve ownership and recovery guarantees for module-owned records. New tenants default to the German reference language unless the administrator selects another enabled system language; existing tenant and user preferences remain unchanged. Tenant administrators can inherit or override the system side-rail order and visibility and can lock entries visible for users; system locks remain effective. Personal navigation preferences still take precedence except that they cannot hide locked entries. Navigation changes never grant module entitlement, View visibility, or permissions. Tenancy contributes system tenant management and tenant settings to the shared administration workspace; without this module, the Core and Access baseline can operate in single-scope compatibility mode. Core-reserved module entitlement settings are managed only through the Admin module's tenant-module policy endpoints and are preserved when generic tenant settings are replaced.",
|
||||
body="A tenant is a concrete administrative and data boundary. Tenant lifecycle changes must preserve ownership and recovery guarantees for module-owned records. New tenants default to the German reference language unless the administrator selects another enabled system language; existing tenant and user preferences remain unchanged. Tenant administrators can inherit or override the system side-rail order and visibility and can lock entries visible for users; system locks remain effective. Personal navigation preferences still take precedence except that they cannot hide locked entries. Tenant appearance likewise inherits the system palette until explicitly selected; an unlocked tenant default permits a personal palette, while a policy-authorized tenant lock suppresses it and a system lock always wins. Resetting the tenant palette restores inheritance rather than copying the current system value. Navigation and appearance changes never grant module entitlement, View visibility, or permissions. Tenancy contributes system tenant management and tenant settings to the shared administration workspace; without this module, the Core and Access baseline can operate in single-scope compatibility mode. Core-reserved module entitlement settings are managed only through the Admin module's tenant-module policy endpoints and are preserved when generic tenant settings are replaced.",
|
||||
documentation_types=("admin",),
|
||||
audience=("system_admin", "tenant_admin", "operator"),
|
||||
related_modules=("access", "admin", "audit"),
|
||||
@@ -83,7 +83,7 @@ manifest = ModuleManifest(
|
||||
id="tenancy.reference.admin-fields",
|
||||
title="Tenant administration fields and consequences",
|
||||
summary="Tenant identity, ownership, locale, governance overrides, and lifecycle state have different mutation and recovery consequences.",
|
||||
body="A tenant slug is immutable after creation and identifies the administrative boundary. The initial owner receives the protected tenant-owner role. German is the reference and new-tenant default; locale and enabled languages are bounded by system language packages and may be changed explicitly. Tenant navigation inherits the system layer until explicitly saved; tenant locks keep entries visible for users but cannot relax a system lock. Governance overrides may narrow a system allowance but cannot loosen a system denial. Suspension keeps tenant-owned data and audit evidence while preventing normal use; an operator must switch away from the active tenant before suspending it.",
|
||||
body="A tenant slug is immutable after creation and identifies the administrative boundary. The initial owner receives the protected tenant-owner role. German is the reference and new-tenant default; locale and enabled languages are bounded by system language packages and may be changed explicitly. Tenant navigation and appearance inherit their system layers until explicitly saved. Palette choices use validated Core presets only. A tenant appearance lock requires policy-write authority, suppresses personal palette choices, and cannot relax a system lock. Governance overrides may narrow a system allowance but cannot loosen a system denial. Suspension keeps tenant-owned data and audit evidence while preventing normal use; an operator must switch away from the active tenant before suspending it.",
|
||||
documentation_types=("admin",),
|
||||
audience=("system_admin", "tenant_admin", "operator"),
|
||||
related_modules=("access", "admin", "audit"),
|
||||
|
||||
Reference in New Issue
Block a user