Roles and Grants
An App declares who calls each operation and who gets each Item. A Role names the people, and a Grant gives a Role an operation or the Items of a Template. The Runtime checks both in every call that runs as a person, and a handler checks a Role where a Grant does not say enough.
Builder calls
Section titled “Builder calls”app.addRole
Section titled “app.addRole”app.addRole(key: string, options: AddRoleOptions): AppRoleBuilderrole.extend(role: BaseRoleBuilder): BaseRoleBuilderrole.prn: stringDeclares an App Role: a Role that a person holds once it is assigned to them. A Grant names the Role, and a handler checks it.
| Argument | Type | Description |
|---|---|---|
key |
string |
A lowercase letter, then letters and digits, 64 characters at most. An App uses a Role key once. |
options |
AddRoleOptions |
The options below. Pass {} for none. |
| Option | Description |
|---|---|
label |
The label of the Role. Without it, the Role has none. |
Returns the Role. role.prn is its PRN, as prn::leave:role/employee.
| Modifier | What it declares |
|---|---|
extend |
That a person who holds this Role holds role too. role is an App Role. The Builder refuses Roles that extend one another. Returns this Role. |
const employee = app.addRole('employee', { label: 'Employee' }); const manager = app.addRole('manager', { label: 'Manager' }); manager.extend(employee);Behavior
- A person who holds a Role calls the operations granted to it, and the Runtime refuses them every other operation.
- A person who holds a Role that extends another calls the operations granted to the other, and gets the Items it gets.
- Where a third Role extends the second, a person who holds it holds all three.
See: Roles and Grants, Grant access, Tutorial: who can do what, AppBuilder.addRole.
addGrant
Section titled “addGrant”mutation.addGrant(role: BaseRoleBuilder): OperationGrantquery.addGrant(role: BaseRoleBuilder): OperationGrantDeclares a Grant on an operation: a person who holds the Role calls the Mutation or the Query. Call it once for each Role.
| Argument | Type | Description |
|---|---|---|
role |
BaseRoleBuilder |
An App Role, as app.addRole returns it. |
Returns the Grant. The sample grants three operations to every employee, and two to every manager.
request.addGrant(employee); cancel.addGrant(employee); list.addGrant(employee); approve.addGrant(manager); reject.addGrant(manager);Behavior
- An operation with no Grant refuses every person.
- The Runtime refuses a person who holds none of the Roles before it reads the input or runs the handler. The call throws.
- Where an operation has a Grant for each of two Roles, a person who holds either one calls it.
- An operation that a handler calls through
currentchecks its own Grants, for the person the call runs as.
See: Roles and Grants, Grant access, Tutorial: who can do what, MutationBuilder.addGrant.
template.addRole
Section titled “template.addRole”template.addRole( key: string, callback: TemplateRoleCallback,): TemplateRoleBuilderhandler(item: ReadItem): Promise<string[]>Declares a Template Role: a Role that a person holds on one Item at a time. The second signature is the callback. It gets an Item of the Template, and returns who holds the Role on that Item: Identifiers of people, PRNs of App Roles, or both.
| Argument | Type | Description |
|---|---|---|
key |
string |
A key as for an App Role. A Template uses a Role key once. |
callback |
TemplateRoleCallback |
The function to run: the second signature above. |
Returns the Role. In the sample the person who opened a ticket holds opener on it, and every agent holds agents on every ticket.
const opener = ticket.addRole('opener', async (item) => { const openedBy = item.properties.openedBy; return typeof openedBy === 'string' ? [openedBy] : []; }); const agents = ticket.addRole('agents', async () => [agent.prn]);Behavior
- A person holds the Role on the Items for which the callback returns their Identifier, and on no other Item.
- A person holds the Role on each Item for which the callback returns the PRN of an App Role they hold.
- The PRN of a Role that their Role extends counts as well.
See: Roles and Grants, Grant access, Let people see only their own items, Support tickets with an SLA, TemplateBuilder.addRole.
template.addGrant
Section titled “template.addGrant”template.addGrant( role: BaseRoleBuilder, properties: PropertyBuilder[],): TemplateGrantBuildergrant.addProperty(property: PropertyBuilder | PropertyBuilder[]): voidDeclares a Grant on a Template: a person who holds the Role on an Item gets that Item, and reads the Properties the Grant lists.
| Argument | Type | Description |
|---|---|---|
role |
BaseRoleBuilder |
A Role of the Template, as template.addRole returns it. |
properties |
PropertyBuilder[] |
The Properties the Grant lists. Object.values(leaveRequest.properties) is every Property the Template has when the call is made. |
Returns the Grant.
| Modifier | What it declares |
|---|---|
addProperty |
One more Property of the Grant, or a list of them. |
const leaveRequest = app.addTemplate('leaveRequest'); leaveRequest.addProperty('reason', 'string'); leaveRequest.addProperty('days', 'integer');
const staff = leaveRequest.addRole('staff', async () => [employee.prn]); leaveRequest.addGrant(staff, Object.values(leaveRequest.properties));Behavior
- Where a person holds no Role that a Grant names on an Item, a lookup gives them
undefinedfor it, and a selector leaves it out. - A Role that no Grant names gives no Item to the people who hold it.
- A person reads the Properties that the Grant of their Role lists, and with two Roles the Properties of both Grants.
- An App adds a Role and a Grant to a Template of an App it imports. The people who hold that Role then get its Items.
See: Roles and Grants, Grant access, Tutorial: who can do what, Employee onboarding checklists, TemplateBuilder.addGrant.
In a handler
Section titled “In a handler”checkRole, assertRole
Section titled “checkRole, assertRole”current.checkRole(role: ReadAppRoleInstance): booleancurrent.assertRole(role: ReadAppRoleInstance): truecurrent.roles: Record<string, ReadAppRoleInstance>role.checkRole(): booleanrole.assertRole(): trueChecks whether the person the call runs as holds an App Role. checkRole answers true or false. assertRole returns true, or throws an AssertRoleFailedError. current.roles holds the Roles of the App by key, and each Role has both calls for itself, as current.roles.admin.assertRole().
| Argument | Type | Description |
|---|---|---|
role |
ReadAppRoleInstance |
A Role of current.roles. |
In the sample every editor renames a page, a locked page takes an admin, and the note says who renamed it.
const rename = app.addMutation('rename', async (input: RenameInput, current) => { const item = await current.templates.page.getItem(input.id); if (item === undefined) throw new Error('Page not found'); if (item.properties.locked === true) current.assertRole(current.roles.admin); const by = current.checkRole(current.roles.admin) ? 'an admin' : 'an editor'; return await item.updateProperties({ note: `Renamed by ${by}`, title: input.title }); });Behavior
checkRolegivestruefor a Role the person holds and for each Role it extends, andfalsefor every other Role.assertRolethrows for a Role the person does not hold. The call throws that error.- The calls of a Role answer as the calls of
currentdo for that Role.
See: Roles and Grants, Grant access, ReadAppInstance.checkRole, ReadAppInstance.assertRole.
item.checkRole, item.assertRole
Section titled “item.checkRole, item.assertRole”item.checkRole(role: TemplateRoleInstance): Promise<boolean>item.assertRole(role: TemplateRoleInstance): Promise<true>item.currentRoles(): Promise<BaseRoleInstance[]>template.roles: Record<string, TemplateRoleInstance>Checks whether the person the call runs as holds a Template Role on one Item. Each call runs the callback of the Role for that Item. template.roles holds the Roles of a Template by key. item.currentRoles gives every Role of the Template that the person holds on the Item.
| Argument | Type | Description |
|---|---|---|
role |
TemplateRoleInstance |
A Role of the Template of the Item, as roles.author in the sample. |
In the sample only its author submits a draft, and the draft of an author who is an admin is approved at once.
const submit = app.addMutation('submit', async (input: IdInput, current) => { const item = await current.templates.draft.getItem(input.id); if (item === undefined) throw new Error('Draft not found'); const roles = current.templates.draft.roles; await item.assertRole(roles.author); const reviewed = await item.checkRole(roles.admins); return await item.updateProperties({ status: reviewed ? 'approved' : 'submitted' }); });Behavior
checkRolegivestrueon an Item for which the callback returns the Identifier of the person or the PRN of an App Role they hold, andfalseon any other.assertRolethrows for a Role the person does not hold on the Item. The call throws that error.currentRolesgives the Roles the person holds on the Item, and no other Role of the Template.
See: Roles and Grants, Grant access, ReadItem.checkRole, ReadItem.assertRole.
assign, revoke
Section titled “assign, revoke”role.assign(user: BaseIdentityUserItem | 'current'): Promise<void>role.revoke(user: BaseIdentityUserItem): Promise<boolean>current.roles: Record<string, WriteAppRoleInstance>Assigns an App Role to a person, and takes it away. In a handler that writes, each Role of current.roles has both calls. A person is an Item of the Template user of the App identity.
| Argument | Type | Description |
|---|---|---|
user |
BaseIdentityUserItem | 'current' |
For assign: the person, as user in the sample, or 'current' for the person the call runs as. |
user |
BaseIdentityUserItem |
For revoke: the person, in the same form. |
assign returns nothing. revoke returns whether the person held the Role.
const setEditor = app.addMutation('setEditor', async (input: SetEditorInput, current) => { const templatePrn = 'prn::identity:template/user'; const user: BaseIdentityUserItem = { id: input.id, templatePrn }; if (input.editor) { await current.roles.editor.assign(user); return true; } return await current.roles.editor.revoke(user); });Behavior
- After
assign, the person holds the Role in every call they make. revokegivestruefor a person who holds the Role, who then holds it no longer, andfalsefor a person who does not.
See: Roles and Grants, Grant access, WriteAppRoleInstance.assign, WriteAppRoleInstance.revoke.
ctx.principalId
Section titled “ctx.principalId”ctx.principalId: stringThe Identifier of the person the call runs as: the Identifier of their user Item. A handler saves it on an Item to record who made the Item, and compares it with a saved one to tell that person from every other. The callback of a Template Role returns a saved one.
The sample compares it with bookedBy, which the Mutation that made the booking saved.
const cancel = app.addMutation('cancel', async (input: IdInput, current, ctx) => { const item = await current.templates.booking.getItem(input.id); if (item === undefined) throw new Error('Booking not found'); if (item.properties.bookedBy !== ctx.principalId) { throw new Error('Only the person who booked can cancel'); } const transition = item.state?.transitions.cancel; if (transition === undefined) throw new Error('The booking is already canceled'); return await transition.execute({}); });Behavior
- In a call that runs as a person,
principalIdis the Identifier of that person. - A handler that compares it with a saved Identifier refuses every person but the one who made the Item.
- In an execution that runs as the system, as a Listener does, reading
principalIdthrows.
See: Auth contexts, Room bookings that refuse a clash, ReadRuntimeInstance.principalId.
ctx.sudo
Section titled “ctx.sudo”ctx.sudo( fn: (ctx: ReadRuntimeInstance) => Promise<unknown>,): Promise<unknown>Runs a function with a context that no Grant limits. Through that context the function reads the Items that the person the call runs as does not get. In a Mutation the context is a WriteRuntimeInstance, and the function writes through it too.
| Argument | Type | Description |
|---|---|---|
fn |
(ctx: ReadRuntimeInstance) => Promise<unknown> |
The function to run. It reaches the App through the context it gets, as sudo.apps.docs in the sample. |
Returns what fn returns. In the sample seen counts the visits through current, and all through the context fn gets.
const visits = app.addQuery('visits', async (_input: unknown, current, ctx) => { return await ctx.sudo(async (sudo) => { const seen = await current.templates.visit.selectItems().execute(); const all = await sudo.apps.docs.templates.visit.selectItems().execute(); const last = all.results.at(-1)?.properties.by; return { all: all.results.length, last, seen: seen.results.length }; }); });Behavior
- Through the context
fngets, a selector lists Items that no Grant gives the person. - Inside
fn,currentreads what the person reads, as it does outside. principalIdof the contextfngets is the Identifier of the person.- In a Mutation, what
fnwrites through that context is saved.

