feat(backend): add user and global preferences (IFC-2719, IFC-2722)

Merged
PA
4mo ago
+1913-69

Why

UI settings like date format and timezone live in component state or the browser locale today: an organisation cannot set defaults centrally and a user's choice doesn't survive a device switch. This PR adds the backend foundation — two schema-backed preference surfaces and one merged read query — so the frontend (follow-up PRs) can render dates per user preference.

Non-goals: frontend tabs/hooks (IFC-2720), DateDisplay integration (IFC-2721), filtering generic reads of CoreUserPreference (mutation-only enforcement in V1, deliberate).

Part of epic IFC-2718 · Spec: dev/specs/2026-04-user-preferences.md (branch ple-user-preferences) · Idea: INFP-512

What changed

  • Schema: CoreGlobalPreference (singleton, branch-agnostic) and CoreUserPreference (one per account, cascade-deleted, unique on account), both with optional date_format/timezone Text attributes.
  • Permissions: new manage_global_preferences global permission, wired into get_global_permission_for_kind() + a dedicated checker (mirrors RepositoryManagerPermissionChecker). Without it, the default General Access wildcard would let every user edit org defaults.
  • Effective query: InfrahubEffectivePreferences returns scalar date_format/timezone merged user > global > null for the calling account.
  • Owner scoping: CoreUserPreference writes are restricted to the owning account (admins bypass); id-less upserts are idempotent (relationship-based uniqueness derives no hfid — see dev/knowledge/backend/mutations.md); singleton creation is lock-guarded.
  • Seeding: first_time_initialization() + migration m074 for existing installs (permission node + empty singleton; role assignment beyond super-admins is a documented manual step).
  • Knowledge docs + towncrier fragments included.

Testing

93 targeted tests green (merge matrix, ownership attack paths, permission gating admin/wildcard/role-granted, migration idempotency, schema integrity) + ruff/mypy clean.

🤖 Generated with Claude Code


Summary by cubic

Adds backend support for per-user and org-wide UI preferences (date_format, timezone) via an owner-scoped Preference StandardNode, three typed GraphQL reads, and a single write mutation. Global edits require manage_global_preferences; identity-bound queries require auth; writes are serialized per owner for safe concurrency (IFC-2719, IFC-2722).

  • New Features

    • Model: Preference keyed by owner_id (account id or GLOBAL_OWNER_ID); reads never create; per-owner lock serializes writes; no seeding; account deletion can orphan rows (IFC-2867).
    • Repository: get_for_owner, get_for_owners, get_all, save with deterministic ordering and first-row wins if duplicates exist.
    • GraphQL reads: InfrahubEffectivePreferences returns date_format/timezone with source = USER | GLOBAL | DEFAULT; InfrahubUserPreferences and InfrahubGlobalPreferences return raw values. All require auth; GLOBAL read/write gated by manage_global_preferences.
    • GraphQL write: InfrahubSetPreferences(scope: USER|GLOBAL, date_format?, timezone?); omitted args are ignored; explicit null resets.
    • Date formats: DateFormat enum (ISO_8601, ISO_DATETIME, ISO_DATETIME_SECONDS, EU_DATETIME, US_12H) persisted and validated by the GraphQL enum.
  • Refactors

    • Persistence centralized in PreferenceRepository; read queries return only required fields; GLOBAL_OWNER_ID replaces Root-id coupling; lock namespace moved to constants.
    • Auth: anonymous sessions are denied for identity-bound operations via QUERIES_REQUIRING_AUTHENTICATION; InfrahubSetPreferences is exempt from the default-branch write gate; GLOBAL scope enforced via manage_global_preferences.
    • Query layer: copy shared return-labels to avoid cross-instance mutation; timezone remains free-form (no backend validation).