Skip to content

Workspaces and access

A Workspace is the tenant and execution boundary: every Item, Event and Metric row belongs to exactly one, and a PlatformInstance is bound to one for its whole life. An auth context names the principal the instance acts as. Roles group principals, and Grants link a Role to what it may reach. The Runtime checks Grants at two points: when a principal calls an operation and when a principal reads or writes a Property.

examples/concepts/workspaces-and-access.ts
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
import type { WriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
interface RateInput {
readonly id: string;
readonly rating: number;
}
export const reviewsApp: AppSpec = (app) => {
app.configure('reviews');
const member = app.addRole('member', { label: 'Member' });
const manager = app.addRole('manager', { label: 'Manager' });
manager.extend(member);
const review = app.addTemplate('review');
const notes = review.addProperty('notes', 'string');
const rating = review.addProperty('rating', 'integer');
const subjectId = review.addProperty('subjectId', 'id');
review.addGrant(manager, [notes, rating, subjectId]);
const subject = review.addRole('subject', async (item) => {
const id = item.properties.subjectId;
return typeof id === 'string' ? [id] : [];
});
review.addGrant(subject, [rating]);
const auditEntry = app.addTemplate('auditEntry');
auditEntry.addProperty('action', 'string');
auditEntry.addProperty('reviewId', 'id');
const rate = app.addMutation('rate', async (input: RateInput, current, ctx) => {
const item = await current.templates.review.getItem(input.id);
if (item === undefined) throw new Error('Review not found');
await item.updateProperties({ rating: input.rating });
await ctx.sudo(async (system: WriteRuntimeInstance) => {
const workspace = await system.getWorkspaceItem();
await workspace.createChild(system.apps.reviews.templates.auditEntry, {
properties: { action: 'rate', reviewId: input.id },
});
});
});
rate.input.addProperty('id', 'id').mandatory();
rate.input.addProperty('rating', 'integer').mandatory();
rate.output.void();
rate.addGrant(manager);
};

A Workspace is created for one Definition and holds one Item tree rooted at its Workspace Item. Nothing crosses the boundary: a selector returns Items of this Workspace, a Relation links Items of this Workspace, and a Metric row carries this Workspace’s Identifier. The reactor processes an Event in the Workspace that appended it. A privileged context widens what a principal may do inside the Workspace; it never widens which Workspace.

The Workspace Item is an ordinary Item whose Template the root App declares through getWorkspaceTemplate(). Put per-tenant configuration there, such as a timezone or an integration id, and reach it with ctx.getWorkspaceItem(). App-level Events are published against it, and App Hooks receive it when a Workspace is created or seeded.

AlpyneAuthContext has four variants, each carrying the workspaceId it is valid for.

  • principal is a person or client acting as itself. It carries principalId, the rolePrns assigned to that principal and a sessionId. Grants are checked against those Roles.
  • masquerade is one principal acting as another. It carries the puppet’s principalId and Roles plus underlyingPrincipalId for the operator. Access is evaluated as the puppet; the Event history keeps both identities.
  • sudo is Runtime authority on behalf of a principal. It carries underlyingPrincipalId and a tag naming where it was minted. Grants are not consulted.
  • system is Runtime authority with no principal behind it, tagged for tracing. The reactor, the projector and the test harness run as system.

ctx.principalId resolves to the acting principal for principal and masquerade and to the underlying principal for sudo. Handlers rarely branch on the context type; they declare Grants and let the Runtime enforce them.

A Role is a named grantee. An App Role, declared with app.addRole(key, options), is assigned to principals and appears in their rolePrns. extend makes one App Role act as another, so manager above passes every check member passes. A Template Role, declared with template.addRole(key, callback), is computed per Item. The callback receives the Item and returns the access ids that hold the Role for it: principal Identifiers or App Role PRNs. In the example, the subject of a review holds the subject Role on that review alone.

A Grant links a Role to a resource. mutation.addGrant(role) and query.addGrant(role) grant the right to call an operation. template.addGrant(role, properties) grants access to the listed Properties on Items of that Template. transition.addGrant(role) grants the right to execute a transition, and metric.addGrant(role) the right to query a Metric. Grants only add. When several apply, the principal has the union of what they permit, and a Template with no Grant for a principal’s Roles exposes nothing to that principal.

The two checks are independent. Holding a Grant on rate lets a manager call it. Inside the handler, getItem returns the review only when the manager’s Roles hold a Grant on that Template, and properties contains only the Properties those Grants list. A member with no Grant on review receives undefined from the same getItem call, and the Runtime rejects updateProperties on a Property outside the Grant. Runtimes apply the check in two passes: a coarse pass that decides which Items a selector returns, then a fine pass that filters each Item’s Properties. A visible Item does not mean every Property on it is visible.

Design Grants from the data outward. Decide which Properties each Role may see on each Template, then grant operations to the Roles that need them. An operation Grant without a Template Grant yields a handler that runs and finds nothing.

ctx.sudo(fn) runs fn with a sudo context derived from the current one, in the same execution and transaction, and returns its result. Inside, Grants are not consulted, so the handler reads or writes Items the calling principal cannot. rate does this when it appends an audit entry no member may touch. The Event history keeps the underlying principal, and the Workspace does not change. Calling sudo from a sudo or system context returns the same authority. A read execution stays read-only under sudo.

Use sudo at the narrowest scope that works: authorize the caller first with assertRole or checkRole, then elevate for the specific reads or writes the principal cannot perform. Prefer a Template Grant when the access is a standing rule, and sudo when it is a consequence of an operation the principal is already allowed to perform.

  • A Workspace is not a copy of the application. It is a data boundary inside one deployed Definition.
  • A Role is not a permission. It is a grantee; Grants say what it may reach.
  • Operation access is not data access. Each is granted and checked separately.
  • sudo and system do not cross Workspaces, and a read context stays read-only under either.
  • Grant access to declare Roles and Grants and test them with principal contexts.
  • Application model for how a Workspace relates to a Definition and a Runtime.
  • Authorization for the normative rules on Grants and privileged contexts.
  • Data model for how filtered Properties appear on an Item.
  • Glossary for the canonical definition of each term.