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
} code | Meaning |
|---|---|
ALLOWED | Rolebook allowed it; decision is the Rolebook decision. |
FORBIDDEN | The actor has a relationship to the resource and Rolebook denied it; decision is usually present. |
NOT_FOUND_OR_DENIED | The 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 tomanagement.members. invitationsis present only when the actor is also allowedmanagement.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 (
teamby default) and are not expanded; comparesubject.typewith 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) isNOT_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); limitis an integer from 1 toMAX_AUDIT_PAGE_LIMIT(100).- Each event is a frozen
AuditHistoryEvent: aSharingAuditEventwithout the caller-chosenidempotencyKey. Actions aremember.granted,member.role_changed,member.revoked,ownership.transferred,invitation.issued,invitation.consumed,invitation.revoked,resource.initialized,resource.deleted, andresource.moved. An invitation issued or consumed under a nested-team profile also carrieslineageDirectandlineageWitness. Flat events omit both. nextCursoris present untilexhausted. Like a listing cursor it is the adapter’s position sealed with AES-256-GCM from the samelistingCursorKey, 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.
| Error | code | Cause |
|---|---|---|
AuditHistoryError | INVALID_LIMIT | limit is not an integer from 1 to 100, or the request is not a plain object of own data properties. |
AuditHistoryError | UNKNOWN_RESOURCE_TYPE | resource.type is not a configured resource type. |
InvalidCursorError | INVALID_CURSOR | cursor does not open as an audit cursor for the same tenant, actor, and resource under a key this instance holds. |
InvalidCandidatePageError | INVALID_CANDIDATE_PAGE | The 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.