roles
What it does
CRUD over public.roles — role definitions (slug, permissions JSON, is_system, is_protected) that principals carry and the permission engine matches against design_schema.record_permissions.
Route and methods
/.netlify/functions/roles, reachable as /api/roles (/api/* → /.netlify/functions/:splat, .framework/public/_redirects). Dispatch is by HTTP method only (?action= is ignored):
| Call | Handler |
|---|---|
GET /api/roles | list |
GET /api/roles?id=<uuid> | get |
POST /api/roles | create |
PATCH /api/roles (id in body) | update |
DELETE /api/roles (id in body) | remove |
Authentication
Standard createHandler auth: x-api-key → machine principal, x-internal-secret → internal principal, Bearer <jwt> → human; anonymous → 401.
createrequiressystem_admin, or acanCreategrant fromresolveFirstSurfacePermissions(..., 'role', 'create'). Creating thesystem_adminslug itself always requiressystem_admin.update/removehave no explicit role gate — writes go throughadminDbfor system admins andctx.db(JWT-scoped, RLS) for other humans, so a non-admin's update fails at the database asRole not found or access denied.list/getare readable by any authenticated principal.
Request contract
list (query): app_id ('null' filters to global roles), is_system ('true'/'false'), limit (default 50), offset (default 0). Active rows only, ordered is_system desc then name, joining app:apps(id, slug, name).
get (query): id — active rows only.
create (body): slug, name required. Optional: app_id, description, permissions (default {}), is_system (default false). is_active: true is forced.
update (body): id plus any subset of role columns — no field allowlist; every key except id is written plus updated_at, after validateUpdatePermissions(ctx, updates, current, 'role') passes.
remove (body): id — soft delete (is_active = false).
Response contract
Standard { data, error, meta } envelope.
list→ array of role rows;get/create/update/remove→ single row.- System admins get raw rows. Other principals get
sanitizeRecordDataoutput — role records carry nodesign_schema, so non-admin reads collapse to{ id }(andlistreturns an array of{ id }objects). Row-level access for humans is additionally bounded by RLS onctx.db.
Side effects
createinserts intopublic.rolesafter a duplicate-slug pre-check scoped toslug+app_id(orapp_id IS NULLfor global roles).removeis a soft delete;update/removestampupdated_at.emitLogwritesrole.created/role.updated/role.deletedaudit rows topublic.logswith before/after payloads.
Errors
| Condition | HTTP | Message |
|---|---|---|
| Missing create fields | 500 | slug and name are required |
Non-admin creating system_admin | 500 | system_admin role can only be created by system administrators |
| Non-admin without create grant | 500 | Insufficient permissions to create roles |
| Duplicate slug for app scope | 500 | Role slug already exists for this app |
| Missing/inaccessible row | 500 | Role not found or access denied |
Missing id | 500 | Role ID is required |
| Unknown method | 500 | Unsupported method: <METHOD> |
Validation failures surface as HTTP 500 — thrown Errors without a statusCode map to 500 in the shared error path.
Agent notes
updateandremovereadidfrom the body only — a?id=query param is not consulted (unlike most sibling functions).- The pre-check scopes slug uniqueness to
app_id, but the database enforces a global unique index onroles(slug)(roles_slug_key,.framework/migrations/009_roles_slug_unique.sql) — a slug already used by another app passes the pre-check then fails the insert. The pre-check also ignoresis_active, so soft-deleted slugs still conflict. updateaccepts arbitrary columns (no allowlist):slug,is_system,is_protected,permissionscan all be rewritten by a caller that passesvalidateUpdatePermissions.- Role
permissionsis a free-form JSON column — the grant semantics live indesign_schema.record_permissionsmatching on role slug, not in this table.