Roles
A role is what someone does in the business: sells, designs, runs the office. In
Spools a role decides what the CRM puts in front of them: what Home and the
sidebar show (and the tiles on Home, which follow the sidebar). With access
it also decides which objects they may open and change, and the server enforces that.
The two are different things. hide only tidies the sidebar: someone whose sidebar leaves
purchase orders out can still open them from search or a link. access is enforced on every
read and write: an object a role is denied is not there for its holders, by any path. Roles
keep kinds of record apart; to keep groups of people (branches, regions) to their own
records, use record scopes. Both can be on, and the stricter wins.
Defining roles#
Roles are in crm.config.json. Without the roles key there are none, and no Team page.
"roles": { "sales": { "label": "Salesperson", "hide": ["purchaseOrders", "payments"], "home": { "pipeline": { "owner": "owner" } } }, "designer": { "label": "Designer", "home": { "pipeline": { "owner": "designer" } } }, "office": { "label": "Office", "hide": ["deals"], "home": { "create": false, "pipeline": false } }, "management": { "label": "Management", "home": { "pipeline": { "team": true } } }}
| Key | Required | Description |
|---|---|---|
label |
yes | What the role is called on the Team page. |
hide |
no | Objects the sidebar leaves out for this role. |
home.create |
no | false leaves the New button (home.create) off this role's Home. |
home.pipeline |
no | This role's part of home.pipeline, or false to leave the pipeline off its Home. |
home.pipeline.owner |
no | Which of the pipeline's owner fields make a record this role's: one field or a list. Without it, every one of them does. |
home.pipeline.team |
no | true shows this role everyone's records, under "Team pipeline". |
access |
no | What the role may reach: { "deny": [objects], "readOnly": true or [objects] }. See Access. |
record.<object> |
no | { "open": [sections], "hide": [sections] }: how that object's record page starts for this role. See Record page sections. |
With a pipeline whose owner is ["owner", "designer"], a salesperson's pipeline is the
deals they own, a designer's is the deals they design, and management sees the team's.
The New button fills in the owner field of the role of whoever presses it.
Several roles, or none#
Someone can hold more than one role, and gets what any of them is given:
- Their pipeline is the records any of their roles makes theirs. A salesperson who also designs sees the deals they own and the deals they design, each counted once.
- If any of their roles is shown the team, they are.
- The sidebar leaves out only what every role they hold hides.
- The pipeline or the New button is left off Home only when every role they hold leaves it off.
Someone with no role sees Home and the sidebar as the config has them, as everyone did before roles.
Record page sections#
An object with sections can start differently for each
role: a designer opens Design and Site, the office opens Invoicing and never sees Design.
"roles": { "designer": { "label": "Designer", "record": { "jobs": { "open": ["design", "site"], "hide": ["invoicing"] } } }, "office": { "label": "Office", "record": { "jobs": { "open": ["invoicing"], "hide": ["design", "more"] } } }}
open: these sections start open for the role, and every other one collapsed. Someone holding several roles has open every section any of them opens. When none of their roles saysopenfor the object, each section starts as the config says.hide: these sections are left off the role's record page and new-record form. A section is left off only when every role someone holds hides it, so holding a role with norecordentry for the object, or one withouthide, keeps every section.- Someone with no role sees each section as the config starts it.
moreis the built-in "More details" section, and can be opened or hidden like any other.- The keys are checked: an object that isn't there, one without
sections, or a section it doesn't have, is refused. A module's sections can be named.
hide is presentation only. It tidies a page; it keeps nothing from anyone. The fields of a
hidden section are still in the record the browser is sent, and still the role's to read and
change in lists, filters, exports, imports, the API and the assistant. To keep an object from a
role, use access. A required field is never left off a new-record form, whatever
hide says: it is put first, with the title field.
Access#
"roles": { "office": { "label": "Office" }, "designer": { "label": "Designer", "access": { "deny": ["ledger"], "readOnly": ["jobs", "visits"] } }, "viewer": { "label": "Look only", "access": { "deny": ["ledger"], "readOnly": true } }},"access": { "allowedDomains": ["example.com"], "default": { "readOnly": true }}
deny: objects whose records the role's holders never read or change. To them the records are not found, everywhere: lists, record pages, search, relation pickers and titles, saved views, the timeline, tasks and notes, reports, exports, imports, files and actions. A relation to one shows no title, and the timeline of a record the role reads never names one: a change to a relation that leads to an object any role,access.defaultorassistant.denykeeps from someone is written "updated Client" (or "added Tags: 2"), for everyone, because a timeline line is kept as it was written.readOnly:truefor every object, or a list. Its holders read the records and change nothing of them: not the record, and not what hangs off it. No note, task or logged call is added to one, changed or deleted (a task assigned to them included), no email is sent to it, no action, workflow or sequence is started for it, and no file or folder in its folder, or in the folders inside it, is added, renamed, moved or deleted. Nor is a view everyone shares of that object; they make, change and delete views only they see. They still open, list, search and download it all, and star a favourite, which is their own. The page doesn't offer the controls, and the server refuses in words ("You can look at jobs but not change them.") before the app's hooks run or anything is sent."readOnly": true(or every object read-only or denied) is someone who may change no record, and so changes nothing that is the whole business's either, as with record scopes: no task or note on no record, nothing in the shared file library or any folder ("You can look at these files, but not change them."), no view everyone shares, nothing on the Marketing page, and no connection the team or a group shares. Their own things (their mailbox, their favourites, the views only they see) stay theirs.- A role without
accessallows everything.
Someone with several roles gets what any of them allows: an object is hidden from them only when every role they hold denies it, and read-only only when none of their roles may change it. A designer who is also in the office edits everything.
Someone who holds no role reaches everything, as before roles, unless access.default
says otherwise. With "default": { "readOnly": true } a new teammate looks until an admin gives
them a role. A config with no roles at all and an access.default applies it to everyone.
Timeline lines written before an object was denied may still name its records. After adding a
deny, an access.default or an assistant.deny to a Spool with history, run
bunx spools timeline redact: it rewrites those lines as a line written today would say them.
It finds a change line by the field it is about, which every change line records from this
version on, and an older line by the field's label as it is now. It can't fix an older line
written under a label the field no longer has, nor a title in words that aren't a change line
(a note, an email, a line your own code or an automation wrote): look for those yourself.
The Team page shows what each role may do with each object. The
security page says exactly what role access keeps apart and what it
doesn't. Notably, the assistant's tool calls can't say which person they are for, so roles
don't narrow the assistant: assistant.deny
(configuration) keeps objects from it.
Where roles come from#
What a role means is always in the config. Who holds it comes from one of three places, and a Spool uses one:
| Source | Chosen by | Roles are set |
|---|---|---|
| The CRM itself | Nothing: it is the default | On the Team page, by the people access.admins names |
| Your sign-in provider | The auth adapter, e.g. clerk({ roles: 'metadata' }) |
At the provider, and on the Team page when the provider takes changes (Clerk's public metadata) |
| Your own code | roles in src/app/extensions.ts |
Wherever your code reads them from |
Your own code wins over the provider, and the provider over the CRM. Whichever answers,
a role counts only when roles in the config has it, and an answer from outside the CRM
is kept for a minute per person.
The CRM itself: the Team page#
The Team page lists everyone who has signed in, with a box for each role. Ticking a box saves at once, and the person's Home and sidebar follow on their next page.
Only the people access.admins names see the Team page or can change a role:
"access": { "allowedDomains": ["example.com"], "admins": ["pat@example.com"]}
An admin must also be let in by the access list. The server checks the list on every call: a page that hid nothing would still be refused.
Roles are kept by email, so someone can be given theirs before they first sign in: add their address at the bottom of the Team page. It must be an address the access list lets in. Their Home is ready the first time they sign in with it.
Clerk#
// src/spool.server.tsauth: clerk({ roles: 'metadata' }),
roles: 'metadata'reads the user's public metadata: a list,{ "roles": ["designer", "management"] }, or one,{ "role": "designer" }. Set it on the user in Clerk's dashboard, or with the Backend API. Public metadata is written only with your secret key, never by the person themselves. An invitation can carry it, so someone has their role before they first sign in.roles: { organization: 'org_...' }reads their role in that Clerk organization, without theorg:prefix:org:designeris the roledesigner. Clerk gives a person one role in an organization, so nobody holds two this way. A role in any other organization counts for nothing: unless you have turned it off, anyone can create an organization and be its admin.
With roles: 'metadata' the Team page sets roles too. An admin ticks a box, and the
CRM writes the list to that person's public metadata through Clerk's Backend API, with
the secret key the Spool already has. Whatever else their metadata holds is kept,
including roles that are not this CRM's, and the change shows on their next page.
Someone has to have signed in once: the roles go on an account, and until there is one
the page says so. (An invitation can carry the metadata before that.)
With a role in an organization, the Team page lists everyone and what they hold and changes nothing: a membership is changed in Clerk, with its members.
Your own code#
// src/app/extensions.tsroles: { held: async (user) => rolesFromPayroll(user.email), // ["designer"]},
held is given the person's id and email and returns the keys of the roles they hold.
Use it when roles are kept in a directory, a payroll system, or a table of your own.
A source of your own, in an adapter#
Roles (@swirls/spools/server/roles) is the service all three sources are. An auth
adapter supplies one as roles on what it returns: held(user) says what a person
holds, managedIn names where roles are kept, and an optional set(user, roles, by)
lets the Team page write to it. Leave set out and the page only shows.
Adding roles to a Spool made before them#
Add
rolesandaccess.adminstocrm.config.json.Add the page's route file,
src/routes/_crm/team.tsx:tsimport { createFileRoute } from '@tanstack/react-router'import { teamPage } from '@swirls/spools/pages/team'export const Route = createFileRoute('/_crm/team')(teamPage)Run
bun run crm:gen, thenbun run db:pushor plan a migration: there is one new table,team_roles.Run
bun run buildso the new route is typed.
"team" can no longer be the key of an object: it is the page's address.