Files
ipal-kit/docs/access.md
T
Radosław SmolińskiandClaude Opus 5.5 b61b753ec2 Merge main (security-perf + SEO) into docs; finish English docs and agent guide
- Resolve conflicts in analytics, consent, content, i18n, mcp and stats,
  keeping the docs fixes and the new code behaviour, verified against src
- Translate the remaining 14 module pages, ipal-seo-check skill and script
- Integrate the security-perf and SEO agent-guide text: getRobots,
  isPathVisible, Search & AI (DNS verification first, meta fields optional),
  sitemap index/cache/503, consentMode basic/advanced, policyVersion,
  globalOverrides, revalidatePath / buildGlobalRevalidateHook, authAccess,
  firstUserAdmin, allowCollectionMismatch, unknown-IP rate limit; stale
  claims fixed across AGENT.md, skills, frontend, developer guide, vibe
- docsConsistency: pending list removed (all docs English); relative links
  now also check #anchors

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:59:52 +02:00

149 lines
6.7 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 Site
> Integrations global and the `/api/ipal/test-email` endpoint then work only through the Local API
> (`overrideAccess`). The seed / first user must have `roles: ['admin']`.
## Predicates (front end / hooks / access)
```ts
import { isAdmin, isEditor, hasMinimumRole } from '@intecion/ipal-kit'
isAdmin(user) // admin?
isEditor(user) // editor or admin (hierarchy)
hasMinimumRole(user, 'editor') // at least editor
```
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.