Skip to content
Docs
Changelog

Platform roles, organization roles and consent

The repository's architectural decision, rationale and consequences.

Markdown

Status: accepted · Date: 2026-11-27 · Builds on: ADR-0003, ADR-0009

Enforcement: packages/auth/src/access.test.ts (both permission matrices), packages/auth/src/access-policy.test.ts, packages/auth/src/organization-roles.test.ts, packages/api/src/features/access/access.procedures.test.ts, packages/api/src/features/consent/consent.procedures.test.ts, packages/api/src/platform/rpc/rls.isolation.test.ts (organization_role, data_consent), packages/api/src/platform/rpc/session-authority.test.ts.

Until now a User was user or admin, and a Member was owner, admin or member with Better Auth's default organization permissions; every Member could do everything in their Organization. The next admin screens (feedback inbox, analytics, errors, payments) need staff who are not full Admins, and the first client is a coaching business whose staff read students' measurements, health records and progress photos. Health data is sensitive personal data (LGPD art. 11, GDPR art. 9): it needs specific, per-purpose consent that can be withdrawn as easily as it was given.

Decision

  1. One permission vocabulary, in code. packages/db/src/schema/access.enums.ts (a leaf module) lists every resource and action (member: read/invite/remove, role: manage, feedback: read/triage/reply, analytics: read, errors: read, billing: read/refund, sensitiveData: read) and what each built-in role holds. Nothing outside the list can be granted. It lives in packages/db, not packages/api: packages/auth builds Better Auth's createAccessControl from it, and packages/auth cannot import packages/api, which depends on it (a package cycle Turborepo refuses). The db enum leaves are already the shared leaf every contract may import (scripts/leaf-modules.test.mjs).
  2. Two role layers.
    • Platform roles (user.role): user, admin, support, analyst, through the Better Auth admin plugin's ac and roles (packages/auth/src/access.ts). Only admin holds Better Auth's user/session actions, so only an Admin may call /admin/*. support holds feedback: *; analyst holds analytics: read and errors: read. No platform role holds sensitiveData: read.
    • Org roles (member.role): the built-in owner, admin and member, plus Custom roles each Organization defines through the organization plugin's dynamicAccessControl (better-auth 1.7.27, at most 20 per Organization), stored in organization_role under RLS (tenants read their Active Organization's rows; only Better Auth, as the connection role, writes them).
  3. Owner and admin never lose role: manage. Built-in roles are code. Better Auth refuses a stored role with a built-in name, refuses editing or deleting one, and a stored row could only add to a role anyway.
  4. Custom role writes go through Better Auth's routes (/organization/create-role, update-role, delete-role), so its checks apply whatever door the write came through: the writer needs ac actions and can only grant what they hold, a role still assigned to Members cannot be deleted. A before hook (packages/auth/src/access-policy.ts) adds three rules: a stored permission is exactly storedPermission(grants) — the app grants plus the Better Auth grants they imply (member: invite → invitation: create/cancel, member: remove → member: delete, role: manage → ac: * and member: update) — so Better Auth's own resources cannot be granted on their own; a name is 2–32 lowercase characters starting with a letter; a role is never renamed (Members reference it by name and Better Auth does not rewrite member.role). The after hook records org.role_created, org.role_updated and org.role_deleted.
  5. Procedures decide locally. requirePermission({ ... }) (platform/rpc/require-permission.ts) chains after orgProcedure, reads the Member's Custom roles from organization_role through context.db (RLS scopes the read to the Active Organization) and decides with the same ac the organization plugin uses. The Better Auth instance stays out of the oRPC context (its type makes the router type unserializable, TS7056). Platform screens use platformProcedure({ ... }) (platform/rpc/procedures.ts), which re-reads the database session like adminProcedure and refuses MCP access tokens. Unknown roles, malformed rows and empty requests fail closed with FORBIDDEN.
  6. Consent per Organization and category. data_consent holds one row per Member, Organization and category (measurements, health_records, progress_photos) with granted_at, revoked_at and the text_version of the consent text the Member saw. Only the Member writes their own rows (RLS); a revoke sets revoked_at and rows are never deleted by a tenant. A grant under an older CONSENT_TEXT_VERSION no longer counts until renewed. consent.granted and consent.revoked Audit Events carry the category and are the history.
  7. One door to someone else's sensitive data. A procedure that returns another Member's sensitive data chains sensitiveDataRead({ category, subject }) (platform/rpc/sensitive-data.ts). In one tenant transaction it checks sensitiveData: read, checks the subject's current Consent, runs the handler and enqueues a sensitive_data.viewed Audit Event; no event, no data. Reading your own data needs none of it. Services that read sensitive data inside a larger program call assertConsent from the consent feature.

Consequences

A Member holding only built-in roles costs no extra statement; a Member holding a Custom role costs one indexed read per guarded procedure. Nothing is cached across requests, so a role edit or a Consent revoke takes effect on the next request, and a revoke hides the data on the very next statement. The in-request membership memo (platform/rpc/membership.ts) still covers membership itself; permissions and Consent are read per call.

member.role may now name a Custom role, so clients that assumed owner | admin | member must accept any string. The admin screens can set support and analyst; until the platform screens for feedback, analytics and errors exist, those roles open nothing but are ready for platformProcedure.

Follow-ups: show sensitive_data.viewed events to the data subject in their own activity timeline (the audit_log tenant policy and personalActivityWhere need a change); move the remaining requireOrgRole use cases to permissions.

Alternatives

  • The vocabulary in packages/api, as first planned. packages/auth would have to import packages/api, a package cycle.
  • The Better Auth instance in the context, calling auth.api.hasPermission per procedure: TS7056 on the router type, and a network-shaped call for a decision that is a set lookup.
  • Roles cached in the session snapshot. Saves a statement but keeps a demoted Member's permissions for up to 60 seconds; the read is cheap and only paid by Custom role holders.
  • Our own role tables and write procedures instead of Better Auth's dynamic access control: Better Auth's member and invitation routes would not honour Custom roles, and a second write path would need its own escalation checks.
  • One blanket consent for all sensitive data: does not meet the specificity the LGPD asks for health data and cannot be withdrawn per purpose.
Edit on GitHubDocumentation is in English.
Cookie settings
We use essential cookies to make our app work. With your consent, we also use optional cookies to improve your experience.

Read our Cookie Policy and Privacy Policy.