Permissions & Multi-Tenancy
Role and policy patterns for real applications: scoped data access, tenant isolation, and record immutability.
Roles and Policies
Each application role pairs with an identically-named access policy — for example app_staff and app_staff_policy. Create the role, link the policy through access entries, then add permission rows for each action (create, read, update, delete) on each collection the role may touch.
$CURRENT_USER Belongs in Presets, Not Defaults
$CURRENT_USER is not a valid schema.default_value — it fails with 500 invalid uuid. To default a field to the logged-in user, use a permission preset on the role's create permission:
{
"collection": "app_tasks",
"action": "create",
"presets": { "created_by": "$CURRENT_USER" }
}Deep paths work in both presets and filters: $CURRENT_USER.app_store resolves to the related item, not just the raw key.
Multi-Tenancy Pattern
The standard recipe for tenant-scoped apps (branches, sites, organizations):
- Tenant root collection —
app_tenants, one row per tenant. tenantM2O on every tenant-owned collection — plus on thesystem_userscollection, so each staff user is assigned to a tenant.- Role permission filters — staff roles read and write through:
{ "tenant": { "_eq": "$CURRENT_USER.tenant" } }For child collections, filter through the parent ({ "work_order": { "tenant": { "_eq": "$CURRENT_USER.tenant" } } }); for junctions, filter through the junction's parent key.
- Create presets — staff creates get
{ "tenant": "$CURRENT_USER.tenant" }so rows are stamped with the creator's tenant automatically. - An unfiltered admin role — a manager/admin tier sees all tenants by omitting the filter entirely.
Serial numbers can embed a tenant code for per-tenant, per-year counters — for example DOC-SITE1-2026-0001. See Automation Recipes for the stamping flow.
Append-Only Collections
For audit trails and activity feeds — anything staff may add to but never rewrite:
- Remove
updateanddeletepermissions for staff roles; keep all actions for managers. - Make
created_byreadonlyon the field and preset it to$CURRENT_USER. - Make system-stamped fields (like
notification_sent_at)readonly; a blank value conveniently means "pending".