Roles and patterns
Roles a user is granted, and whole schemas for shapes that come up often. Part of the schema language.
Roles and grants (RBAC)#
A role is declared in the schema and granted to users as data, so changing who holds one — who runs the app, who edits a team, who owns a deal — is a write, not a schema push, and no email address goes in a schema.
An app names its roles what it likes. october reserves no word inside
a tenant's schema, so :operator below is simply the october waitlist's
name for the people who keep the list.
- Lang
- EDN
{:roles {:operator {:doc "Runs the waitlist"} :owner {:scope :teams :doc "Owns a team" :grant [(has-role? ?user :operator)]} :editor {:scope :teams :grant [(or-join [?scope ?user] (has-role-on? ?user :owner ?scope) (has-role? ?user :operator))]}} :entities {...}}
| Key | Meaning |
|---|---|
:scope |
Absent: the role is tenant-wide. An app entity (:teams): the role is held on one entity of it (editor of a team). |
:grant |
The rule saying who may grant and revoke the role: ?user is the granter, ?scope (scoped roles only) the entity the role is held on. Absent: only the tenant's admins (october roles, the admin API). true: anyone signed in. |
:doc |
A description, shown by october roles. |
:retired? |
true: the role's grants are kept, but no new grants are made and no rule may test it. |
Rules test roles with two built-ins, plain Datalog over the grants:
- Lang
- EDN
[(has-role? ?user :operator)] ; tenant-wide [[?e :docs/team ?t] (has-role-on? ?user :editor ?t)] ; held on the doc's team
The role is a keyword literal naming a declared role, tenant-wide for
has-role? and scoped for has-role-on?, or the plan refuses it. A rule
that calls either mentions ?user, so it fails closed for signed-out
callers (inside an or, the other branches still serve them: see
Published or mine). Roles work inside or, not
and named rules like any rule call, and a write's check watches the
grants it read: a grant revoked between the check and the commit makes
the write retry, and then it is refused.
Granting and revoking#
- The tenant's admins grant any role, whatever its
:grantrule:october roles SLUG grant operator ada@example.com(see cli-commands.md). That is how the first grant of every role is made. - Through the API,
grantRole(role: OPERATOR, email: "...")orgrantRole(role: EDITOR, userId: ..., scopeId: <team id>)andrevokeRole(id: ...), checked against the role's:grantrule (see api-mutations.md). Granting what is already granted returns that grant. Grants are attributed writes like any other:grantedByis the granter (null for an admin's), and the transaction is in history. - By email, a grant is held by whoever signs in with that address, from their first sign-in on (nothing to accept or claim). It shows its email, never a user, so a granter doesn't learn whether the address has an account. In demo mode anyone can sign in as any email, so grants by email are refused there (grants to a user id still work), and demo mode can't be turned on while a tenant has any.
- By id, the granter must be able to see the user (users are closed by default: see Who may see users). The holder of a grant by id may revoke it themself (leave the role).
- Deleting a scope entity deletes the grants held on it.
Who sees grants: each user their own (viewer { roles }), and
whoever may grant a role (its :grant rule holds, on that scope) that
role's grants (roleGrants(role:, scopeId:)). viewer { can { grantRole(role:, scopeId:) } } says whether you may grant one. Rules can't read grants
directly ($roleGrants is a system entity), only through the two
built-ins.
Changing roles#
Plan lists added, removed and changed roles and :grant rules under
Permissions, and warns about a role only the tenant's admins can grant
(often what you want for the role that runs the app). Removing a role that has grants is
destructive and refused, as removing an enum value is: revoke the grants,
or keep the role with :retired? true. Making a tenant-wide role scoped,
or scoping it to another entity, is refused while it has grants that
don't fit.
Roles: patterns#
- Lang
- EDN
{:roles {:org-admin {:doc "Administers the organisation"} :owner {:scope :teams :grant [(has-role? ?user :org-admin)]} :editor {:scope :teams :grant [(or-join [?scope ?user] (has-role-on? ?user :owner ?scope) (has-role? ?user :org-admin))]}} :entities {:teams {:attrs {:name {:type :string :min 1 :max 100}} :permissions {:view [(authenticated? ?user)] :create [(has-role? ?user :org-admin)] :update [(or-join [?e ?user] (has-role-on? ?user :owner ?e) (has-role? ?user :org-admin))] :delete [(has-role? ?user :org-admin)]}} :docs {:attrs {:title {:type :string :min 1 :max 200} :team {:type :ref :to :teams :required? true} :author {:type :ref :to :$users :required? true :default :auth/user}} :permissions {:view [[?e :docs/team ?t] (or-join [?t ?user] (has-role-on? ?user :editor ?t) (has-role-on? ?user :owner ?t) (has-role? ?user :org-admin))] :create [(authenticated? ?user) [?e :docs/author ?user] [?e :docs/team ?t] (or-join [?t ?user] (has-role-on? ?user :editor ?t) (has-role-on? ?user :owner ?t))] :update [[?e :docs/team ?t] (or-join [?t ?user] (has-role-on? ?user :editor ?t) (has-role-on? ?user :owner ?t))] :delete [[?e :docs/team ?t] (has-role-on? ?user :owner ?t)]}}}}
- Org admin: tenant-wide, granted by the tenant's admins only (no
:grant). Creates teams and makes their owners. - Owner of a team: granted by an org admin; edits the team, grants and revokes its editors, deletes its docs.
- Editor of a team: granted by the team's owner (or an org admin); reads and writes the team's docs, nothing on other teams.
Patterns#
An :update rule without before is checked on the entity before
the change and again after it, so a rule over the state is also a
rule over every move between states: a move is allowed only when both
ends pass. With before (see
Field :update rules) a
rule can tell the two ends apart. These were run against a live
server, as the author, as another user and anonymously.
A state that freezes the entity#
A closed question can't be edited or reopened, and its author may close it:
- Lang
- EDN
{:rules [[(author? ?e ?u) [?e :questions/author ?u]]] :entities {:questions {:attrs {:title {:type :string :min 1 :max 200} :closed {:type :boolean :optional? true} :author {:type :ref :to :$users :required? true :default :auth/user}} :permissions {:view true :create [(authenticated? ?user) (author? ?e ?user) (not [?e :questions/closed true])] :update [(author? ?e ?user) (before (not [?e :questions/closed true]))]}}}}
(before (not [?e :questions/closed true])) reads the question as it
was before the write: every change, closing included, needs it open
then. Once closed, editing and reopening both fail. The create rule
keeps questions from being created closed.
- Lang
- Text
alice updateQuestion(title: "How, exactly?") ok (open) alice updateQuestion(closed: true) ok alice updateQuestion(title: "x") FORBIDDEN (closed before) alice updateQuestion(closed: false) FORBIDDEN (closed before) bob updateQuestion(title: "mine now") FORBIDDEN (not the author) anon updateQuestion(closed: false) UNAUTHENTICATED
To keep the rest editable and freeze only the flag, put the rule on the
attribute instead: :closed {:type :boolean :optional? true :update [(before (not [?e :questions/closed true]))]}.
Soft delete, with no undelete#
A deleted flag that hides the row from everyone, for good:
- Lang
- EDN
{:rules [[(author? ?e ?u) [?e :notes/author ?u]]] :entities {:notes {:attrs {:text {:type :string :min 1 :max 500} :deleted {:type :boolean :optional? true} :author {:type :ref :to :$users :required? true :default :auth/user}} :permissions {:view [(not [?e :notes/deleted true])] :create [(authenticated? ?user) (author? ?e ?user) (not [?e :notes/deleted true])] :update [(author? ?e ?user)]}}}}
The view rule hides deleted rows from lists, by-id roots and nested
fields. It is also what refuses every later change: an update first
looks the entity up as the caller sees it before the change, and a
deleted note is not found. The :update rule must not test
deleted without before: it is checked after the change too, so it
would refuse the soft delete itself.
- Lang
- Text
bob updateNote(deleted: true) FORBIDDEN (not the author) alice updateNote(deleted: true) ok alice updateNote(deleted: false) NOT_FOUND alice updateNote(text: "x") NOT_FOUND alice note(id:) null anon notes(first: 5) the other notes only
The rows stay in the database. When you don't need them, delete for
real: delete<T> (a :delete rule), or delete<Plural>(ids:) for many
at once, all or nothing (see api.md).
Children of a parent that can be hidden again#
Answers on questions that may go back to draft. Drafts can't be answered, and a question can't return to draft once it has answers:
- Lang
- EDN
{:rules [[(q-author? ?q ?u) [?q :questions/author ?u]] [(a-author? ?a ?u) [?a :answers/author ?u]]] :entities {:questions {:attrs {:title {:type :string :min 1 :max 200} :published {:type :boolean} :author {:type :ref :to :$users :required? true :default :auth/user :reverse :questions}} :permissions {:view [(or-join [?e ?user] [?e :questions/published true] (q-author? ?e ?user))] :create [(authenticated? ?user) (q-author? ?e ?user)] :update [(q-author? ?e ?user) (or-join [?e] [?e :questions/published true] (not [_ :answers/question ?e]))]}} :answers {:attrs {:body {:type :string :min 1 :max 2000} :question {:type :ref :to :questions :required? true :reverse :answers} :author {:type :ref :to :$users :required? true :default :auth/user :reverse :answers}} :permissions {:view [(or-join [?e ?user] (and [?e :answers/question ?q] [?q :questions/published true]) (a-author? ?e ?user))] :create [(authenticated? ?user) (a-author? ?e ?user) [?e :answers/question ?q] [?q :questions/published true]] :update [(a-author? ?e ?user)]}}}}
The question's :update holds while it is published, or while nothing
answers it. Publishing a draft passes (no answers before, published
after); editing a published question passes; unpublishing one with
answers fails on the state after the change. The answer's view has a
"mine" branch, so if a question is hidden anyway (by a schema that
allowed it, or data from before this rule), each answer's author still
sees their own answer, while everyone else doesn't.
- Lang
- Text
bob createAnswer on a draft NOT_FOUND alice updateQuestion(title:) on her draft ok alice updateQuestion(published: true) ok bob createAnswer ok alice updateQuestion(published: false) FORBIDDEN (answered) alice updateQuestion(title:) ok (still published) alice unpublish a question with no answers ok
With a looser :update ([(q-author? ?e ?user)]) the unpublish goes
through; then me { answers } still gives Bob his answer (its
question is null), and Carol and anonymous callers get none.
Teams: membership by consent#
People join a team, and team members see the team's records and each other's display names. The membership is the permission, so who may create one is the whole security of the app: a membership is created only by the person joining, either the team's owner or someone a member invited.
- Lang
- EDN
{:rules [[(member? ?t ?u) [?m :memberships/team ?t] [?m :memberships/user ?u]] [(teammate? ?a ?b) [?m :memberships/user ?a] [?m :memberships/team ?t] (member? ?t ?b)] [(invited? ?t ?u) [?i :invites/team ?t] [?i :invites/invitee ?u]]] :entities {:$users {:attrs {:display-name {:type :string :optional? true :max 40 :view [(or (self? ?e ?user) (teammate? ?e ?user))]} ;; set by the user, so members can find them to invite them :findable {:type :boolean :optional? true}} :permissions {:view [(or-join [?e ?user] (self? ?e ?user) (teammate? ?e ?user) (and (authenticated? ?user) [?e :$users/findable true]))]}} :teams {:attrs {:name {:type :string :min 1 :max 100} :owner {:type :ref :to :$users :required? true :default :auth/user}} :permissions {:view [(or [?e :teams/owner ?user] (member? ?e ?user) (invited? ?e ?user))] :create [(authenticated? ?user) [?e :teams/owner ?user]] :update [[?e :teams/owner ?user]]}} :invites {:attrs {:team {:type :ref :to :teams :required? true} :invitee {:type :ref :to :$users :required? true :reverse :invites} :inviter {:type :ref :to :$users :required? true :default :auth/user :reverse false}} :permissions {:view [(or-join [?e ?user] [?e :invites/invitee ?user] (and [?e :invites/team ?t] (member? ?t ?user)))] :create [(authenticated? ?user) [?e :invites/inviter ?user] [?e :invites/team ?t] (member? ?t ?user)] :delete [(or [?e :invites/invitee ?user] [?e :invites/inviter ?user])]}} :memberships {:attrs {:team {:type :ref :to :teams :required? true :reverse :members} :user {:type :ref :to :$users :required? true :default :auth/user}} :permissions {:view [(or-join [?e ?user] [?e :memberships/user ?user] (and [?e :memberships/team ?t] (member? ?t ?user)))] :create [(authenticated? ?user) [?e :memberships/user ?user] [?e :memberships/team ?t] (or [?t :teams/owner ?user] (invited? ?t ?user))] :delete [[?e :memberships/user ?user]]}} :deals {:attrs {:title {:type :string :min 1 :max 200} :team {:type :ref :to :teams :required? true}} :permissions {:view [[?e :deals/team ?t] (member? ?t ?user)] :create [(authenticated? ?user) [?e :deals/team ?t] (member? ?t ?user)] :update [[?e :deals/team ?t] (member? ?t ?user)]}}}}
- An invite is created by a member of the team, naming the invitee. The invitee (and the team's members) can see it, and the invitee can see the team it is for.
- A membership names the caller (
:userdefaults to the caller and is pinned to them;[?e :memberships/user ?user]says so again), and the team's owner or an invite for the caller to that team must exist. Nobody can put someone else on a team. - Records (
deals) are visible to members of their team; display names to the user and their teammates. - Users are closed by default, so a member can name someone in an invite
only once they can see them: a teammate, or a user who made themself
findable(updateUser, which only they can do). Their display name stays hidden until they join.
Why consent. The tempting version lets any member add any user id:
:create [[?e :memberships/team ?t] (member? ?t ?user)]. Then every rule
that trusts membership trusts the attacker: Mallory creates a team, adds
Alice to it, and a rule like "teammates see each other's display names"
shows her Alice's name, or anything else gated on sharing a team. A
review of a CRM built on october harvested users' display names exactly
so, by force-adding them to its own team. With an invite that the
invitee accepts, a membership is a statement by the member, and rules
may rely on it.
- Lang
- Text
alice createMembership(Sales) ok (the owner joins) alice createInvite(Sales, bob) NOT_FOUND (bob is a stranger: users are closed) bob updateUser(bob, findable: true) ok (bob lets people find him) alice createMembership(Sales, userId: bob) VALIDATION_FAILED (not the caller: userId is pinned) bob createMembership(Sales) NOT_FOUND (no invite: the team is hidden) mallory createInvite(Sales, mallory) NOT_FOUND (not a member) alice user(bob) { displayName } null alice createInvite(Sales, bob) ok bob me { invites { team { name } } } Sales bob createMembership(Sales) ok (accepts) alice user(bob) { displayName } "Bob" bob deals(first: 5) the team's deals mallory createMembership(Mine, userId: alice) VALIDATION_FAILED (pinned: anyone but the caller) mallory createInvite(Mine, bob) ok, but it means nothing until Bob accepts mallory user(bob) { displayName } null mallory users(first: 50) mallory and bob (findable), nobody else anon teams(first: 5) none
An invitee can accept an invite more than once; that makes duplicate
memberships, which change nothing. Delete the invite after accepting if
you want it gone (deleteInvite is allowed to the invitee and the
inviter). Leaving a team is deleteMembership, by the member.