Skip to content

The identity App

Every App starts with people. Alpyne adds an App named identity to every application, so the people who use your App, and the person behind each call, are there before you declare a Template. You import nothing.

This page shows what you do with it, in a small notes App. Members post notes, and an editor reads them all.

notes.ts imports two types and no App. The Builder still puts two Apps in its Definition: notes and identity. A person is an Item of the user Template of the identity App, and the script adds three:

run.ts
const people = await workspace.write(async (ctx) => {
const root = await ctx.getWorkspaceItem();
const user = ctx.apps.identity.templates.user;
const roles = ctx.apps.notes.roles;
const templatePrn = 'prn::identity:template/user';
const alice = await user.createItem(root, { properties: { displayName: 'Alice' } });
const bob = await user.createItem(root, { properties: { displayName: 'Bob' } });
const carol = await user.createItem(root, { properties: { displayName: 'Carol' } });
await roles.member.assign({ id: alice.id, templatePrn });
await roles.member.assign({ id: bob.id, templatePrn });
await roles.editor.assign({ id: carol.id, templatePrn });
console.log(`Alice: an Item of ${alice.templatePrn}`);
return { alice: alice.id, bob: bob.id, carol: carol.id };
});

The script reaches the App as ctx.apps.identity, and a handler reaches it the same way. displayName holds the name of a person. assign gives a person a Role of your App: it takes the Identifier of the person and the PRN of the user Template. From then on, the person holds that Role in every call they make.

Notes App is running in Workspace fd2eae0b-4d5a-54fa-b514-f022447b1d8a
Alice: an Item of prn::identity:template/user

In a call a person makes, ctx.principalId is the Identifier of that person’s user Item. post saves it on the note, in a Property of the type id:

notes.ts
const post = app.addMutation('post', async (input: PostInput, current, ctx) => {
const workspace = await ctx.getWorkspaceItem();
return await workspace.createChild(current.templates.note, {
properties: { postedBy: ctx.principalId, text: input.text },
});
});

Alice and Bob are members, and each posts a note. Where a call runs as the system, as the lines of the script above do, there is no person behind it, and reading ctx.principalId throws.

As Alice: posted "Fix the door"
As Bob: posted "Order more chairs"

A Template Role has a callback that names who holds it for one Item. A callback that returns a saved Identifier gives the Role to that person, for that Item alone.

notes.ts
const poster = note.addRole('poster', async (item) => {
const postedBy = item.properties.postedBy;
return typeof postedBy === 'string' ? [postedBy] : [];
});
const editors = note.addRole('editors', async () => [editor.prn]);
note.addGrant(poster, Object.values(note.properties));
note.addGrant(editors, Object.values(note.properties));

poster returns the Identifier that post saved, and editors returns the editor Role. Each member finds the note they posted, and Carol, the editor, finds both:

As Alice: finds "Fix the door"
As Bob: finds "Order more chairs"
As Carol: finds "Fix the door" and "Order more chairs"

A person does not find a user Item through a call of their own, not even their own Item:

As Carol: Alice's Item is not there, her own is not there

A handler that needs a person reads the Item in ctx.sudo, where no Grant limits what it finds:

notes.ts
const whoPosted = app.addQuery('whoPosted', async (input: IdInput, current, ctx) => {
const item = await current.templates.note.getItem(input.id);
if (item === undefined) throw new Error('Note not found');
const person = await ctx.sudo(async (sudo: ReadRuntimeInstance) => {
const user = sudo.apps.identity.templates.user;
return await user.getItem(item.properties.postedBy);
});
return person?.properties.displayName;
});

whoPosted reads the note as the caller first, so it answers only a person who finds that note. Then it reads one user Item through sudo.apps.identity, and returns the name and not the Item. Carol finds the note and gets the name. Bob is a member, but the note is not his, and the App refuses him.

As Carol: the note was posted by Alice
As Bob: Refused. Note not found

These are the parts of the identity App that the notes App and its script use.

What Where What it is for
The App ctx.apps.identity It is in every application, under this name. Its PRN is prn::identity
The user Template ctx.apps.identity.templates.user A person is one of its Items. Its PRN, prn::identity:template/user, is the templatePrn that assign takes
The Identifier of a user Item ctx.principalId, for the person who made the call A handler saves it on an Item, a Template Role returns it, and assign takes it
The name of a person properties.displayName of a user Item A handler reads it in ctx.sudo

An App that keeps more about a person declares a Template of its own. The Sales pipeline keeps one rep for each person who joins, under a key that is the Identifier of that person.