Skip to content

ADR 0016: Organization membership and session-selected tenancy

Accepted. 2026-11-26. Implements the identity decision in ADR 0012.

ADR 0012 identified the per-tenant users table and the unscoped findByEmail lookup as blockers for hosted onboarding. A person needs one identity across organizations, a role and status per organization, and a safe way to choose the active organization after login. Self-host deployments also need a first-administrator path, and the synthetic demo needs the same isolation guarantees as any other tenant.

One global user identity is keyed by normalized email. Each organization membership carries the user’s role and status for that organization. Magic links resolve the global identity. A session selects the organization automatically for one active membership, waits for a choice for multiple memberships, and provides an access-ended state when no active membership remains.

Requests take the tenant from the session and check the active membership before using a tenant-scoped repository. Switching replaces the session’s selected tenant. Deactivation clears sessions scoped to that membership and leaves users able to choose another organization they belong to. The application uses a host-only cookie at the configured application origin.

Membership changes require an active administrator in the selected organization. The database serializes writes per organization, checks the last-active-administrator rule, and writes the audit event in the same atomic operation. The D1 adapter uses a transactional batch. The Node adapter uses an immediate SQLite transaction or a Postgres transaction that locks the organization row.

The demo organization uses ordinary tenant boundaries and explicit synthetic memberships. Initial self-host setup creates the first administrator through the bootstrap command. Administrators can add existing active users in the product. Hosted invitations and provisioning remain separate capabilities.

  • A user can belong to several organizations with a different role in each.
  • Tenant-scoped queries never take the tenant from the hostname or a global user role.
  • Deactivation removes the active tenant from affected sessions without signing the user out of other organizations.
  • Membership changes are auditable and cannot remove the last active administrator.
  • The submitter role is read-only in the web app: it can view the inbox but cannot review, resolve parcels, export, or administer the organization.
  • The demo organization is a normal tenant for authorization and isolation.
  • Self-host operators create the first administrator with a transactional bootstrap command.

Migration 0003 merges trimmed, lowercased email addresses. It prefers an active membership over an inactive one, then the highest role (admin, reviewer, submitter). Unknown legacy roles map to submitter; any non-active status maps to disabled for the global user and inactive for the membership. The preflight reports duplicate, invalid-role, and invalid-status counts, and the detailed collision query lists the affected rows. Review both before applying the migration. A tenant-scoped alias preserves attribution for legacy capture audit events whose original user ID was merged. Existing sessions and unconsumed magic links are invalidated. Users sign in again under the new membership model.