Policy templates
See exactly which policies the template catalog and the Studio offer, apply one through the policies API, and know which templates need a role or a different expression to do what their name says.
A policy template is a prepared set of fields for the policies API: a command, the target roles and the using and with check expressions. Aurabase has two sets: the catalog served by GET /v1/db/policy-templates, and the list built into the RLS policies tab of the Studio. Neither creates anything by itself; applying one creates an ordinary policy, which you can change or drop like any other (Policies).
Prerequisites#
-
A linked project:
aura link <project-id>. -
The example schema, installed with the service role key of the project:
export AURABASE_SERVICE_ROLE_KEY=aura_sk_… aura db seed --example docsIt creates
profiles,orgs,org_members,posts,todos, the viewopen_todosand the functiontodo_count(p_user uuid). See Example schema.
The examples create the table sec_tpl_notes, then remove it:
aura db raw "create table sec_tpl_notes (
id bigint generated always as identity primary key,
user_id uuid not null default auth.uid(),
body text not null)"
aura db raw "insert into sec_tpl_notes (user_id, body) values
('00000000-0000-4000-8000-0000000000a1', 'Alice note'),
('00000000-0000-4000-8000-0000000000b2', 'Bob note')" ℹ No results (0 rows).
ℹ No results (0 rows).The template catalog#
The catalog is static: it does not depend on a project, so its path has no project id, and it is served on the management API only, with the session token of a console account (Authorization: Bearer …), not with a project key. Request (excerpt, not replayed by the documentation checks: it needs a console session):
curl -s "$AURA_GATEWAY_URL/v1/db/policy-templates" -H "Authorization: Bearer $AURA_CONSOLE_TOKEN"Response captured on a local stack (the name and description fields are in French in this version):
{"data":[
{"id":"public_read","name":"Lecture publique","description":"Tous les utilisateurs (anon + authenticated) peuvent lire","command":"SELECT","using_expr":"true","check_expr":null,"target_roles":[]},
{"id":"auth_read","name":"Lecture authentifiee","description":"Seuls les utilisateurs connectes peuvent lire","command":"SELECT","using_expr":"true","check_expr":null,"target_roles":["authenticated"]},
{"id":"owner_all","name":"Acces proprietaire (CRUD complet)","description":"Chaque utilisateur ne voit et modifie que ses propres lignes","command":"ALL","using_expr":"auth.uid() = user_id","check_expr":"auth.uid() = user_id","target_roles":[]},
{"id":"owner_read","name":"Lecture proprietaire","description":"Chaque utilisateur ne voit que ses propres lignes","command":"SELECT","using_expr":"auth.uid() = user_id","check_expr":null,"target_roles":[]},
{"id":"owner_insert","name":"Insertion authentifiee","description":"Les utilisateurs connectes peuvent inserer leurs propres lignes","command":"INSERT","using_expr":null,"check_expr":"auth.uid() = user_id","target_roles":[]},
{"id":"admin_all","name":"Acces admin complet","description":"Seuls les admins ont acces total","command":"ALL","using_expr":"auth.role() = 'admin'","check_expr":"auth.role() = 'admin'","target_roles":[]}
],"meta":null}Sent to a project URL with a project key, the same path is read as a table named policy-templates and answers 404 TABLE_NOT_FOUND.
What each template creates#
An empty target_roles creates the policy for every role (public), anon included.
id | SQL created on <table> | What it really does |
|---|---|---|
public_read | for select using (true) | Every role reads every row, the anon key included. See Public read access. |
auth_read | for select to aura_authenticated using (true) | Every signed-in user reads every row; visitors read none. |
owner_all | for all using (auth.uid() = user_id) with check (auth.uid() = user_id) | Each signed-in user reads and writes its own rows. Visitors match nothing, since auth.uid() is NULL for them. |
owner_read | for select using (auth.uid() = user_id) | Each signed-in user reads its own rows. |
owner_insert | for insert with check (auth.uid() = user_id) | Each signed-in user inserts rows in its own name. |
admin_all | for all using (auth.role() = 'admin') with check (auth.role() = 'admin') | Nobody: auth.role() returns aura_authenticated or aura_anon through the SDK and authenticated or anon through SQL, never admin. Only the service_role key, which bypasses policies, reaches the rows. |
The owner_* templates need a uuid column named user_id; on another column name, edit the expressions before applying.
Apply a template#
Copy the fields of the template into POST /v1/db/{project_id}/policies, with a name of your choice. Adding target_roles: ["authenticated"] to owner_all keeps the anon key out of the policy explicitly:
curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/policies" \
-H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' \
-d '{"table": "sec_tpl_notes", "name": "sec_tpl_notes_owner_all", "command": "ALL",
"using_expr": "auth.uid() = user_id", "check_expr": "auth.uid() = user_id",
"target_roles": ["authenticated"]}'
echo
aura db raw --as authenticated --user 00000000-0000-4000-8000-0000000000a1 "select body from sec_tpl_notes"{"data":null,"meta":null}
ℹ Executed as authenticated (00000000-0000-4000-8000-0000000000a1).
body
──────────
Alice note
ℹ 1 row(s).The same fields work with aura db policy add <table> <name> <command> --using-expr … --check-expr …, which has no option for roles (the policy then applies to every role).
Replace admin_all#
Applied as is, admin_all lets nobody in, even a user whose token says it is an administrator. Give administrators a role in app_metadata.roles from a server, and test it with auth.has_role(). The simulator compares both, for a user that carries the role:
curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/policies" \
-H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' \
-d '{"table": "sec_tpl_notes", "name": "sec_tpl_notes_admin_all", "command": "ALL",
"using_expr": "auth.role() = '"'admin'"'", "check_expr": "auth.role() = '"'admin'"'"}'
echo
simulate() {
curl -s -X POST "$AURABASE_URL/v1/db/$PROJECT_ID/rls/simulate" \
-H "apikey: $AURABASE_SERVICE_ROLE_KEY" -H 'content-type: application/json' \
-d '{"table": "sec_tpl_notes", "operation": "SELECT", "role": "authenticated",
"user_id": "00000000-0000-4000-8000-0000000000c3", "claims": {"app_metadata": {"roles": ["admin"]}}}' \
| node -e 'console.log("admin user reads", JSON.parse(require("fs").readFileSync(0, "utf8")).data.row_count, "notes")'
}
simulate
aura db policy update sec_tpl_notes sec_tpl_notes_admin_all --using-expr "auth.has_role('admin')" --check-expr "auth.has_role('admin')"
simulate{"data":null,"meta":null}
admin user reads 0 notes
✔ Policy 'sec_tpl_notes_admin_all' updated on 'sec_tpl_notes'.
• USING: auth.has_role('admin')
• WITH CHECK: auth.has_role('admin')
admin user reads 2 notesapp_metadata is written by your server, never by the user, so a user cannot give itself the role. user_metadata, which the user can change, must never grant access.
Templates of the Studio#
The RLS policies tab of a table (Database › a table) has its own list, built into the page; it does not read the catalog. Choosing one fills the form, which then calls the same policies API:
| Studio template | Fields filled | Result |
|---|---|---|
| Strict owner | ALL, auth.uid() = user_id for both expressions | Same as owner_all. Check authenticated in Target roles to keep visitors out explicitly. |
| Public read for all | SELECT, true | Same as public_read. |
| Team members | SELECT, team_id IN (SELECT get_user_team_ids(auth.uid())) | Refused: the policies API does not accept subqueries. Write it in SQL, with a function you created. |
| Team administrators only | ALL, is_team_admin(team_id, auth.uid()) | Refused unless your schema has an is_team_admin(…) function: neither function is provided. |
The two team templates show a pattern, not a ready policy: create the security definer function first (Multi-tenant schema design), then a policy that calls it without a subquery (using (is_member(org_id))).
Result#
aura db policy list --table sec_tpl_notes --json | node -e 'for (const p of JSON.parse(require("fs").readFileSync(0, "utf8"))) console.log(p.policy_name, p.roles.join(","), p.using_expr)'sec_tpl_notes_admin_all public auth.has_role('admin'::text)
sec_tpl_notes_owner_all aura_authenticated (auth.uid() = user_id)Common errors#
| Message | Cause | Fix |
|---|---|---|
404 TABLE_NOT_FOUND, Could not find the table 'policy-templates' … | The catalog was requested on a project URL with a project key. | Request it on the management API with a console session, or use the table on this page. |
401 UNAUTHORIZED, A console token is required (Authorization header or token query parameter) | No console session on the management API. | Sign in with aura login or the Studio. |
the SQL keyword 'SELECT' is not allowed in policy expressions — subqueries are not permitted | The Team members template of the Studio. | Create the policy in SQL, or call a function without select. |
function is_team_admin(integer, uuid) does not exist | The Team administrators template, without the function. | Create the function first. |
Every user sees nothing after admin_all | auth.role() never equals admin. | Use auth.has_role('admin') (Replace admin_all). |
policy "…" for table "…" already exists | A template applied twice with the same name. | Choose another name, or update the policy. |
Clean up#
drop table sec_tpl_notes; ℹ No results (0 rows).