Mainstay GitHub
Editing

Capabilities derived from your types

The admin has accounts of its own, in a users table of its own behind a guard of its own, so a site's members and the people editing it never share a table or a login. What each account may do is built from capabilities: they are strings, a role is a named list of them, and a user holds one role plus exceptions of their own.

Capabilities are computed, never stored

Each registered type has a set of capabilities named after it, and each capability also has a form covering every type of that kind, the ones deployed later included. For the Article type:

What Article derives

Write drafts, of its own edit_articles
Change one that is live edit_published_articles
Change someone else's edit_others_articles
Put a draft live publish_articles
Move its own to the trash delete_articles
Trash someone else's delete_others_articles

edit_entries is the same capability for every entry type, so a role holding it edits a type that does not exist yet. A global has edit and publish, a taxonomy manage, and beside them are the media library's upload_media and edit_others_media, manage_users for accounts, manage_connectors for the AI connectors, and use_ and a plugin's handle for each plugin with screens of its own.

None of these is stored. A role in mainstay_roles is a name and a JSON list, because roles are the part an administrator edits; the vocabulary they draw from is worked out from the registered types. Storing it would mean seeding it and then proving it still agrees with the classes, the drift problem the schema check already spends a command on. A type that is deployed adds its capabilities everywhere at once, and a role naming one that no longer exists is inert rather than broken. Mainstay ships two roles, because a package cannot know a site's types: administrator, holding *, and editor, holding the every-type forms. The rest are the site's to make, in a grid of the capabilities its types derive.

An entry's question, mapped to capabilities

Asking whether someone may change one particular entry is asking several things at once: whether they edit the type at all, whether the entry is live, whether it is theirs. In Laravel a policy is that mapping, so one policy for each kind of content does it:

packages/cms/src/Policies/EntryPolicy.php
public function update(?User $user, Entry $entry): Response
{
    return $this->needs($user, $entry::class, 'edit', ...(isset($entry->id) ? ['edit_published'] : []), ...$this->others($user, $entry, 'edit_others'));
}

An entry with an id is live, so changing it needs edit_published as well. One that is not the user's needs edit_others, and one with no owner at all counts as someone else's, which is the answer that fails closed. Each check names the type's own capability and the every-type form together, and holding either is enough.

One role, and the exceptions

A user holds one role, and grants and denials of their own for the exception, where a denial wins. Several roles per user would need a rule for combining them and another for their conflicts; one role with exceptions needs neither.

Nobody hands out a capability they do not hold. A role edited, an account invited or a token made can only carry what its maker could do themselves.

Everything runs through Laravel's Gate, so a site that writes its own policy for one of its types does it the way Laravel documents, with no authorisation API of Mainstay's to learn.

Keep reading

All articles