Rolebook

Sharing and teams

Sharing that works the way people expect

Rolebook Sharing (@rolebook/sharing) lets your users share projects, folders and documents with colleagues and teams, and keeps the record in your own database. Rolebook decides every step, filters your own lists to what each person may open, and lets support see why.

How it works

01 · Sharing and teams

Share an item with people and teams

What it does
Owners and managers give a person or a team a role on one item, change it, or take it away.
Why it matters
Being in a workspace does not open its private items. People see what was shared with them, and nothing more.
How it works
Each change is one command, checked by Rolebook and safe to retry with the same idempotency key. The PostgreSQL and SQLite adapters save it in one transaction with its history entry.
share.ts
const tenant = sharing.forTenant({ tenantId: "acme", actor: { type: "user", id: "sam" } });

// Share Brand assets with the Design team as editors, until 31 March.
await tenant.execute({
  kind: "grantRole",
  resource: { type: "project", id: "brand_assets" },
  subject: { type: "team", id: "design" },
  role: "editor",
  expiresAt: new Date("2027-03-31T00:00:00Z"),
  idempotencyKey: "share-brand-assets",
});
// { ok: true, code: "APPLIED", ... }

const history = await tenant.auditHistory({
  resource: { type: "project", id: "brand_assets" },
  limit: 50,
});
// newest first, a page at a time

Read the docs: Share an item with people and teams

02 · Sharing and teams

Member roles

What it does
You decide which roles a member can have on each kind of item, such as Viewer and Editor, and which role the owner has.
Why it matters
The words your users see in a share dialog map to rules you wrote once.
How it works
Each member role points to a role in your policy. The sharing layer turns memberships into assignments and asks Rolebook for the final answer.

Read the docs: Member roles

03 · Sharing and teams

Invitations

What it does
Invite an existing user to an item by account or by verified email address, with a role and an end date.
Why it matters
People can be brought in before they open the item, and an invitation cannot be passed on to someone else.
How it works
Each invitation has its own token and expiry, and only its recipient can accept it. When it is accepted, Rolebook checks again that whoever sent it may still give that access.
invite.ts
await tenant.execute({
  kind: "issueInvitation",
  resource: { type: "project", id: "brand_assets" },
  recipient: { type: "email", email: "ada@example.com" },
  role: "editor",
  expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // the invitation lapses in 7 days
  membershipExpiresAt: new Date("2027-03-31T00:00:00Z"), // the access it gives ends on 31 March
  idempotencyKey,
});
// { ok: true, code: "APPLIED", invitation: { id, token }, ... }

Read the docs: Invitations

04 · Sharing and teams

Access that ends by itself

What it does
A membership or an invitation can carry an end date. After it, that membership grants nothing.
Why it matters
Contractors and guests keep access only as long as they need it, without anyone remembering to remove it.
How it works
A membership at or past its end date grants nothing and drops out of every read. The row stays stored until it is replaced or removed.
  1. 31 MarRolebook endedAda ParkCan edit · Brand assets
  2. 1 MarSam Lee gaveAda ParkCan edit until 31 Mar · Brand assets

Read the docs: Access that ends by itself

05 · Sharing and teams

Ownership transfer

What it does
Every item has exactly one owner, who can hand it over to someone else.
Why it matters
When a person leaves or changes teams, their work moves on without losing who is responsible for it.
How it works
A dedicated command moves ownership. The former owner can stay on as a member with a role you choose. A team can never own an item. A former owner who retries the same hand-over with the same idempotency key gets the saved answer, even though they no longer own the item.
transfer.ts
await tenant.execute({
  kind: "transferOwnership",
  resource: { type: "project", id: "brand_assets" },
  to: { type: "user", id: "ada" },
  formerOwnerRole: "editor", // Sam stays on as an editor
  idempotencyKey,
});

Read the docs: Ownership transfer

06 · Sharing and teams

Delegation limits

What it does
A manager can only hand out, change or remove access they hold themselves on that item.
Why it matters
Sharing rights cannot be used to climb: a manager cannot give anyone more than they have, or lock the owner out.
How it works
Every grant, change and invitation is compared with the actor's own rules. Only the owner may give a team a role that blocks something, and the owner can always undo it.

Read the docs: Delegation limits

07 · Sharing and teams

Who has access, and what can I do here

What it does
Answers for your screens: everything a person may do on an item, who it is shared with, and the items a person can open, page by page.
Why it matters
Share dialogs, member lists and buttons show exactly what Rolebook would allow, without one check per button.
How it works
permissions() decides every action in one read. describeAccess() lists the owner, members and pending invitations. listAccessible() pages through the items a person may do one action on, and listAccess() through every item they may do anything on, with the answer for each action. Rolebook confirms every result, so a database query never decides access.
  • Each page is read at one point in time (items, identity, roles, and expiry dates), so both the listed items and the "no more items" flag reflect a single real state.
  • If a consistent snapshot cannot be read, the call returns a typed error instead of a partial or stale list.
  • Consistent paging uses the in-memory adapter's own snapshot, the checked executor on PostgreSQL, or the worker-backed executor on SQLite, paired with identity read from the same point (such as a local projection table).
  • Each page takes a fresh snapshot (up to 100 items per page, scanning at most 2,000 candidates). Items changed between pages may move, but never appear twice.
  • describeAccess() shows what is stored on one item (owner, direct members, and pending invitations); it does not list everyone a workspace role or condition would allow.
access.ts
const permissions = await tenant.permissions({ resource: { type: "project", id: "brand_assets" } });
// permissions.permissions.allowed: { read: true, update: true }

const access = await tenant.describeAccess({ resource: { type: "project", id: "brand_assets" } });
// access.access: { owner, members, invitations, sharingRevision }

const page = await tenant.listAccessible({ type: "project", action: "read", limit: 20 });
// page: { resources, nextCursor?, exhausted }

Read the docs: Who has access, and what can I do here

08 · Sharing and teams

Why can or can't this person do that

What it does
A support engineer or an admin asks why one person can or can't do one thing to one item right now, and reads the decision with the rules behind it.
Why it matters
Support can answer "why can't I open this?" from your real rules and your real data, without guessing and without signing in as the customer.
How it works
explain() is off until you switch it on with your own rate limiter and audit log. Only someone allowed to inspect the item can ask, by default whoever may manage its members; anyone else gets the same answer as for an item that does not exist. Each answer waits until your audit log has recorded it, and it is an explanation to read, never a permission.
  • Served over HTTP as sharing.explain, and called by @rolebook/client and the Python SDK. The Go and Rust SDKs do not call it.
  • The answer leaves out free-text reasons, the values conditions compared, people's attributes, emails and tokens.
  • At most 32 inspections run at once per instance, and each one has a 5-second deadline.
inspect.ts
// Off unless createSharing() was given inspection: { audit, limiter }.
const support = sharing.forTenant({
  tenantId: "acme",
  actor: { type: "user", id: "support_1" }, // the person asking
});

const result = await support.explain({
  resource: { type: "project", id: "brand_assets" },
  action: "update",
  subject: { type: "user", id: "ada" }, // whose access to explain
});

if (result.ok) {
  const { decision } = result.explanation;
  decision.code;  // "ALLOWED", "EXPLICIT_DENY" or "NOT_GRANTED"
  decision.trace; // the rules evaluated, with their conditions
} else {
  result.code;    // for example "NOT_FOUND_OR_DENIED" or "RATE_LIMITED"
}

Read the docs: Why can or can't this person do that

09 · Sharing and teams

Your own lists, showing only what each person may open

What it does
Register a query over your own table, with filters and one sort order, then page through it. Every item on a page is one the person may open.
Why it matters
Lists, searches and dashboards built on your own columns never show an item someone couldn't open, and a database filter never decides access.
How it works
Each candidate row gets the same decision as a single check, and a page reads and checks everything inside one database snapshot. A page returns item keys, not rows: load what you show through your own query. Cursors are sealed, tied to the person, and expire after 15 minutes.
  • PostgreSQL, and SQLite on Bun through its worker executor.
  • Up to 100 items a page, 25 by default. A page checks at most 2,000 rows and can come back short, with a cursor to continue.
  • Queries run in your server code with the filters and the sort you registered. They are not served over HTTP.
  • Which items a filter returns, and in what order, can reveal the filtered values, so register a separate query where audiences differ.
query.ts
const tenant = queries.forTenant({
  tenantId: "acme",
  actor: { type: "user", id: "ada" },
  sessionBinding: session.authorityId,
});

const result = await tenant.query({
  name: "recentDocuments", // registered once, with its filters and its sort
  filters: [{ field: "status", op: "in", value: ["open", "review"] }],
  limit: 50,
});

if (result.ok) {
  result.page.items;      // [{ type: "document", id }, ...]: only keys Ada may read
  result.page.nextCursor; // sealed; pass it back as cursor for the next page
}

Read the docs: Your own lists, showing only what each person may open

10 · Sharing and teams

Custom roles per customer

What it does
Each of your customers can define their own roles, based on a role you provide.
Why it matters
Customers get roles that match their team, and you keep control of what any role can reach.
How it works
Part of the versioned sharing setup. A custom role starts from a template role and can never exceed it, and it keeps the template's blocks. A role that no longer fits its template stops granting anything. Each customer can have up to 32.

Read the docs: Custom roles per customer

11 · Sharing and teams

Inherited folder access

What it does
Grant someone a role on a folder and have it apply to the subfolders and documents inside it, up to 16 levels deep.
Why it matters
Teams can share a whole folder once instead of sharing each document one by one.
How it works
Opt in per resource type on PostgreSQL or SQLite with inheritance: "ancestors", and decide before storing data: unenrolled tenants cannot read or mutate opted-in types, and enrollment refuses tenants that already have resources outside the managed hierarchy. Once a tenant is enrolled and activated, memberships on parent folders flow down, ownership does not, explicit blocks still win at every level, and a missing or inconsistent chain fails closed.

Read the docs: Inherited folder access

12 · Sharing and teams

Audit history and retention

What it does
Every grant, change, removal, invitation and hand-over is recorded, and can be read newest first.
Why it matters
You can always answer who gave access to what, and when, for support and for compliance reviews.
How it works
The PostgreSQL and SQLite adapters save each history entry in the same transaction as its change. Only people who may manage the item can read it. A maintenance job deletes settled invitations and old retry records after a retention you choose (30 days by default), and never deletes history.
audit.ts
const result = await tenant.auditHistory({
  resource: { type: "project", id: "brand_assets" },
  limit: 50,
});
if (result.ok) {
  const { events, nextCursor, exhausted } = result.page;
  // events are newest first; pass nextCursor back as cursor for more
}

Read the docs: Audit history and retention

13 · Sharing and teams

Teams

What it does
Share an item with a team, and every current member gets the team's role. Decisions name the team access came through.
Why it matters
Access follows team membership: someone who leaves the team loses that access on their next request, while a direct share to them stays.
How it works
@rolebook/teams reads teams from Better Auth or your own SQL tables. Flat teams (withTeams) are the default; optional bounded nested team lineage (withTeamLineage) is available on PostgreSQL and SQLite. Teams can never act or own items, a person can hold at most 64 teams, and database fences recheck membership inside every write.
teams.ts
await tenant.execute({
  kind: "grantRole",
  resource: { type: "project", id: "brand_assets" },
  subject: { type: "team", id: "design" },
  role: "editor",
  idempotencyKey,
});

// Ada is on the Design team.
const decision = await sharing
  .forTenant({ tenantId: "acme", actor: { type: "user", id: "ada" } })
  .authorize({ resource: { type: "project", id: "brand_assets" }, action: "update" });

decision.decision?.matched?.via; // { type: "team", id: "design" }

Read the docs: Teams

Next: Integrations and tools

Ship enterprise-ready access control from day one.

Allow wisely.