Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-023 — Canonical consumption of hierarchical settings

Date: 2026-10-21 Status: Accepted — superseded in part by ADR-024 (§3: Settings.User.* are no longer system permissions; preferences moved to the User level); superseded in part by ADR-025 (§5: the migration that drops TenantSettings applies late updates only) Supersedes in part: ADR-012 — the TenantSetting entity and the template's PerTenantSettingsStore are replaced; the per-tenant auth settings slices stay. Pairs with: DevPack ADR-016 (definitions in code, overrides per scope, secrets in the store, cache after commit) — also shipped in the package, at {NuGetPackageRoot}/itlab.template.devpack/{version}/devpack-docs/adrs/016-hierarchical-settings.md.

Context​

The template stored per-tenant settings as raw strings in tenancy.TenantSettings, seeded defaults for the default tenant, and resolved them through its own PerTenantSettingsStore. Callers parsed "true"/"false" and treated a missing row as "not configured". DevPack 10.23.0 ships a hierarchical mechanism (DevPack ADR-016): typed definitions with defaults in code, overrides per scope (Global, Tenant, User), authorization in the manager and the cache invalidated after the commit.

Decision​

  1. One table. settings.SettingValues (mapped by ApplyDevPackSettingsModel(Schemas.Settings)) replaces tenancy.TenantSettings. Defaults live in code; nothing is seeded.
  2. Definitions in Application. AuthSettingDefinitions declares AuthProvider.* (and DevPack declares EntraID.*). Every identity key is EditableBy = System, AllowedLevels = [Tenant] and SecurityBinding — a tenant admin cannot point the Entra directory elsewhere, and no global or configuration value can become every tenant's trusted directory.
  3. Authorizers per level — the DevPack ones (AddPermissionScopeAuthorizers, DevPack 10.24.0), configured with the template's permission names; the template only answers whether a tenant exists (SettingTenantDirectory). Global needs Settings.Global.* ([SystemPermission]); Tenant needs Settings.Tenant.* on the caller's own tenant; Settings.Tenant.CrossTenant ([SystemPermission]) is the system reach — another tenant's settings, and the settings only the system administrator edits (EditableBy = System) at any tenant; User needs the caller's tenant and a member user, with Settings.User.*, or the user editing their own EditableBy = User keys. Reads outside the reach, and a tenant that does not exist, answer 404; forbidden writes answer 403. The admin read of the auth settings goes through ISettingManager.DescribeAsync, so it is authorized like the update. The auth settings endpoints keep their own permission (Tenants.AuthSettings.Read/Update) on top: a caller needs both that and the system reach. Settings.User.* are [SystemPermission] while no definition accepts the user level, so a tenant administrator's group shows no toggle that changes nothing.
  4. Anonymous flows pass the scope. /auth/config resolves the tenant from the host and reads with SettingContext.For(Tenant(id)); the Entra reverse lookup finds the tenant through the indexed lookup value (EntraID.TenantId is a lookup key) and refuses a directory trusted by more than one tenant (it used to take the first match).
  5. Data migration without a transaction. The production deploy applies the per-migration scripts (runbook 03, ADR-022) and the copy re-runs after the rollout, so the copy from TenantSettings is an idempotent INSERT … WHERE NOT EXISTS (TenantSettingsCopy). The value kind is driven by the key (booleans were stored with Type = String), soft-deleted rows are skipped and every active row is copied, including those equal to the default; a key without a definition is copied as a string and reported by the integrity check, so nothing is dropped silently. The ciphertext of secrets is copied as is. Host rows (TenantId 0 or NULL) are skipped: the old store never wrote one. TenantSettings is frozen for one release and dropped in the next; a reconciliation step after draining the old pods copies rows they wrote late (shipped as TenantSettingsReconcile.sql, runbook 03), and the migration that drops the table runs it first, so a forgotten reconciliation cannot lose those rows.
  6. Automation sets global values through configuration (Settings:{key}); a client certificate (ADR-017) writes only Tenant scope with the permission.

Consequences​

  • A new setting is one definition plus registration — no migration.
  • /auth/config and the Entra validators read resolved values: a tenant with no row sees the default, which the seed used to guarantee.
  • Writes are staged and committed by the caller's unit of work; the cache is invalidated only after the real commit.
  • Explicit choices equal to the default become rows (no behaviour change now); a future global override does not affect them.
  • Preferences (UserPreference) move to the User scope in a later delivery; the preferences HTTP contract stays.

Alternatives considered​

  • Keep TenantSetting behind the adapter: two stores for the same concept; no defaults in code.
  • Migrate in C# at startup: leaves the migration outside the reviewable script the deploy applies.
  • Testcontainers for SQL tests: the CI's migration-scripts stage already starts a SQL Server container and points SETTINGS_SQLSERVER_TESTS at it; locally any server with a throwaway database per run does, and the tests are skipped without the variable.