Multi-tenant setup
Model tenants, load bindings and resources per tenant, and keep every input authoritative.
Rolebook has no notion of a tenant of its own: a tenant is simply the root of your resource hierarchy, and tenant isolation comes from how you load bindings and resources. This guide shows the shape that keeps it safe.
Model the tenant as the root resource
Make the tenant (an organization or a workspace) the root resource type, and give every tenant-owned type a parent chain that ends there:
import { definePolicy } from "rolebook";
export const policy = definePolicy({
version: "2026-11-01",
resources: {
workspace: { actions: ["read", "manage"] },
project: { parent: "workspace", actions: ["read", "update", "delete"] },
document: { parent: "project", actions: ["read", "update", "delete"] },
},
roles: {
workspace_admin: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "workspace:*" },
{ effect: "allow", permission: "project:*" },
{ effect: "allow", permission: "document:*" },
],
},
workspace_member: {
scope: "workspace",
rules: [{ effect: "allow", permission: "workspace:read" }],
},
project_editor: {
scope: "project",
rules: [
{ effect: "allow", permission: "project:read" },
{ effect: "allow", permission: "project:update" },
{ effect: "allow", permission: "document:read" },
{ effect: "allow", permission: "document:update" },
],
},
},
}); Because every resource’s ancestry ends at its workspace, an assignment on one workspace can never apply to a resource of another: the scope resource would not be in its ancestry.
Load the binding from the tenant
On every request, resolve the tenant from your routing or session, verify the user is a member of exactly that tenant, and load only that tenant’s assignments:
import { createAuthorizer, type BindInput, type RoleAssignment } from "rolebook";
interface AssignmentRow {
readonly role: "workspace_admin" | "workspace_member" | "project_editor";
readonly scopeType: "workspace" | "project";
readonly scopeId: string;
}
declare const db: {
isMember(tenantId: string, userId: string): Promise<boolean>;
assignments(tenantId: string, userId: string): Promise<readonly AssignmentRow[]>;
};
const authorizer = createAuthorizer(policy);
export async function bindingFor(
tenantId: string,
userId: string,
): Promise<BindInput<AssignmentRow["role"]> | undefined> {
if (!(await db.isMember(tenantId, userId))) return undefined;
const subject = { type: "user", id: userId };
const assignments: RoleAssignment<AssignmentRow["role"]>[] = (await db.assignments(tenantId, userId)).map(
(row) => ({
subject,
role: row.role,
scope: { resource: { type: row.scopeType, id: row.scopeId }, appliesTo: "descendants" },
}),
);
return { subject, assignments, context: { tenantId } };
}
export async function authorizationFor(tenantId: string, userId: string) {
const binding = await bindingFor(tenantId, userId);
return binding && authorizer.bind(binding);
} - The tenant comes from the request path or the session, never from the body. Use the same tenant ID for the membership check, the assignment query, and every resource query.
- Map rows to exactly the documented fields. An extra key on an assignment denies the binding, and a
nullorundefinedcolumn is not a valid value. - Bind once per request, not once per process. A binding is a snapshot. Positive decisions should not be cached across requests, because membership and assignments change.
Load resources by tenant and ID
Load every resource with a query that names both the tenant and the ID, and build the ancestry from stored parent links:
import { trustedResource, type TrustedResource } from "rolebook";
declare const documents: {
find(tenantId: string, id: string): Promise<
{ readonly id: string; readonly projectId: string; readonly ownerId: string; readonly archived: boolean } | undefined
>;
};
export async function loadDocument(tenantId: string, id: string): Promise<TrustedResource | undefined> {
const row = await documents.find(tenantId, id); // WHERE workspace_id = $1 AND id = $2
if (!row) return undefined;
return trustedResource(
{ type: "document", id: row.id },
[
{ type: "project", id: row.projectId },
{ type: "workspace", id: tenantId },
],
{ ownerId: row.ownerId, archived: row.archived },
);
}
export async function canUpdateDocument(tenantId: string, userId: string, documentId: string) {
const [authorization, document] = await Promise.all([
authorizationFor(tenantId, userId),
loadDocument(tenantId, documentId),
]);
if (!authorization || !document) return false; // treat "missing" and "not allowed" alike
return authorization.check({ permission: "document:update", resource: document }).allowed;
} A resource that exists in another tenant is simply not found by the tenant-scoped query, so a client cannot reach it by guessing IDs. Treat “not found” and “not allowed” the same in responses when existence itself is sensitive.
Many resources for one user
When you check one user against many resources (filtering a list), bind once and run every check on that one authorization:
export async function visibleDocuments(tenantId: string, userId: string, candidates: readonly TrustedResource[]) {
const authorization = await authorizationFor(tenantId, userId);
if (!authorization) return [];
return candidates.filter(
(resource) => authorization.check({ permission: "document:read", resource }).allowed,
);
} Each candidate must itself come from a tenant-scoped loader such as loadDocument above.
For listings that must page through large tables, private resources shared per user, and invitations, use the sharing packages: they own the storage, candidate queries, and transactional fences.
Teams and other groups
If users act through groups, load the user’s groups in the same tenant and pass them as principals. Load assignments for the user and for each of its groups, from the same tenant-scoped source. Never trust group membership from a token or the client.
Checklist
- The tenant ID used for a request comes from routing or the session, and is the same for membership, assignments, groups, and resources.
- Every resource query filters by tenant and ID.
- Ancestry is built from stored parent links and ends at the tenant.
- Context values a client can influence are not used as authority.
- The policy is defined once and reused; bindings are per request.
- Policy changes are reviewed with
diffPolicies()before they ship.