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
- 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 inpackages/db, notpackages/api:packages/authbuilds Better Auth'screateAccessControlfrom it, andpackages/authcannot importpackages/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). - Two role layers.
- Platform roles (
user.role):user,admin,support,analyst, through the Better Authadminplugin'sacandroles(packages/auth/src/access.ts). Onlyadminholds Better Auth'suser/sessionactions, so only an Admin may call/admin/*.supportholdsfeedback: *;analystholdsanalytics: readanderrors: read. No platform role holdssensitiveData: read. - Org roles (
member.role): the built-inowner,adminandmember, plus Custom roles each Organization defines through the organization plugin'sdynamicAccessControl(better-auth 1.7.27, at most 20 per Organization), stored inorganization_roleunder RLS (tenants read their Active Organization's rows; only Better Auth, as the connection role, writes them).
- Platform roles (
- 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. - 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 needsacactions and can only grant what they hold, a role still assigned to Members cannot be deleted. Abeforehook (packages/auth/src/access-policy.ts) adds three rules: a stored permission is exactlystoredPermission(grants)— the app grants plus the Better Auth grants they imply (member: invite→invitation: create/cancel,member: remove→member: delete,role: manage→ac: *andmember: 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 rewritemember.role). Theafterhook recordsorg.role_created,org.role_updatedandorg.role_deleted. - Procedures decide locally.
requirePermission({ ... })(platform/rpc/require-permission.ts) chains afterorgProcedure, reads the Member's Custom roles fromorganization_rolethroughcontext.db(RLS scopes the read to the Active Organization) and decides with the sameacthe organization plugin uses. The Better Auth instance stays out of the oRPC context (its type makes the router type unserializable, TS7056). Platform screens useplatformProcedure({ ... })(platform/rpc/procedures.ts), which re-reads the database session likeadminProcedureand refuses MCP access tokens. Unknown roles, malformed rows and empty requests fail closed withFORBIDDEN. - Consent per Organization and category.
data_consentholds one row per Member, Organization and category (measurements,health_records,progress_photos) withgranted_at,revoked_atand thetext_versionof the consent text the Member saw. Only the Member writes their own rows (RLS); a revoke setsrevoked_atand rows are never deleted by a tenant. A grant under an olderCONSENT_TEXT_VERSIONno longer counts until renewed.consent.grantedandconsent.revokedAudit Events carry the category and are the history. - 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 checkssensitiveData: read, checks the subject's current Consent, runs the handler and enqueues asensitive_data.viewedAudit Event; no event, no data. Reading your own data needs none of it. Services that read sensitive data inside a larger program callassertConsentfrom 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/authwould have to importpackages/api, a package cycle. - The Better Auth instance in the context, calling
auth.api.hasPermissionper 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.