Mhmod-Hsn

Payload RBAC Plugin

by Mhmod-Hsn
Roles & permissions
v3
^3.85.0
MIT

Overview

Summary generated from the README

Adds Roles and Permissions collections to Payload and injects a roles relationship field into your auth collection, saved to the JWT. Access control is done by checking permissions rather than roles, using the checkPermission wrapper and hasPermission helper. Permissions can carry conditions set in the admin UI that map to Payload Where queries. Requires Payload 3.85 or newer.

Features

  • Auto-injected Roles and Permissions collections with configurable slugs
  • Roles relationship field added to the auth collection and saved to the JWT
  • checkPermission wrapper returns standard Payload access functions
  • Row-level conditions with variables such as user id and user roles
  • Bulk CRUD generator creates permissions like posts:read in one step
  • Extra custom fields and hide options for the RBAC admin collections

Use cases

  • Protect collections with permissions that admins change without code deploys
  • Restrict users to documents they own using conditions set in the admin UI
  • Generate a full set of CRUD permissions for a new collection quickly

From the author

RBAC plugin for payloadcms

Installation

npm i payload-rbac-plugin

6

Stars

0

Forks

0

Open Issues

6 months ago

Last Updated

38

Weekly Downloads

v1.1.0

npm Version

91.1 KB

Unpacked Size

0

Dependencies

GitHub stars

6+2 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 20266
Dec 5, 20266
Dec 4, 20266
Dec 3, 20266
Dec 2, 20266
Dec 1, 20266
Nov 30, 20266
Nov 29, 20266
Nov 28, 20266
Nov 27, 20266
Nov 26, 20266
Nov 25, 20266
Nov 24, 20266
Nov 23, 20266
Nov 22, 20266
Nov 21, 20266
Nov 20, 20266
Nov 19, 20266
Nov 18, 20266
Nov 17, 20266
Nov 16, 20266
Nov 15, 20266
Nov 14, 20266
Nov 13, 20266
Nov 12, 20266
Nov 11, 20266
Nov 10, 20266
Nov 9, 20266
Nov 8, 20266
Nov 7, 20266
Nov 6, 20266
Nov 5, 20266
Nov 4, 20266
Nov 3, 20266
Nov 2, 20266
Nov 1, 20266
Oct 31, 20266
Oct 30, 20266
Oct 29, 20266
Oct 28, 20266
Oct 27, 20266
Oct 26, 20266
Oct 25, 20266
Oct 24, 20266
Oct 23, 20266
Oct 22, 20266
Oct 21, 20266
Oct 20, 20266
Oct 19, 20266
Oct 18, 20266
Oct 17, 20266
Oct 16, 20266
Oct 15, 20265
Oct 14, 20265
Oct 13, 20265
Oct 12, 20264
Oct 11, 20264
Oct 10, 20264
Oct 9, 20264
Oct 8, 20264
Oct 7, 20264
Oct 6, 20264
Oct 5, 20264
Oct 4, 20264
Oct 3, 20264
Oct 2, 20264
Oct 1, 20264
Sep 30, 20264
Sep 29, 20264
Sep 28, 20264
Sep 27, 20264
Sep 26, 20264
Sep 25, 20264
Sep 24, 20264
Sep 23, 20264
Sep 22, 20264
Sep 21, 20264
Sep 20, 20264
Sep 19, 20264
Sep 18, 20264
Sep 17, 20264
Sep 16, 20264
Sep 15, 20264
Sep 14, 20264
Sep 13, 20264
Sep 12, 20264
Sep 11, 20264
Sep 10, 20264
Sep 9, 20264
Sep 8, 20264
Sep 7, 20264
npm weekly downloads

38+27 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202638
Dec 5, 202618
Dec 4, 202620
Dec 3, 202622
Dec 2, 202633
Dec 1, 202650
Nov 30, 202654
Nov 29, 202654
Nov 28, 202641
Nov 27, 202638
Nov 26, 202634
Nov 25, 202623
Nov 24, 20266
Nov 23, 20263
Nov 22, 20265
Nov 21, 20268
Nov 20, 20268
Nov 19, 20269
Nov 18, 202620
Nov 17, 202620
Nov 16, 202626
Nov 15, 202623
Nov 14, 202621
Nov 13, 202622
Nov 12, 202632
Nov 11, 202624
Nov 10, 202629
Nov 9, 202627
Nov 8, 202632
Nov 7, 202637
Nov 6, 202636
Nov 5, 202628
Nov 4, 202625
Nov 3, 202620
Nov 2, 202627
Nov 1, 202622
Oct 31, 202619
Oct 30, 202620
Oct 29, 202617
Oct 28, 202617
Oct 27, 202621
Oct 26, 202610
Oct 25, 202611
Oct 24, 20269
Oct 23, 20267
Oct 22, 20269
Oct 21, 202610
Oct 20, 20266
Oct 19, 20266
Oct 18, 20265
Oct 17, 20265
Oct 16, 20265
Oct 15, 20264
Oct 14, 20263
Oct 13, 20263
Oct 12, 20262
Oct 11, 20263
Oct 10, 20262
Oct 9, 20262
Oct 8, 20262
Oct 7, 20262
Oct 6, 20264
Oct 5, 20268
Oct 4, 202623
Oct 3, 202625
Oct 2, 202628
Oct 1, 202632
Sep 30, 202633
Sep 29, 202631
Sep 28, 202627
Sep 27, 202614
Sep 26, 202615
Sep 25, 202613
Sep 24, 20268
Sep 23, 20267
Sep 22, 202619
Sep 21, 202619
Sep 20, 202623
Sep 19, 202620
Sep 18, 202624
Sep 17, 202631
Sep 16, 202637
Sep 15, 202627
Sep 14, 202630
Sep 13, 202624
Sep 12, 202625
Sep 11, 202620
Sep 10, 202613
Sep 9, 202610
Sep 8, 202610
Sep 7, 202611

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

Health Score

49/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/1257615366-root.svg)](https://payloaddirectory.dev/plugins/1257615366-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1257615366-root"><img src="https://payloaddirectory.dev/badge/1257615366-root.svg" alt="Payload Directory"></a>
Updated Jun 8, 2026Created Jun 2, 2026Published Jun 8, 2026

README

Payload CMS Dynamic RBAC Plugin

NPM Version NPM Downloads License CI Status

A professional, database-backed Role-Based Access Control (RBAC) system for Payload CMS (v3).

This plugin emphasizes checking permissions instead of roles to avoid hard-coded authorization logic in your source code, making your access control highly scalable, modular, and dynamic.


Features

  • Attribute-Based Access Control (ABAC): Support for dynamic, Row-Level Security conditions configured right from the Payload Admin UI. No code updates required!
  • Dynamic Collections: Automatically injects database-backed Roles and Permissions collections.
  • Auth Collection Extension: Injects a roles relationship field into your target auth collection (e.g., users) with saveToJWT: true for zero-cost runtime checks.
  • Bulk Permission Generator: In the Admin UI, easily switch between creating a single permission or using the Bulk CRUD Generator to automatically generate separate permissions (e.g., posts:create, posts:read, posts:update, posts:delete) at once.
  • UI-Only Helpers: Bulk generator fields (type, collectionName, CRUD checkboxes) are purely for the UI and are never saved to your database, keeping your collections clean.
  • Customizable Schemas: Allow developers to inject additional custom fields into both Roles and Permissions schemas.
  • Granular Control: Set visibility permissions (hideRoles, hidePermissions) dynamically or statically for the RBAC admin interface.
  • Helper Utilities: Simple and robust functions to check permissions (hasPermission) and access control wrappers (checkPermission).
  • Fully Tested: Powered by a robust integration and unit test suite built with Vitest.

Screenshots

1. Permissions List View

Shows flat generated permissions (e.g. posts:create, posts:read) and legacy system permissions. Permissions List View

2. Single Permission Form

Create individual custom permissions. Single Permission Form

3. Bulk CRUD Generator Form

Select a collection and check CRUD actions to instantly generate multiple permissions. Bulk CRUD Generator Form

4. Roles List View

Displays roles and all permissions associated with them. Roles List View

5. Role Configuration Form

Assign permissions directly to roles in the admin UI. Role Configuration Form


Installation

Add the plugin to your project:

pnpm add payload-rbac-plugin
# or
npm install payload-rbac-plugin
# or
yarn add payload-rbac-plugin

Setup & Configuration

Import and configure the plugin in your payload.config.ts:

import { buildConfig } from 'payload'
import { rbac, hasPermission } from 'payload-rbac-plugin'

export default buildConfig({
  collections: [
    {
      slug: 'posts',
      fields: [],
    },
  ],
  plugins: [
    rbac({
      enabled: true,
      // Optional configurations:
      authCollectionSlug: 'users',        // Default: 'users'
      rolesCollectionSlug: 'roles',        // Default: 'roles'
      permissionsCollectionSlug: 'permissions', // Default: 'permissions'
      
      // Control access/visibility of the RBAC panel:
      hidePermissions: ({ user }) => !hasPermission(user, 'access:permissions'),
      hideRoles: ({ user }) => !hasPermission(user, 'access:roles'),
    }),
  ],
})

Configuration Options

OptionTypeDefaultDescription
enabledbooleantrueEnable or disable the plugin.
authCollectionSlugstring'users'The collection slug representing the users collection.
rolesCollectionSlugstring'roles'The slug for the automatically injected Roles collection.
permissionsCollectionSlugstring'permissions'The slug for the automatically injected Permissions collection.
rolesFieldsField[][]Extra custom fields to inject into the Roles collection.
permissionsFieldsField[][]Extra custom fields to inject into the Permissions collection.
hideRolesboolean | ((args: { user: any }) => boolean)falseHide the Roles collection from the sidebar navigation.
hidePermissionsboolean | ((args: { user: any }) => boolean)falseHide the Permissions collection from the sidebar navigation.
permissionsAccessCollectionConfig['access']{ ... }Access control functions for the generated Permissions collection (defaults to public).
rolesAccessCollectionConfig['access']{ ... }Access control functions for the generated Roles collection (defaults to public).

Usage

1. Protecting Collections (Access Control)

You can protect collections using the checkPermission Higher-Order Function. It returns a standard Payload Access control resolver.

import { checkPermission } from 'payload-rbac-plugin'

export const PostsCollection = {
  slug: 'posts',
  access: {
    create: checkPermission('posts:create'),
    read: checkPermission('posts:read'),
    update: checkPermission('posts:update'),
    delete: checkPermission('posts:delete'),
  },
  fields: [],
}

2. Dynamic Row-Level Security (ABAC)

The checkPermission wrapper natively supports Attribute-Based Access Control (ABAC). If an administrator assigns a permission with conditions in the Admin UI (e.g. sender equals {{user.id}}), checkPermission will automatically map it to a Payload Where object without any code changes!

If you need to construct these queries manually outside of standard access control, you can use the getPermissionQuery utility:

import { getPermissionQuery } from 'payload-rbac-plugin'

export const CustomEndpoint = async (req) => {
  // Returns `true`, `false`, or a Payload Where object depending on UI conditions
  const accessQuery = getPermissionQuery(req.user, 'posts:read')
  
  if (!accessQuery) return Response.json({ error: 'Forbidden' }, { status: 403 })

  const posts = await req.payload.find({
    collection: 'posts',
    where: typeof accessQuery === 'object' ? accessQuery : undefined,
  })
}
Supported Dynamic Variables

When defining conditional Values in the UI, you can inject variables:

  • {{user.id}} - Replaced with the current user's ID
  • {{user.roles}} - Replaced with an array of the user's role IDs (useful for the in operator)
  • {{user.role}} - Replaced with the user's role ID (if using a single custom role field)
  • true / false - Properly parsed into booleans (useful for the exists operator)

3. Manual Permission Verification (hasPermission)

For custom endpoints, hooks, or conditionally rendering logic, use the hasPermission utility:

import { hasPermission } from 'payload-rbac-plugin'

const myCustomEndpoint = (req, res) => {
  const user = req.user
  
  if (hasPermission(user, 'export:reports')) {
    // allow operation
  } else {
    res.status(403).send('Forbidden')
  }
}

Bulk Permission Generator UI

When navigating to the Permissions collection in your Payload Admin panel:

  1. Select Bulk CRUD Generator in the Type select box (visible only during document creation).
  2. Enter the target collection slug (e.g. posts) in the Collection Name field.
  3. Check the CRUD operations you wish to generate (e.g., create, read).
  4. Save the document.
  5. The plugin runs a beforeChange hook that creates individual flat records (posts:create, posts:read) in the database, and avoids saving any temporary helper fields to the database.

Development & Testing

If you are developing this plugin locally, you can run the test suite:

# Run unit and integration tests
pnpm test:int

# Run tests in watch mode
pnpm test:int --watch

License

MIT

Similar plugins