Mainstay GitHub
All docs

Accounts, roles and capabilities

Who may do what, as capabilities derived from your types and gathered into roles.

Access is built from capabilities, which are strings, never stored, computed from the types you register, and from roles, each a name holding some of them. The query layer asks Laravel's Gate on every call, and the admin, the API and your code are answered alike, except where code passes overrideAccess: true and skips the Gate.

The capabilities a type derives

Each type gets a set named after it, and each capability has a form covering every type of its kind, those you add later included.

For an Article type

Write and change its drafts edit_articles
Change what is live edit_published_articles
Change someone else's edit_others_articles
Put it live publish_articles
Move it to the trash delete_articles
Trash someone else's delete_others_articles

edit_entries and the rest cover every entry type. A global has edit_ and publish_ its handle, covered by edit_globals and publish_globals; a taxonomy has manage_tags, covered by manage_terms. Beside them stand upload_media, edit_others_media, manage_users, manage_connectors, and use_ and a plugin's handle for each plugin with screens.

Roles

Two roles come with the package. administrator holds *, every capability. editor holds the forms covering every type, the media library's two, and nothing over accounts or connectors. The rest are yours to make in the admin's Roles, a grid of every capability the types derive. A role holding a capability no type derives any longer holds nothing by it.

Nobody hands out more than they hold

An account can give a role, or make one, only of capabilities it holds itself, and the installation is never left without an account that can manage accounts. The same holds for API tokens: a token holds no more than its account, and loses what its account loses.

From code

use Mainstay\Auth\Role;

$editor = Role::query()->where('name', 'editor')->value('id');

Mainstay::accounts()->create('Ada', '[email protected]', role: $editor, overrideAccess: true);

The account has no password until its owner chooses one from the link invite() mails it; the admin's Users screen mails it as it makes the account. saveRole() makes or changes a role, and assign() gives an entry to another author:

Mainstay::assign(Article::class, 5, owner: 3);

A policy of your own binds API tokens only if it asks $user->holds(...), as Mainstay's do, rather than reading the role.