Quick start
Define a policy, bind a subject, check a permission.
This page walks through one complete permission check in four short steps:
- Define a policy with three resource types (organization, workspace, project) and an editor role.
- Bind the signed-in user and the roles your app loaded for them.
- Pass the target project as a trusted resource (with its parent workspace and organization) and call
check(). - Throw on denial with
enforce(), or inspect which rules ran withexplain().
1. Define a policy
Resources declare their concrete actions and at most one parent type. Roles live at a scope and hold
allow and deny rules. Define the policy once, at module level, and reuse the returned object:
definePolicy() validates it, freezes it, and caches its compiled form by object identity.
import { definePolicy } from "rolebook";
export const policy = definePolicy({
version: "4.0.0",
resources: {
organization: { actions: ["read"] },
workspace: { parent: "organization", actions: ["read", "update"] },
project: { parent: "workspace", actions: ["read", "update", "delete"] },
},
roles: {
workspace_editor: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "project:read" },
{ effect: "allow", permission: "project:update" },
],
},
},
}); definePolicy() throws an Error for a malformed policy, so a bad policy fails at startup rather than
on a request. Permission strings are typed: "project:archive" would not compile, because project
does not declare archive.
2. Bind the request
Load the subject’s scoped assignments for this request from your own authoritative store, and bind them together with the authenticated subject. The subject itself never carries roles.
import { createAuthorizer } from "rolebook";
const authorization = createAuthorizer(policy).bind({
subject: { type: "user", id: "user_123", attributes: { mfa: true } },
assignments: [
{
subject: { type: "user", id: "user_123" },
role: "workspace_editor",
scope: {
resource: { type: "workspace", id: "ws_456" },
appliesTo: "descendants",
},
},
],
context: { requestId: "req_789" },
}); appliesTo: "descendants" makes the assignment cover the workspace itself and everything below it.
3. Check against a trusted resource
Load the resource through your authoritative, tenant-scoped data path, then snapshot it with its complete ancestry, from the immediate parent to the root.
import { trustedResource } from "rolebook";
const project = trustedResource(
{ type: "project", id: "project_1" },
[
{ type: "workspace", id: "ws_456" },
{ type: "organization", id: "org_1" },
],
{ ownerId: "user_123", status: "draft" },
);
const decision = authorization.check({ permission: "project:update", resource: project });
// decision.allowed === true
// decision.code === "ALLOWED"
// decision.matched -> { effect: "allow", role: "workspace_editor", permission: "project:update" } The assignment applies because ws_456 is in the project’s ancestry. Checking project:delete
returns { allowed: false, code: "NOT_GRANTED" }: no applicable rule allows it.
4. Enforce or explain
check() never throws for malformed input; it returns a closed decision with a stable code.
enforce() throws AuthorizationError on any denial, which suits handlers that should stop. explain()
runs the same evaluation and adds a bounded trace of the rules it evaluated.
import { AuthorizationError } from "rolebook";
try {
authorization.enforce({ permission: "project:delete", resource: project });
} catch (error) {
if (error instanceof AuthorizationError) console.log(error.decision.code); // "NOT_GRANTED"
}
const explanation = authorization.explain({ permission: "project:update", resource: project });
for (const entry of explanation.trace) {
console.log(entry.outcome, entry.effect, entry.role, entry.permission);
} Next
Policies
Resources, actions, rules, wildcards, and the policy lifecycle.
Roles and scopes
Where a role lives, what it may target, and inheritance.
Multi-tenant setup
Loading assignments and resources per tenant, safely.
Route middleware
Guard Hono, Express 5, and Fastify 5 routes with a binding and resource per request.
Testing your integration
Policy tests as a table or as JSON, and the adapter contracts.
Choose how decisions run
Sharing, the HTTP API, the decision service, and offline snapshots around this engine.