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

ADR-024 — User preferences as settings of the User level

Date: 2026-10-23 Status: Accepted Pairs with: ADR-023 (canonical consumption of hierarchical settings) and DevPack ADR-016 — also shipped in the package, at {NuGetPackageRoot}/itlab.template.devpack/{version}/devpack-docs/adrs/016-hierarchical-settings.md. Supersedes in part: the UserPreference table behind GET/PATCH {Bff}/preferences; the HTTP contract stays.

Context​

ADR-023 moved the per-tenant identity settings to the hierarchical settings of DevPack 10.23.0 and left the user's display preferences (settings.UserPreferences, one row per user and tenant) as a second store for the same concept. That store has no defaults outside the code, so a tenant cannot set the look its users start from, and every field is copied into every row. The settings already support a User level (SettingScope.User(tenantId, userId)) and DevPack 10.24.0 authorizes it (PermissionUserSettingsAuthorizer).

Decision​

  1. One definition per preference. PreferenceDefinitions (Application, next to the enums) declares Preferences.Language, .FontSize, .Locale, .RowDensity, .Experience, .ThemeMode, .V2Palette, .FavoriteDestinations, .SidebarStyle, .FullWidth and .LayoutStyle, group preferences, defaults equal to the previous ones. The enums are stored by their wire names ("br", "extra-large"); FavoriteDestinations is a list a write replaces, normalized with Distinct.
  2. Levels Global, Tenant, User; edited by the user. EditableBy = User: the user changes their own value without any permission; a tenant (or the platform, at Global) may set the default its users inherit. The user scope is {tenantId}:{userId} — preferences belong to a membership, as the old unique index (TenantId, UserId) did.
  3. Every definition falls back. InvalidStoredValue = FallBackToNextLayer on all of them, and the enum definitions refuse an undefined member (Enum.IsDefined): an integer, a comma-composed name or a value retired from an enum reads as the inherited value instead of failing the page.
  4. The HTTP contract is a facade. GET {Bff}/preferences reads every definition with ISettingProvider at the user scope; PATCH writes the fields sent with ISettingManager.SetManyAndCommitAsync (absent or null = kept). PreferencesResponse, UpdatePreferencesCommand and their schemas are unchanged, pinned by PreferencesContractTests against a snapshot taken before the change.
  5. The palette is checked only when it changes. A v2Palette different from the value in effect (own or inherited) must be a theme available in the tenant (Preferences.V2PaletteUnknown, 400); one whose theme was retired is kept when re-sent with other fields, as before. The check stays in the handler — it reads ThemeAvailability, and a group validator would run on every write of the group.
  6. Data copy, then freeze. The CopyUserPreferencesToSettings migration copies settings.UserPreferences into settings.SettingValues with the rules of ADR-023 §5 (idempotent SQL, no own transaction, soft-deleted and host rows skipped), plus: a per-column allowlist of enum members by wire name and case (BIN2), frozen in the migration; FavoriteDestinations only when it is a non-empty JSON array (ISJSON); and only fields different from the default. UserPreferences is frozen for one release (no writes, entity only mapped) and dropped in the next, whose migration runs the reconciliation (UserPreferencesReconcile.sql, runbook 03) first.
  7. The scope follows the membership. Removing a user's membership (UserManagement) also removes their overrides in that tenant (ISettingManager.DeleteScopeAsync), in the same commit; a later membership starts from the tenant's defaults. Users are deactivated, never deleted, so there is no user-level cleanup.
  8. Settings.User.Read/Update become console permissions. ADR-023 kept them [SystemPermission] while no definition accepted the user level. Now one does, and the reach the authorizer gives them is the tenant's own: a tenant's Admin manages the preferences of its members, while another tenant's users and the EditableBy = System definitions still need Settings.Tenant.CrossTenant. They reach every tenant's Admin through AllExceptSystem() (the system group of each tenant is reconciled at startup).

Consequences​

  • A user's explicit choice of a value equal to the default becomes inheritance after the copy: if the tenant later sets another default, that user follows it. Accepted — the old rows stored every field, so an explicit choice of the default cannot be told apart from never having chosen.
  • The reconciliation works per legacy row (the old table keeps one UpdatedAt per user): if an old replica saves any preference of a user after a new one saved another, the old row wins as a whole and the newer change is lost. Accepted — it is limited to the rollout window, and the runbook keeps that window short; per-field timestamps do not exist in the source.
  • A tenant can set the default look of its users without a migration; the management API and screens for that arrive with the generic settings API (plan Fase 5/6).
  • Preferences are cached like any setting (in process for 30 s, plus the shared cache): a write is visible to the writer at once and to other replicas within that window.
  • PATCH now answers 404 for a scope the authorizer does not reach — a session whose membership was removed — where it used to accept the write. GET still answers: it is the user's own runtime read, not authorized.
  • UserPreference survives one release as a read-only mapped entity; IAppDbContext.UserPreferences is gone.

Alternatives considered​

  • Keep UserPreferences and add a tenant-default table: a third store for the same concept; no shared cache or authorization.
  • Copy every field, defaults included (as ADR-023 did for identity settings): keeps explicit choices, but freezes every existing user against tenant defaults forever — the feature this change exists for would only reach new users.
  • Validate the palette with an ISettingGroupValidator: sees the changed keys, but runs on every write of the group, including tenant defaults written by an administrator who may legitimately pick a theme before making it available.