Roles
What this is for
A role is a named principal category assigned to people (people.role_id). Its slug is what permission evaluation keys on: each type's design_schema.record_permissions maps role slugs to the record actions that role may perform. The role's own permissions column feeds principal.permissions for coarse gates.
Why it exists
Multi-tenant SaaS needs per-account, per-app role definitions that RLS and the permission engine can enforce.
Why it exists (5-whys):
- Permission vocabularies are domain-shaped — a support app's roles aren't a CRM's — and hardcoding them freezes domain into core.
permissionsJSONB (a document per role) beats join tables here: role definitions are small, config-shaped, and read whole — the consumer needs "what can this role do?" in one row.UNIQUE(slug)is global whileapp_id/account_idcolumns scope ownership — slugs resolve globally (role_sluglookups ininvites.ts), but system roles are platform-wide while apps/accounts can define domain vocabularies.is_system+is_protectedkeep framework roles (system_admin,member) intact — installs and seeds can't rewrite the vocabulary the platform depends on.- Root: permissions need a name before they need enforcement —
rolesis the shared vocabulary thatrecord_permissions,required_roles,min_role, and RLS policies all dereference. (confirmed 2026-11-13)
Fields and settings
- Name — string, required. Display label (e.g. "Support Manager");
slugandnameare the API's required fields. - Slug — string, required, unique per app. Disabled after creation because
record_permissionskeys andpeople.role_idreferences depend on it. - Description — string, optional.
- System Role — checkbox, create-only. Reserved roles can't be deleted and are protected for core functions.
- Active — checkbox. Inactive roles can't be assigned to people.
- Permissions (JSON) — the form suggests an object mapping type slugs to action arrays (
{ "ticket": ["create","read"] }), but the permission engine does not evaluate this column for record access — see Downstream effects. A flat array form (["read","write","admin","system"]) is whatauth.tscopies intoprincipal.permissions.
Downstream effects
- Record CRUD is governed by each type's
design_schema.record_permissions, keyed by role slug — e.g."record_permissions": { "support-agent": ["read"] }. Assigning a person a role takes effect immediately (the engine resolvespeople.role_id → roles.slugper request, short cache). record_permissionskeys are matched verbatim againstroles.slug—system-adminis a dead key that silently denies the realsystem_adminrole. Theallkey is a wildcard granting every authenticated principal.auth.tscopiesrole.permissionsintoprincipal.permissionsonly when it is a flat array (system_adminis hardcoded to["read","write","admin","system"]).principal.permissionsbacks coarse checks like the invitessystemgate — not per-record access.- Malformed permissions don't lock anyone out of records (the column isn't read for that) — a wrong
record_permissionsslug on the type does. - Creating a role with slug
system_adminis restricted tosystem_adminprincipals.
Minimal configuration
{
"name": "Support Agent",
"slug": "support-agent",
"is_active": true,
"permissions": { "ticket": ["create", "read", "update"] }
}Full configuration
{
"name": "Support Manager",
"slug": "support-manager",
"is_active": true,
"is_system": false,
"permissions": { "ticket": ["create", "read", "update", "delete"] },
"metadata": { "department": "support" }
}Both examples use the object form the UI suggests — it is stored as documentation/delegated-policy data. Record access still comes from each type's record_permissions under the same slug.
Example
A support app ships three roles: support-agent, support-manager, and support-viewer. On the ticket type, record_permissions grants support-agent ["create","read","update"], support-manager all four actions, and support-viewer ["read"]; assignment happens on people records via role_id.
Core files
.framework/functions/roles.ts— API (list/get/create/update/delete;system_adminslug creation gated).framework/functions/_shared/permissions.ts—record_permissions/fields.*.permissionsevaluation (role-slug keyed;allwildcard).framework/functions/auth.ts/.framework/functions/_shared/principal.ts—role.permissions→principal.permissions(flat array only).framework/src/pages/admin/RolesPage.tsx/RoleDetailPage.tsx— admin UI- Table:
public.roles; assignment onpublic.people.role_id
Agent notes
- The Permissions textarea's own placeholder shows the object form — know that it is not what gates records. The gate is
record_permissions[role_slug]on the type. - Slug is immutable after create; rename = new role + reassignment. Never introduce a hyphenated
system-adminslug — seeded slugs use underscores. - The seeded
system_adminrole must stay assigned for admin functions;auth.tshardcodes its principal permissions, it is not data-driven.