Tutorial 4: Role-Based Access Control
Right now, only admin users have access to tasks — no permissions are configured for regular users yet. In a real team app you need fine-grained control: Managers should be able to do everything, while Members can only manage their own tasks.
Buildpad’s RBAC system maps roles to permission policies on each DaaS collection. The UI component CollectionForm automatically enforces these permissions — no manual field-hiding logic required.
By the end you’ll have:
- Two roles: Manager and Member
- Policies that let Members read all tasks but only create/edit their own
- A permission-aware form that hides forbidden fields automatically
- An
usePermissionshook powering conditional action buttons
Prerequisites
- Completed Tutorial 3: Building UI.
How RBAC works in Buildpad — Roles are defined in DaaS and assigned to users. Each role carries one or more policies; a policy bundles permission rules for specific collections and actions (read, create, update, delete), optionally filtered by a rule (e.g., “own items only”). A user’s effective access is the additive union of all policies across all their roles — policies can only expand access, never revoke it. Users with admin_access = true bypass all permission checks entirely. CollectionForm reads these policies at runtime and hides or disables fields the current user cannot edit.
Scaffold the Roles
Open Copilot Chat and type:
/create-rbacWhen prompted, define two roles:
Manager
tasks: read (all), create (all), update (all), delete (all)projects: read (all), create (all), update (all), delete (all)
Member
tasks: read (all), create (all), update (own), delete: noneprojects: read (all), create: none, update: none, delete: none
Why “create (all)” not “create (own)”? — A permission filter can’t reference user_created at creation time because the item doesn’t exist yet. Instead, /create-rbac adds a preset that automatically sets user_created = $CURRENT_USER when a Member creates a task, recording ownership at save time. Only update and delete use the own item filter.
On a Buildpad Engine project, /create-rbac adds no preset, because the Engine stores presets but does not apply them. The Engine fills user_created itself through the field’s user-created special.
The skill uses MCP tools to configure these roles, policies, and permissions directly in your DaaS backend — no migration file is generated. On a Buildpad Engine project, the roles and permissions live in the Engine, not in Postgres row-level security. You can also set them in the Studio.
Verify the Configuration
/create-rbac configures roles, policies, and permissions directly in DaaS via MCP tools — no migration is needed.
Check the roles in the Studio (Open Studio on the project’s Details page). To use the API, send the request to the Engine URL with an access token from a password sign-in, not to the <project-id>.daas.<domain> host (that host is the Studio). See Calling the Engine API From a Terminal.
Verify the roles were created in DaaS Studio under Roles and Policies, or via the API:
curl -H "Authorization: Bearer <your-token>" \
https://<your-project>.daas.buildpad.ai/api/rolesAdd the Permissions Proxy Route
CollectionList and CollectionForm resolve permissions by calling GET /api/permissions/me on your Next.js app, which proxies to the data service. The CLI’s bootstrap installs this route. If it is missing, or the call fails, the components grant nothing: every user sees Create, Edit and Delete disabled, so RBAC looks over-restrictive rather than broken.
Check whether the route exists:
ls app/api/permissions/me/route.tsIf it’s missing, create it:
On a Buildpad Engine project, the route forwards the signed-in user’s access token to the Engine’s /api/permissions/me. The DaaS URL is the Engine URL without /api:
import { type NextRequest, NextResponse } from "next/server";
import { getAuthHeaders, getDaasUrl } from "@/lib/api/auth-headers";
export async function GET(request: NextRequest) {
try {
const daasUrl = getDaasUrl();
const headers = await getAuthHeaders();
const searchParams = request.nextUrl.searchParams.toString();
const url = `${daasUrl}/api/permissions/me${searchParams ? `?${searchParams}` : ""}`;
const response = await fetch(url, { headers, cache: "no-store" });
const data = await response.json();
return NextResponse.json(data, { status: response.status });
} catch (error) {
const message = error instanceof Error ? error.message : "Proxy error";
return NextResponse.json({ errors: [{ message }] }, { status: 500 });
}
}import { type NextRequest, NextResponse } from "next/server";
import { getAuthHeaders, getDaasUrl } from "@/lib/api/auth-headers";
export async function GET(request: NextRequest) {
try {
const daasUrl = getDaasUrl();
const headers = await getAuthHeaders();
const searchParams = request.nextUrl.searchParams.toString();
const url = `${daasUrl}/api/permissions/me${searchParams ? `?${searchParams}` : ""}`;
const response = await fetch(url, { headers, cache: "no-store" });
const data = await response.json();
return NextResponse.json(data, { status: response.status });
} catch (error) {
const message = error instanceof Error ? error.message : "Proxy error";
return NextResponse.json({ errors: [{ message }] }, { status: 500 });
}
}Without this route, CollectionList and CollectionForm disable Create, Edit and Delete for every user, admins included. The data service still enforces permissions server-side either way.
Assign Roles to Users
On a Buildpad Engine project, users cannot sign up, so you create each test account yourself. Do not test with the Studio administrator (admin@example.com): that account has admin access and bypasses every permission rule. Make two test accounts:
- Click Open Studio on the project’s Details page and sign in as the Studio administrator.
- Go to Users and click Add User.
- Enter the email
manager@example.comand a password. Under Roles, click Add Existing, select Manager and click Add Selected. Make sure the status isactive, then click Create. - Do steps 2 and 3 again for
member@example.comwith the role Member.
If you forget a test password, reset it in the Studio’s Users module. See Users .
Your AI assistant cannot create these accounts: the Engine’s users MCP tool only reads and updates users. You can also create them with POST /api/users and give a role with POST /api/users/:id/roles, with an administrator access token. A user can hold more than one role.
In DaaS Studio, go to Users, select a user, and assign them a role. For testing:
- Assign yourself the Manager role.
- Create a second test account and assign it the Member role.
New accounts start with the built-in User role, which has admin_access = false and no collection permissions by default. Until you assign a Manager or Member role, the test account will see empty lists and no create/delete buttons — this is expected. Assign the role first, then test.
You can also ask /create-rbac to seed test users with roles — it will assign roles programmatically via the DaaS access API. Users can hold multiple roles simultaneously; role assignment is M2M, not a single field on the user.
Verify Permission Enforcement in the Form
Sign in as the Member test account and navigate to a task you did not create (/content/tasks/<id>).
CollectionForm will automatically:
- Show all fields as read-only (because the Member policy only allows
updateon own items) - Hide the Delete button entirely
Sign in as Manager and notice full edit access is restored. No code changes needed — the form reads permissions from the DaaS API at runtime.
If the form is still editable for tasks the Member didn’t create, CollectionForm may not be evaluating the item-level permission filter against the loaded item. See Troubleshooting below.
See Permission-Aware UI in Action
CollectionList automatically fetches GET /api/permissions/me on mount and gates its built-in create and delete buttons — no manual permission checks required. The tasks page from Tutorial 3 already works correctly:
"use client";
import { useRouter } from "next/navigation";
import { CollectionList } from "@/components/ui";
export default function TasksListPage() {
const router = useRouter();
return (
<CollectionList
collection="tasks"
enableSearch
enableFilter
enableCreate
enableDelete
enableSort
enableSelection
limit={25}
onCreate={() => router.push("/content/tasks/+")}
onItemClick={(item) => router.push(`/content/tasks/${item.id}`)}
/>
);
}- Manager: the Create button is visible and active; bulk delete is available.
- Member: the Create button is visible and active (Members have
create (all)on tasks); the bulk Delete button is disabled with a “Not allowed” tooltip.
No code changes are needed — CollectionList reads permissions at runtime.
If you need to gate a custom action beyond CRUD (e.g., an “Export” button), use the usePermissions hook:
import { usePermissions } from "@/lib/buildpad/hooks";
const { canPerform } = usePermissions({ collections: ["tasks"] });
{canPerform("tasks", "create") && <Button onClick={handleExport}>Export</Button>}Test with Both Accounts
Manager
- Can see the Create button
- Can open any task and edit all fields
- Can delete any task
Troubleshooting
Form is editable for tasks the Member didn’t create
Symptom: You sign in as the Member test account, open a task created by another user, and all fields are editable — nothing is read-only and the Delete button is visible.
Root cause: CollectionForm checks whether an update action exists in the user’s permissions (it does, because Members can update their own tasks). It does not evaluate the permissions filter — { "user_created": { "_eq": "$CURRENT_USER" } } — against the loaded item. So any task opens as editable regardless of who created it. DaaS still enforces ownership server-side (a save attempt on a non-owned task returns 403), but the UI does not reflect this restriction.
Diagnose: In the StudioDaaS Studio, open Access → Policies → Member policy → tasks → update. Confirm the permission rule shows user_created = $CURRENT_USER. If it does, the policy is correct — the issue is in CollectionForm.
Fix: Open Copilot Chat and paste:
CollectionForm shows editable fields when the Member opens a task they didn't create.
The Member update permission has an item-level filter: { "user_created": { "_eq": "$CURRENT_USER" } }.
Fix CollectionForm to evaluate the update permissions filter against the loaded item.
If the item's user_created field doesn't match the current user's ID, set updateAllowed = false
and mark all fields as readonly.How usePermissions Works
usePermissions({ collections }) returns a canPerform(collection, action) function that checks whether the current user’s policies allow a given action on a collection.
import { usePermissions } from "@/lib/buildpad/hooks";
const { canPerform } = usePermissions({ collections: ["tasks"] });
canPerform("tasks", "read") // → true for all authenticated users
canPerform("tasks", "create") // → true for Managers; true for Members (create (all))
canPerform("tasks", "update") // → true for Managers; true for Members (DaaS enforces own-items filter server-side)
canPerform("tasks", "delete") // → true for Managers; false for MembersItem-level ownership is enforced server-side. canPerform("tasks", "update") returns true for Members because they do have an update policy — DaaS applies the user_created = $CURRENT_USER filter when the actual save request is made. If a Member tries to update a task they don’t own, DaaS returns a 403.
What’s Next
Your app now has proper access control. In the next tutorial you’ll add a task workflow — so tasks move through defined stages with role-based transition rules.