- ipalEnvRules({ storage: true }): R2_PUBLIC_URL optional (media served by
the app); storage: { requirePublicUrl: true } requires it again.
- seo.uploadsCollection: upload collection of the meta image (default media).
- userMeetsRole(req, minimum): the plugin's admin/editor features (Site
Integrations, settings globals, test e-mail, stats, consent records, form
submissions) stay reachable when `access` is omitted — without a roles
field every logged-in account passes, as in Payload's default.
- IndexNow skip for seed writes is covered by test/seed.test.ts.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
153 lines
7.1 KiB
Markdown
153 lines
7.1 KiB
Markdown
# access
|
|
|
|
Role-based access control: hierarchy **admin > editor > user**.
|
|
The plugin injects a fixed `roles` field into the client's auth collection and provides
|
|
predicates and ready access functions (collection-level and field-level).
|
|
|
|
## Config (payload.config.ts)
|
|
|
|
```ts
|
|
ipalKit({
|
|
access: {
|
|
authCollection: 'users', // slug of your auth collection (required)
|
|
// defaultRole: 'user', // role of new accounts
|
|
// firstUserAdmin: true, // the account from "Create first user" → admin
|
|
// authAccess: { … }, // access of the injected auth collection (your keys win)
|
|
},
|
|
// globalOverrides: { … }, // access / admin / hooks / fields of the plugin's globals
|
|
})
|
|
```
|
|
|
|
| Option | Default | What it does |
|
|
|---|---|---|
|
|
| `access.authCollection` | — (required) | the auth collection that gets the `roles` field |
|
|
| `access.defaultRole` | `'user'` | role of a new account when an admin did not set one |
|
|
| `access.firstUserAdmin` | `true` | the account from the "Create first user" screen (`…/first-register` on an empty collection) gets `['admin']`, even when the form picked `user`; `false` = it gets what was submitted |
|
|
| `access.authAccess` | see the table below | `access` of the **injected** auth collection, merged over the default (your keys win) |
|
|
| `globalOverrides` | — | changes to the plugin's globals: an object merges `access` / `admin` key by key (your keys win) and appends `hooks` (yours run after the plugin's), other keys (label, fields, …) replace; a function `(global) => global` takes over the whole global |
|
|
|
|
The `roles` field (select hasMany, required, `saveToJWT`): readable without limits; only an admin
|
|
can set and change it — also on account creation, so an open sign-up cannot
|
|
push `roles: ['admin']` through. The first-user screen and the Local API (seed) run with
|
|
`overrideAccess`. An account without roles meets no threshold (not even `'user'`); `authenticated`
|
|
only checks that someone is logged in.
|
|
|
|
**Auth collection when the project has none.** The plugin adds a minimal collection (`auth: true`,
|
|
`admin.useAsTitle: 'email'`) with the slug `access.authCollection` (or `twoFactor.collectionSlug`,
|
|
`admin.user`, `'users'`). The project's own collection with that slug always wins whole.
|
|
|
|
## What the plugin locks down by itself
|
|
|
|
| Resource | Read | Write | How to change |
|
|
|---|---|---|---|
|
|
| auth collection **injected** by the plugin, with roles | admin: everyone; others: only themselves | `create` / `delete` / `unlock`: admin; `update`: admin or self | `access.authAccess`, or your own collection |
|
|
| Site Settings, Cookie Settings, Notifications | public | `editor+` | `globalOverrides` |
|
|
| Site Integrations | admin | admin | `globalOverrides` |
|
|
| Forms | public (Emails tab only `editor+`) | `editor+` | `forms.formOverrides.access`, the Emails field in `formOverrides.fields` |
|
|
| Form submissions | `editor+` | REST `create`: logged-in users; `delete`: admin | `forms.formSubmissionOverrides.access` |
|
|
|
|
```ts
|
|
import { authenticated } from '@intecion/ipal-kit'
|
|
ipalKit({
|
|
access: { authCollection: 'users', authAccess: { read: authenticated } }, // editors see the account list
|
|
globalOverrides: {
|
|
notifications: { access: { update: authenticated } }, // any logged-in account edits the texts
|
|
'site-settings': (g) => ({ ...g, admin: { ...g.admin, group: 'Configuration' } }),
|
|
},
|
|
})
|
|
```
|
|
|
|
The project's own auth collection keeps its own `access` — the plugin only adds the
|
|
`roles` field. Give it at least `update: adminOrSelf`, `delete: adminOnly`, `create: adminOnly`,
|
|
`unlock: adminOnly`: Payload's default lets any logged-in account change the email and password
|
|
of another account and unlock any account. A collection injected **without** `access` (no roles)
|
|
keeps Payload's default rules — without roles all accounts are equal.
|
|
|
|
> **Without `access` there is no `roles` field**, so `isAdmin` always returns `false`. The plugin's own
|
|
> staff features therefore check with `userMeetsRole(req, minimum)`: when the user's auth collection has no
|
|
> `roles` field, every logged-in account passes (as in Payload's default — without roles all accounts are
|
|
> equal). That keeps Site Integrations, the settings globals, `/api/ipal/test-email`, stats, consent records
|
|
> and form submissions reachable. To narrow them without roles: `globalOverrides` for the globals,
|
|
> `forms.formSubmissionOverrides` for submissions, or add `access` to get roles.
|
|
|
|
## Predicates (front end / hooks / access)
|
|
|
|
```ts
|
|
import { isAdmin, isEditor, hasMinimumRole, userMeetsRole } from '@intecion/ipal-kit'
|
|
|
|
isAdmin(user) // admin?
|
|
isEditor(user) // editor or admin (hierarchy)
|
|
hasMinimumRole(user, 'editor') // at least editor
|
|
userMeetsRole(req, 'admin') // admin — or any logged-in user when the auth collection has no roles field
|
|
```
|
|
|
|
They take a loosely typed user (like `req.user`) and read `roles` defensively.
|
|
|
|
## Ready access functions
|
|
|
|
### Collection-level (return boolean | Where)
|
|
|
|
```ts
|
|
import { adminOnly, adminOrEditor, adminOrSelf, requireRole, authenticated } from '@intecion/ipal-kit'
|
|
|
|
export const Articles = {
|
|
slug: 'articles',
|
|
access: {
|
|
read: authenticated,
|
|
create: adminOrEditor,
|
|
update: adminOrEditor,
|
|
delete: adminOnly,
|
|
},
|
|
// ...
|
|
}
|
|
|
|
// Users: everyone sees themselves, an admin sees everyone
|
|
access: { read: adminOrSelf }
|
|
|
|
// any minimum threshold
|
|
access: { update: requireRole('editor') }
|
|
```
|
|
|
|
### Field-level (return boolean)
|
|
|
|
```ts
|
|
import { adminOnlyField, adminOrEditorField, requireRoleField } from '@intecion/ipal-kit'
|
|
|
|
{
|
|
name: 'internalNote',
|
|
type: 'textarea',
|
|
access: { read: adminOnlyField, update: adminOnlyField },
|
|
}
|
|
```
|
|
|
|
## Hierarchy
|
|
|
|
```ts
|
|
import { ROLE_HIERARCHY } from '@intecion/ipal-kit'
|
|
// ['user', 'editor', 'admin'] — a higher role meets a lower role's requirement
|
|
```
|
|
|
|
## Building blocks: buildRolesField, injectRoles, applyGlobalOverrides
|
|
|
|
```ts
|
|
import { applyGlobalOverrides, buildRolesField, injectRoles } from '@intecion/ipal-kit'
|
|
|
|
buildRolesField('user', { firstUserAdmin: true }) // just the roles field — for your own collection
|
|
injectRoles(config, { authCollection: 'members' }) // add the field to a collection in a finished config
|
|
applyGlobalOverrides(globals, { notifications: { … } }) // the same logic as the globalOverrides option
|
|
```
|
|
|
|
Types: `AccessOption`, `Role`, `GlobalOverride`, `GlobalOverrides`, `IpalGlobalSlug`.
|
|
|
|
## Turn off, override, extend
|
|
|
|
- **Turn off:** leave out `access` — no `roles` field, the auth collection keeps Payload's rules
|
|
(or yours). `access.firstUserAdmin: false` stops giving admin to the first account.
|
|
- **Override:** `access.authAccess` (injected collection), your own auth collection (wins whole),
|
|
`globalOverrides` (globals), `forms.formOverrides` / `formSubmissionOverrides` (forms),
|
|
`defaultRole`. The role hierarchy (`ROLE_HIERARCHY`) is fixed.
|
|
- **Extend:** `requireRole` / `requireRoleField`, predicates, `buildRolesField`, `injectRoles`,
|
|
`applyGlobalOverrides` in your own collections and globals.
|
|
- **Check:** log in with a `user` account → `PATCH /api/globals/site-settings` = 403,
|
|
`DELETE /api/users/<admin id>` = 403; the account from "Create first user" has the `admin` role.
|