RolebookDocs

ACL recipe

Scoped roles, explicit deny, revocation, and resource membership commands.

An access-control list in Rolebook is a scoped role assignment. This recipe uses one tenant, its projects, and its documents. Alice administers tenant A. Bob can read one document. Carol is a tenant member with no assignment. A second assignment can deny Bob, and removing Bob’s own assignment does not revoke a grant he still holds through a team.

The runnable program is examples/acl.ts in a checkout of this repository:

bun examples/acl.ts

Examples are not part of the npm package. Load assignments and ancestry from server-owned records. The objects below are an in-memory stand-in for that store, so the example can run without a database.

One policy

Roles are defined once. tenant_admin names document:read and document:update. It is an ordinary role: there is no bypass role, and a wildcard such as tenant:* grants only tenant actions. It does not grant operations on projects or documents.

import { createAuthorizer, definePolicy, trustedResource } from "rolebook";

const policy = definePolicy({
  version: "acl-recipe",
  resources: {
    tenant: { actions: ["read"] },
    project: { parent: "tenant", actions: ["read"] },
    document: { parent: "project", actions: ["read", "update"] },
  },
  roles: {
    tenant_admin: {
      scope: "tenant",
      rules: [
        { effect: "allow", permission: "document:read" },
        { effect: "allow", permission: "document:update" },
      ],
    },
    document_reader: {
      scope: "document",
      rules: [{ effect: "allow", permission: "document:read" }],
    },
    document_read_denied: {
      scope: "document",
      rules: [{ effect: "deny", permission: "document:read" }],
    },
  },
});

const authorizer = createAuthorizer(policy);
const documentA1 = trustedResource({ type: "document", id: "doc_a1" }, [
  { type: "project", id: "project_a" },
  { type: "tenant", id: "tenant_a" },
]);
const documentB1 = trustedResource({ type: "document", id: "doc_b1" }, [
  { type: "project", id: "project_b" },
  { type: "tenant", id: "tenant_b" },
]);

const alice = authorizer.bind({
  subject: { type: "user", id: "alice" },
  assignments: [
    {
      subject: { type: "user", id: "alice" },
      role: "tenant_admin",
      scope: { resource: { type: "tenant", id: "tenant_a" }, appliesTo: "descendants" },
    },
  ],
});
alice.check({ permission: "document:read", resource: documentA1 }).allowed; // true
alice.check({ permission: "document:read", resource: documentB1 }).allowed; // false

Build trustedResource from the row your server loaded for (tenantId, documentId). The ancestor list is the complete chain from the immediate parent to the root. An id in the URL selects which row to load. It does not prove that the caller owns that row, and it does not prove the parent ids.

PrincipalAssignmentResult
Alicetenant_admin on tenant A, descendantsDocument read and update in A. No rights in B.
Bobdocument_reader on document A1, selfRead A1. Cannot update A1, read A2, or read B1.
CarolTenant member, assignments: []No document access. Membership is not a role.
Bob, plus a denydocument_read_denied on A1 for document:readThe deny wins.

appliesTo: "descendants" includes the scope resource itself and everything below it in the ancestry you supplied. appliesTo: "self" is only that resource.

Revocation is a new binding

bind keeps its own snapshot of the assignments. Changing or deleting the caller’s array does not change an authorizer you already hold. The next request builds a new binding from the store after the deletion.

A team assignment is a second source. Removing Bob’s direct row leaves the team’s document_reader grant in place, so Bob can still read A1 while he remains a member of that team. Revoking one grant is not a promise that every grant is gone.

Sharing commands

For a real membership store, @rolebook/sharing keeps one live membership per subject and resource. grantRole sets that membership’s role. Granting again does not add a second permission; changeRole replaces the role, and revokeMember removes the membership. The membership label, such as reader, is not the policy role it maps to, such as document_reader. The role applies to that resource itself.

Only an eligible tenant member can be granted, and only an actor who may manage members can grant. A manager who is not the owner can hand out only access they hold. See setup, commands, and delegation. This recipe does not start a second sharing application.

import { definePolicy } from "rolebook";
import {
  createHmacInvitationTokens,
  createSharing,
  defineSharing,
  generateInvitationTokenKey,
  type SharingAdapter,
  type TenantIdentityAdapter,
} from "@rolebook/sharing";

const sharingPolicy = definePolicy({
  resources: {
    document: {
      actions: ["read", "update", "manage_members", "manage_invitations", "transfer_ownership"],
    },
  },
  roles: {
    document_owner: {
      scope: "document",
      rules: [{ effect: "allow", permission: "document:*" }],
    },
    document_reader: {
      scope: "document",
      rules: [{ effect: "allow", permission: "document:read" }],
    },
    document_manager: {
      scope: "document",
      rules: [
        { effect: "allow", permission: "document:manage_members" },
        { effect: "allow", permission: "document:manage_invitations" },
        { effect: "allow", permission: "document:read" },
      ],
    },
  },
});

declare const documents: SharingAdapter;
declare const identity: TenantIdentityAdapter;

const sharing = createSharing({
  definition: defineSharing({
    policy: sharingPolicy,
    resources: {
      document: {
        memberRoles: { reader: "document_reader", manager: "document_manager" },
        ownershipRole: "document_owner",
        actions: { read: "document:read", update: "document:update" },
        management: {
          members: "document:manage_members",
          invitations: "document:manage_invitations",
          ownership: "document:transfer_ownership",
        },
        adapter: documents,
      },
    },
  }),
  identity,
  invitationTokens: createHmacInvitationTokens(generateInvitationTokenKey()),
});

const tenant = sharing.forTenant({
  tenantId: "tenant_a",
  actor: { type: "user", id: "alice" },
});

await tenant.execute({
  kind: "grantRole",
  resource: { type: "document", id: "doc_a1" },
  subject: { type: "user", id: "bob" },
  role: "reader",
  idempotencyKey: "grant-bob-doc_a1",
});
await tenant.execute({
  kind: "changeRole",
  resource: { type: "document", id: "doc_a1" },
  subject: { type: "user", id: "bob" },
  role: "manager",
  idempotencyKey: "change-bob-doc_a1",
});
await tenant.execute({
  kind: "revokeMember",
  resource: { type: "document", id: "doc_a1" },
  subject: { type: "user", id: "bob" },
  idempotencyKey: "revoke-bob-doc_a1",
});