RolebookDocs

Authorize, permissions, and inspection

authorize(), permissions(), describeAccess(), and auditHistory().

authorize

authorize({ resource, action }) decides one configured action label for the actor:

const decision = await tenant.authorize({ resource: { type: "project", id: projectId }, action: "update" });

if (decision.allowed) {
  decision.decision.matched; // the Rolebook rule that allowed it
} else if (decision.code === "FORBIDDEN") {
  decision.decision?.code; // the Rolebook denial, for a caller who knows the resource exists
}
codeMeaning
ALLOWEDRolebook allowed it; decision is the Rolebook decision.
FORBIDDENThe actor has a relationship to the resource and Rolebook denied it; decision is usually present.
NOT_FOUND_OR_DENIEDThe resource is missing, the type or action is not configured, the actor is not an eligible member, or the actor has no relationship to the resource. No decision.

The module asks the storage adapter for the memberships of the actor and its groups only (requestingPrincipals), so an adapter need not load every member of a widely shared resource to answer.

permissions

permissions({ resource }) decides every configured action of the resource in one read, for a page that would otherwise call authorize() once per button. It resolves the principal once, reads once with the same projection authorize() uses, binds once, and checks each action, so every entry equals the authorize() answer for that action at the same sharing revision and instant.

const result = await tenant.permissions({ resource: { type: "project", id: projectId } });
if (result.ok) {
  const { allowed, decisions, sharingRevision } = result.permissions;
  // allowed: { read: true, update: true, delete: false }, one entry per configured action label
}

allowed holds one boolean per configured action label, in configuration order, as a frozen null-prototype record of plain data: send it to a front end and gate controls on it there. decisions holds the Rolebook decision behind each entry. When at least one action is allowed, every action is reported. When none is (and for a missing resource, an ineligible actor, an unconfigured type, or malformed input) the answer is { ok: false, code: "NOT_FOUND_OR_DENIED" }.

Expiry does not bump the sharing revision, so sharingRevision alone must not key a decision cache: a cached answer can outlive a membership that expired at the same revision.

describeAccess

describeAccess({ resource }) answers a share dialog: the owner, every live membership (individuals and groups alike, each with its role label and optional expiresAt), and the pending invitations without their token digests.

const result = await tenant.describeAccess({ resource: { type: "project", id: projectId } });
if (result.ok) {
  const { owner, members, invitations, sharingRevision } = result.access;
  for (const member of members) console.log(member.subject, member.role, member.expiresAt);
}
  • It requires management.inspect, which defaults to management.members.
  • invitations is present only when the actor is also allowed management.invitations, so a members-only manager never learns who was invited. Pending means issued and neither consumed, revoked, nor expired.
  • Ownership and memberships come from one storage snapshot at sharingRevision; the invitations are a second read and may observe a later revision.
  • Groups keep their own subject type (team by default) and are not expanded; compare subject.type with your group type to label them.
  • A membership whose label names an unavailable custom role carries roleUnavailable: true.
  • Every refusal (a missing resource, an ineligible actor, an unconfigured type, an actor without inspect, adapter output that fails the module’s capture) is NOT_FOUND_OR_DENIED. An adapter that meets a stored invitation row it cannot decode may throw its own row error instead.

auditHistory

Every mutation commits an audit event in the same transaction. auditHistory({ resource, limit, cursor }) pages them, newest first by (occurredAt, id), for an actor allowed management.inspect:

let cursor: string | undefined;
do {
  const result = await tenant.auditHistory({
    resource: { type: "project", id: projectId },
    limit: 50,
    ...(cursor === undefined ? {} : { cursor }),
  });
  if (!result.ok) break;
  for (const event of result.page.events) console.log(event.occurredAt, event.action, event.actor);
  cursor = result.page.exhausted ? undefined : result.page.nextCursor;
} while (cursor !== undefined);
  • limit is an integer from 1 to MAX_AUDIT_PAGE_LIMIT (100).
  • Each event is a frozen AuditHistoryEvent: a SharingAuditEvent without the caller-chosen idempotencyKey. Actions are member.granted, member.role_changed, member.revoked, ownership.transferred, invitation.issued, invitation.consumed, invitation.revoked, resource.initialized, resource.deleted, and resource.moved. An invitation issued or consumed under a nested-team profile also carries lineageDirect and lineageWitness. Flat events omit both.
  • nextCursor is present until exhausted. Like a listing cursor it is the adapter’s position sealed with AES-256-GCM from the same listingCursorKey, under its own label, so an audit cursor never opens as a listing cursor or the reverse. It is bound to the tenant, the actor, the resource type, and the resource ID, and is always 1,423 characters.
  • A cursor dies with the event it points after: draining the audit table, or deleting and re-creating the resource under the same ID, makes the next call throw InvalidCursorError. Restart from the first page.
  • History covers the resource’s current incarnation only. Every initialization of a key mints an opaque incarnation identifier, kept across ownership transfers and moves; events are stamped with it, and only events of the live incarnation are listed. After a deletion and re-creation under the same ID, the new resource’s inspectors never see the earlier actors, grantees, or invitation IDs.
  • A deleted resource’s history is reachable only by draining the table. Every other refusal is NOT_FOUND_OR_DENIED.
ErrorcodeCause
AuditHistoryErrorINVALID_LIMITlimit is not an integer from 1 to 100, or the request is not a plain object of own data properties.
AuditHistoryErrorUNKNOWN_RESOURCE_TYPEresource.type is not a configured resource type.
InvalidCursorErrorINVALID_CURSORcursor does not open as an audit cursor for the same tenant, actor, and resource under a key this instance holds.
InvalidCandidatePageErrorINVALID_CANDIDATE_PAGEThe adapter returned a malformed page; an adapter contract failure.

These checks run on the captured request before any identity or storage callback. The cursor is opened before the resource ID is checked, so a malformed resource ID passed with a cursor throws InvalidCursorError.

auditHistory() is a read for activity panels and compliance views, not a delivery mechanism. To send audit events elsewhere, drain the adapter’s audit table from a job of your own.