gngz

rbac

by gngz
payload-kit
Roles & permissions
v3
^3.0.0
^3.0.0 · prerelease
MIT

Overview

Summary generated from the README

Maps business roles to atomic capabilities with inheritance and domain wildcards, resolved in memory without extra database queries. Registered through a plugin that takes a roles map, with helpers to guard collection and field access and to check capabilities in server code. Combinators merge boolean checks with Payload Where constraints. Requires Payload 3.

Features

  • Roles with capability inheritance and safe handling of circular inheritance
  • Domain wildcards such as orders:* and *, plus an opt-in super admin role
  • Collection guards: requireAuth, requireRole, requireCapability, nobody and anyone
  • Field guards including readOnlyField and systemOnlyField to block privilege escalation
  • and/or combinators that preserve Payload Where query constraints
  • createRbac for isolated engine instances and a write-protected role field builder

Use cases

  • Restrict order or inventory actions to roles based on atomic capabilities
  • Protect sensitive user fields such as the role field from unauthorized edits
  • Combine capability checks with row-level filters so customers only read their own records

From the author

Role-Based Access Control (RBAC) and capability engine for Payload CMS

Installation

npm i @payload-kit/rbac

0

Stars

0

Forks

3

Open Issues

Yesterday

Last Updated

597

Weekly Downloads

v0.1.0-alpha.1

npm Version

110.3 KB

Unpacked Size

0

Dependencies

Repository stars

00 since Nov 22

Show data
Repository stars by day (UTC)
DateStars
Dec 5, 20260
Dec 4, 20260
Dec 3, 20260
Dec 2, 20260
Dec 1, 20260
Nov 30, 20260
Nov 29, 20260
Nov 28, 20260
Nov 27, 20260
Nov 26, 20260
Nov 25, 20260
Nov 24, 20260
Nov 23, 20260
Nov 22, 20260

Shared by 5 plugins in gngz/payload-kit.

npm weekly downloads

Trends appear after 7 days of data. Collecting since Dec 2 — 4 daily snapshots so far.

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

Health Score

67/100Good

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/1474969753-rbac.svg)](https://payloaddirectory.dev/plugins/1474969753-rbac)
HTML
<a href="https://payloaddirectory.dev/plugins/1474969753-rbac"><img src="https://payloaddirectory.dev/badge/1474969753-rbac.svg" alt="Payload Directory"></a>
Updated Dec 4, 2026Created Nov 27, 2026Published Nov 29, 2026

README

@payload-kit/rbac

Role-Based Access Control (RBAC) and capability engine for Payload CMS. Maps business roles to atomic capabilities with inheritance, domain wildcards (orders:*, *), collection and field access guards, and logical combinators (and, or, not) where and/or preserve Payload Where query constraints.

Installation

pnpm add @payload-kit/rbac

Peer dependency: payload (^3.0.0).

Usage

1. Register the plugin

import { buildConfig } from "payload";
import { rbacPlugin } from "@payload-kit/rbac";

export default buildConfig({
  plugins: [
    rbacPlugin({
      roles: {
        customer: {
          label: "Customer",
          capabilities: ["orders:create", "orders:read-own"],
        },
        operator: {
          label: "Operator",
          capabilities: ["orders:read", "orders:update", "inventory:manage"],
          inherits: ["customer"],
        },
        admin: {
          label: "Administrator",
          capabilities: ["*"],
        },
      },
      defaultRole: "customer",
      superAdminRole: "admin",
    }),
  ],
});

superAdminRole is opt-in (undefined by default); set it explicitly (e.g. "admin") to enable super-admin capability bypass. Capability resolution runs in memory with no extra database queries and handles circular inheritance safely.

2. Protect collections

Combinators (or, and) merge boolean capability checks with Payload Row-Level Security (Where) filters (not inverts boolean rules and throws a TypeError on Where clauses):

import type { CollectionConfig } from "payload";
import { and, nobody, or, requireAuth, requireCapability, requireRole } from "@payload-kit/rbac";

export const Orders: CollectionConfig = {
  slug: "orders",
  access: {
    read: or(requireCapability("orders:read"), ({ req: { user } }) => ({
      "customer.email": { equals: user?.email },
    })),
    create: and(requireAuth(), requireCapability("orders:create")),
    update: requireCapability("orders:update"),
    delete: requireCapability("orders:delete"),
  },
  fields: [],
};

export const AuditLogs: CollectionConfig = {
  slug: "audit-logs",
  access: {
    create: nobody,
    update: nobody,
    delete: requireRole("admin"),
  },
  fields: [],
};

3. Protect fields

Use field guards to restrict sensitive fields or prevent privilege escalation on role fields:

import {
  readOnlyField,
  requireFieldCapability,
  requireFieldRole,
  systemOnlyField,
} from "@payload-kit/rbac";

export const Users = {
  slug: "users",
  fields: [
    {
      name: "role",
      type: "select",
      access: {
        update: requireFieldCapability("users:manage"),
      },
    },
    {
      name: "internalNotes",
      type: "textarea",
      access: {
        read: requireFieldRole(["admin", "operator"]),
        update: requireFieldCapability("users:manage"),
      },
    },
    {
      name: "oidcSub",
      type: "text",
      access: readOnlyField(),
    },
    {
      name: "webhookSecret",
      type: "text",
      access: systemOnlyField(),
    },
  ],
};

4. Check capabilities in server code

import { hasAnyCapability, hasCapability, hasRole } from "@payload-kit/rbac";

if (hasCapability(user, "inventory:manage")) {
  // allow stock update
}

if (hasAnyCapability(user, ["orders:update", "orders:delete"])) {
  // show management actions
}

if (hasRole(user, "admin")) {
  // admin-only branch
}

5. Isolated instances with createRbac

When you need an RBAC engine scoped to a specific tenant or module instead of the global registry:

import { createRbac } from "@payload-kit/rbac";

const appRbac = createRbac({
  roles: {
    viewer: { capabilities: ["reports:view"] },
    editor: { capabilities: ["reports:*"], inherits: ["viewer"] },
  },
});

const canExport = appRbac.hasCapability(user, "reports:export");

API reference

Plugin options (RbacPluginOptions)

  • roles: Map of role names to { label?, capabilities, inherits? }.
  • capabilities: Optional exhaustive list of valid capabilities.
  • defaultRole: Default role applied by createRoleField / rbac.roleField().
  • superAdminRole: Optional role that bypasses all capability checks (opt-in; e.g. "admin").
  • anonymousRole: Optional role for unauthenticated visitors in role/capability checkers.
  • userCollectionSlug: Optional user collection slug enforced during runtime user.collection validation (fails closed for other auth collections when set).
  • rolesFieldName: Field name storing the user role(s) (default "role").
  • createRoleField(options, overrides?) / rbac.roleField(overrides?): Field builder returning a write-protected SelectField that you can place anywhere in your users collection (fields, tabs, row, or sidebar).

Role and capability checkers

  • hasRole(user, role, options?): Checks whether the user has the given role.
  • hasAnyRole(user, roles, options?): Checks whether the user has at least one listed role.
  • hasAllRoles(user, roles, options?): Checks whether the user has every listed role.
  • hasCapability(user, capability, options?): Evaluates wildcards, inheritance, and superAdminRole.
  • hasAnyCapability(user, capabilities, options?): Checks whether the user has at least one capability.
  • hasAllCapabilities(user, capabilities, options?): Checks whether the user has every listed capability.

Collection guards and combinators

  • nobody / denyAll: Always returns false.
  • anyone / allowAll: Always returns true.
  • requireAuth(options?): Requires an authenticated user (accepts AuthCheckOptions with userCollectionSlug, failing closed for other auth collections when configured).
  • requireRole(role | role[]): Requires one of the listed roles.
  • requireCapability(capability | capability[], options?): Requires matching capability.
  • or(...rules): Returns true if any rule returns true, merges Where clauses with { or: [...] }, or returns false if all deny.
  • and(...rules): Returns false if any rule returns false, merges Where clauses with { and: [...] }, or returns true if all allow.
  • not(rule): Inverts a boolean access decision.

Field guards and combinators

  • readOnlyField(): Returns { create: nobodyField, update: nobodyField }.
  • systemOnlyField(): Returns { create: nobodyField, read: nobodyField, update: nobodyField }.
  • nobodyField / denyField: Always returns false.
  • anyoneField / allowField: Always returns true.
  • requireFieldAuth(options?), requireFieldRole(role | role[]), requireFieldCapability(capability | capability[], options?): Field-level equivalents of the collection guards (requireFieldAuth accepts AuthCheckOptions for userCollectionSlug enforcement).
  • fieldOr(...rules), fieldAnd(...rules), fieldNot(rule): Boolean combinators for field access.

License

MIT

Similar plugins