Files
ipal-kit/docs/access.md
T
radoslaw.smolinskiandClaude Opus 5.5 ce0a390351 Security: mandatory Turnstile when configured, admin-only test email, roles on create, safer client IP
- Turnstile is on only when both keys are set (panel or env); then submitForm
  requires a valid token, so a bot can no longer skip the check by omitting it.
  Not configured -> no widget, no check. New getTurnstileConfig() for layouts.
- /api/ipal/test-email now requires the admin role (403 for other users).
- roles field: create access is admin-only, so open sign-up can't self-assign admin.
- clientIp reads X-Forwarded-For from the right, accepts Headers, can be pinned
  via option or IPAL_IP_HEADER; exposed on search/MCP routes.
- Pluggable shared rate-limit store (setRateLimitStore) for multi-instance setups.
- Replace the template int.spec with real unit tests in test/ (44 tests).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 23:54:25 +02:00

2.3 KiB

access

Kontrola dostępu oparta na rolach: hierarchia admin > editor > user. Plugin wstrzykuje sztywne pole roles do klienckiej kolekcji auth i dostarcza predykaty oraz gotowe funkcje dostępu (collection-level i field-level).

Config (payload.config.ts)

ipalKit({
  access: {
    authCollection: 'users',   // slug Twojej kolekcji auth
    // defaultRole: 'user',     // rola nowych użytkowników, domyślnie 'user'
  },
})

Wstrzykuje pole roles (select hasMany, saveToJWT, tylko admin może nadawać i zmieniać role — także przy tworzeniu konta, więc otwarta rejestracja nie przepuści roles: ['admin']; nie-admin dostaje defaultRole) do wskazanej kolekcji. Ekran pierwszego użytkownika i Local API (seed) działają z overrideAccess, więc pierwszego admina nadal da się utworzyć. Kolekcja należy do Ciebie (create-payload-app) — plugin tylko dokłada pole.

Po dodaniu: zadbaj, by seed / pierwszy user miał roles: ['admin'], inaczej stracisz dostęp do zasobów admin-only (np. SiteIntegrations).

Predykaty (front / hooki / access)

import { isAdmin, isEditor, hasMinimumRole } from '@intecion/ipal-kit'

isAdmin(user)                       // admin?
isEditor(user)                      // editor lub admin (hierarchia)
hasMinimumRole(user, 'editor')      // co najmniej editor

Przyjmują luźno typowanego usera (jak req.user), czytają roles defensywnie.

Gotowe funkcje dostępu

Collection-level (zwracają boolean | Where)

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: każdy widzi siebie, admin widzi wszystkich
access: { read: adminOrSelf }

// dowolny minimalny próg
access: { update: requireRole('editor') }

Field-level (zwracają boolean)

import { adminOnlyField, adminOrEditorField, requireRoleField } from '@intecion/ipal-kit'

{
  name: 'internalNote',
  type: 'textarea',
  access: { read: adminOnlyField, update: adminOnlyField },
}

Hierarchia

import { ROLE_HIERARCHY } from '@intecion/ipal-kit'
// ['user', 'editor', 'admin'] — wyższa rola spełnia wymóg niższej