salemaljebaly

payload plugin rbac ui

by salemaljebaly
Roles & permissions
v3
^3.76.1
MIT

Overview

Summary generated from the README

Injects a grouped checkbox permissions field into a Roles collection in the Payload admin. It auto-discovers permission strings from collections and globals, such as Create:Post or Read:Global:Setting, and validates saved permissions against that allow-list. Access rules still have to be added to each collection using the supplied helper pattern. Requires Payload 3.76.1 or later and React 19.

Features

  • Grouped checkbox permissions matrix injected into the roles collection
  • Auto-discovers permissions from collections and globals
  • Validates saved permissions against an allow-list
  • Hybrid mode merges manual permission groups with discovered ones
  • Customizable action lists, permission formatting and group labels
  • Optional callback to modify the final roles collection config

Use cases

  • Build role-based admin access where editors get per-collection create, read, update and delete rights
  • Let super admins manage permissions through a visual matrix instead of typing strings

From the author

Reusable RBAC permissions UI and helpers for Payload CMS

Installation

npm i @salemaljebaly/payload-plugin-rbac-ui

1

Stars

0

Forks

4

Open Issues

4 months ago

Last Updated

0

Weekly Downloads

v0.5.0

npm Version

60.6 KB

Unpacked Size

0

Dependencies

GitHub stars

Trends appear after 7 days of data. Daily snapshots have not started yet.

npm weekly downloads

Trends appear after 7 days of data. Daily snapshots have not started yet.

Each point is npm's rolling 7-day download count.

Health Score

32/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1158141331-root.svg)](https://payloaddirectory.dev/plugins/1158141331-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1158141331-root"><img src="https://payloaddirectory.dev/badge/1158141331-root.svg" alt="Payload Directory"></a>
Updated Jun 8, 2026Created Feb 14, 2026Published Mar 31, 2026

README

@salemaljebaly/payload-plugin-rbac-ui

npm version npm downloads license

RBAC permissions matrix UI for Payload CMS. Injects a grouped checkbox UI into your Roles collection and validates saved permissions against an auto-discovered allow-list.

Permissions Matrix UI

Install

pnpm add @salemaljebaly/payload-plugin-rbac-ui
# or
npm install @salemaljebaly/payload-plugin-rbac-ui

Setup

1. Access helpers

Create these helpers first — they are used in the collections below.

// src/access/index.ts
import type { Access } from 'payload'

/** Check if user is a super admin (bypasses all permission checks). */
export function isSuperAdmin(user: any): boolean {
  return user?.superAdmin === true
}

/** Check if user has a specific permission in any of their roles. */
export function hasPermission(user: any, permission: string): boolean {
  if (!user?.roles) return false
  const roles = Array.isArray(user.roles) ? user.roles : [user.roles]
  return roles.some((role: any) => {
    const permissions = typeof role === 'object' ? role?.permissions : null
    return Array.isArray(permissions) && permissions.includes(permission)
  })
}

/** Use in collection access — super admins always pass, others need the permission. */
export const checkPermission =
  (permission: string): Access =>
  ({ req: { user } }) =>
    isSuperAdmin(user) || hasPermission(user, permission)

2. Users collection

Add a superAdmin flag and a roles relationship to your Users collection:

// src/collections/Users.ts
fields: [
  {
    name: 'superAdmin',
    type: 'checkbox',
    defaultValue: false,
    access: {
      // Only super admins can grant super admin access
      update: ({ req: { user } }) => isSuperAdmin(user),
    },
    admin: { description: 'Grants full access, bypasses all permission checks.' },
  },
  {
    name: 'roles',
    type: 'relationship',
    relationTo: 'roles',
    hasMany: true,
    saveToJWT: true, // Required — without this, hasPermission() always returns false
  },
]

3. Roles collection

// src/collections/Roles.ts
import type { CollectionConfig } from 'payload'
import { isSuperAdmin } from '../access'

export const Roles: CollectionConfig = {
  slug: 'roles',
  admin: { useAsTitle: 'name' },
  access: {
    read:   ({ req: { user } }) => isSuperAdmin(user),
    create: ({ req: { user } }) => isSuperAdmin(user),
    update: ({ req: { user } }) => isSuperAdmin(user),
    delete: ({ req: { user } }) => isSuperAdmin(user),
  },
  fields: [
    { name: 'name', type: 'text', required: true, unique: true },
    { name: 'description', type: 'textarea' },
    // 'permissions' field is injected automatically by the plugin
  ],
}

4. Add the plugin

// src/payload.config.ts
import { rbacUIPlugin } from '@salemaljebaly/payload-plugin-rbac-ui'

export default buildConfig({
  collections: [Users, Roles, Posts /* ... */],
  plugins: [
    rbacUIPlugin({
      rolesCollectionSlug: 'roles',
      autoDiscover: true,
    }),
  ],
})

5. Generate the import map

After adding the plugin, run this once so Payload registers the permissions UI component:

pnpm payload generate:importmap
# or: npx payload generate:importmap

Re-run this whenever you add or remove Payload plugins with custom UI.

6. Enforce permissions on your collections

The plugin does not protect your collections automatically. You must add checkPermission (or isSuperAdmin) to every collection you want to gate. Any collection without a custom access rule remains visible and accessible to all authenticated users.

Not sure what string to pass? Complete the first setup (step 7), then open any Role in the admin UI — all generated permission strings are listed there.

Apply to every collection — including Media:

// src/collections/Posts.ts
import { checkPermission } from '../access'

access: {
  create: checkPermission('Create:Post'),
  read:   checkPermission('Read:Post'),
  update: checkPermission('Update:Post'),
  delete: checkPermission('Delete:Post'),
}

7. First setup

No one has superAdmin: true yet, so no one can access the Roles collection. Bootstrap by adding a temporary seed route:

// src/app/seed/route.ts  — REMOVE IN PRODUCTION
import configPromise from '@payload-config'
import { getPayload } from 'payload'

export const GET = async () => {
  const payload = await getPayload({ config: configPromise })
  const { docs } = await payload.find({ collection: 'users', limit: 1, sort: 'createdAt' })
  if (!docs[0]) return Response.json({ error: 'Create an account first.' }, { status: 404 })
  await payload.update({
    collection: 'users',
    id: docs[0].id,
    data: { superAdmin: true } as any,
    overrideAccess: true,
  })
  return Response.json({ message: 'Done! Log out and back in.', email: docs[0].email })
}
  1. Start your dev server, create your account at /admin/create-first-user
  2. Visit http://localhost:3000/seed — your account is now superAdmin
  3. Log out and back in so the JWT refreshes
  4. Delete the seed route before deploying

The re-login step is required — the superAdmin flag is read from the JWT on each request.


Permission string format

TypeSlugGenerated strings
CollectionpostsCreate:Post, Read:Post, Update:Post, Delete:Post
CollectioncategoriesCreate:Category, Read:Category, …
GlobalsettingsRead:Global:Setting, Update:Global:Setting

Rule: slug → singularize → PascalCase. Open the Roles admin UI to see the exact strings for your app.

Hybrid mode (auto + custom)

rbacUIPlugin({
  autoDiscover: true,
  permissionGroups: [
    {
      label: 'Post',
      permissions: [
        { action: 'Publish', description: 'Publish posts', permission: 'Publish:Post' },
      ],
    },
  ],
})

Auto-discovered and manual groups with the same label are merged.

Options

OptionDefaultDescription
rolesCollectionSlug'roles'Slug of your roles collection
permissionsFieldName'permissions'Field name to inject/patch
permissionGroups[]Manual permission groups
autoDiscoverfalsetrue or config object (see below)
rolesFieldDescription—Description shown under the field
ensurePermissionsFieldtrueAuto-add field if missing. Set false to skip
customFieldPath—Override the React component path
onConfigureRolesCollection—Callback to modify the final collection config

autoDiscover options

OptionDefaultDescription
collectionstrueDiscover from collections
globalstrueDiscover from globals
collectionActions['Create','Read','Update','Delete']Actions per collection
globalActions['Read','Update']Actions per global
includeRolesCollectiontrueInclude the roles collection itself. Set false to exclude — permissions like Create:Role won't be in the allow-list, so don't use them in seed data
formatPermission(ctx)—ctx: { action: string, slug: string, source: 'collection' | 'global' } → string
formatGroupLabel(ctx)—ctx: { slug: string, source: 'collection' | 'global' } → string

Compatibility

  • Payload: ^3.76.1
  • React: ^19
  • Next.js: >=15.2.9 <15.3.0 || >=15.3.9 <15.4.0 || >=15.4.11 <15.5.0 || >=16.2.0-canary.10 <17.0.0

Development

pnpm install
pnpm build
pnpm typecheck
pnpm test

Contributing

  1. Fork and branch from main.
  2. Make focused changes with tests.
  3. Run pnpm build && pnpm typecheck && pnpm test.
  4. Open a PR with clear before/after context.

See CONTRIBUTING.md for details.

Releasing

pnpm version patch   # or minor / major
pnpm publish --access public
git tag -a vX.Y.Z -m "Release vX.Y.Z"
git push origin vX.Y.Z

Similar plugins