teunmooij

rbac

by teunmooij
payload-tools
Roles & permissions
v1
^1.6.22
MIT

Overview

Summary generated from the README

Adds a role system to your auth collections through a Payload plugin configured with a list of roles. It provides ready-made access control functions such as allow anonymous, allow any user, allow by role, allow by environment variable and block all. Functions accept optional where filters and can be combined with composite helpers. Targets Payload 1.x.

Features

  • Plugin adds a roles field to selected auth collections
  • Access functions for anonymous, published, any user, specific role and environment value
  • Optional where filters with function operands that receive access arguments
  • Composite requireOne and requireAll helpers that can be nested and mixed with custom functions
  • Typed generics for collections give path checking and autocomplete
  • Blocked endpoints are excluded from payload-openapi and payload-swagger docs

Use cases

  • Restrict collection access to editors or admins while keeping published content public
  • Combine role checks with per-user filters so authors only see their own documents
  • Limit access to certain collections based on a deployment environment variable

From the author

Simple role based access control for your Payload cms

Installation

npm i payload-rbac

143

Stars

15

Forks

13

Open Issues

2 years ago

Last Updated

95

Weekly Downloads

v1.0.3

npm Version

39.0 KB

Unpacked Size

0

Dependencies

Repository stars

143+2 since Sep 7

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

Shared by 5 plugins in teunmooij/payload-tools.

npm weekly downloads

95+70 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202695
Dec 5, 202663
Dec 4, 202642
Dec 3, 202619
Dec 2, 202621
Dec 1, 202623
Nov 30, 202625
Nov 29, 202629
Nov 28, 202632
Nov 27, 202631
Nov 26, 202630
Nov 25, 202631
Nov 24, 202631
Nov 23, 202625
Nov 22, 202618
Nov 21, 202619
Nov 20, 202627
Nov 19, 202632
Nov 18, 202634
Nov 17, 202633
Nov 16, 202635
Nov 15, 202633
Nov 14, 202631
Nov 13, 202624
Nov 12, 202634
Nov 11, 202629
Nov 10, 202631
Nov 9, 202630
Nov 8, 202642
Nov 7, 202648
Nov 6, 202652
Nov 5, 202644
Nov 4, 202649
Nov 3, 202650
Nov 2, 202660
Nov 1, 202655
Oct 31, 202647
Oct 30, 202643
Oct 29, 202633
Oct 28, 202626
Oct 27, 202625
Oct 26, 202617
Oct 25, 202611
Oct 24, 202616
Oct 23, 202613
Oct 22, 202617
Oct 21, 202628
Oct 20, 202625
Oct 19, 202624
Oct 18, 202634
Oct 17, 202631
Oct 16, 202631
Oct 15, 202628
Oct 14, 202623
Oct 13, 202623
Oct 12, 202622
Oct 11, 202620
Oct 10, 202621
Oct 9, 202623
Oct 8, 202623
Oct 7, 202618
Oct 6, 202618
Oct 5, 202619
Oct 4, 202612
Oct 3, 20268
Oct 2, 202618
Oct 1, 202618
Sep 30, 202617
Sep 29, 202623
Sep 28, 202621
Sep 27, 202632
Sep 26, 202638
Sep 25, 202628
Sep 24, 202629
Sep 23, 202629
Sep 22, 202629
Sep 21, 202629
Sep 20, 202625
Sep 19, 202625
Sep 18, 202632
Sep 17, 202638
Sep 16, 202640
Sep 15, 202634
Sep 14, 202636
Sep 13, 202632
Sep 12, 202628
Sep 11, 202626
Sep 10, 202621
Sep 9, 202620
Sep 8, 202623
Sep 7, 202625

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

Health Score

48/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/611765581-rbac.svg)](https://payloaddirectory.dev/plugins/611765581-rbac)
HTML
<a href="https://payloaddirectory.dev/plugins/611765581-rbac"><img src="https://payloaddirectory.dev/badge/611765581-rbac.svg" alt="Payload Directory"></a>
Updated Jul 17, 2024Created Mar 9, 2023Published Apr 18, 2023

README

author snyk downloads npm version license

payload-rbac

Easy to use Role based access for your Payload cms.

Main features:

  • plugin to add role system to your users collection(s)
  • ready to use access control functions for many different scenario's
  • powerful filtering options built on top of Payloads query system

Installation

With yarn:

yarn add payload-rbac

With npm:

npm install payload-rbac

Usage

Add the plugin to your payload config to extend your auth collection:

import { buildConfig } from 'payload/config';
import rbac from 'payload-rbac';

export default buildConfig({
  plugins: [
    rbac({
      collections: ['users'], // collections to enable rbac on, default: all auth collections
      roles: ['reader', 'maintainer', 'admin'], // roles
    }),
  ],
  // The rest of your config goes here
});

Use the access control functions

All access control functions allow you to control who can access your data and allow you to add an optional filter. This documentation assumes that you are familiar with the Payload documentation on access control.

Allow anonymous

Anyone has access

import { allowAnonymous } from 'payload-rbac';

const unfilteredAccess = allowAnonymous();
const filteredAccess = allowAnonymous<Page>({ _status: { equals: 'published' } });

You can also use the filtered alias, which might make you code more readable if you're using allowAnonymous in combiniation with other access control functions.

import { filtered } from 'payload-rbac';

const filteredAccess = filtered<Page>({ _status: { equals: 'published' } });

Allow anonymous access to published documents

Any has access to published documents

import { allowPublished } from 'payload-rbac';

const allPublishedAccess = allowPublished();
const filteredAccess = allowPublished<Page>({ author: { equals: 'Santa' } });

Allow any user

Any logged in user has access

import { allowAnyUser } from 'payload-rbac';

const unfilteredAccess = allowAnyUser();
const filteredAccess = allowAnyUser<Post>({ author: { equals: ({ req }) => req.user!.id } });

Allow user with a given role

Only users with the given role have access

import { allowUserWithRole } from 'payload-rbac';

const unfilteredAccess = allowUserWithRole('admin');
const filteredAccess = allowUserWithRole<Media>('reader', { _status: { equals: 'published' } });

Allow access based on environment variable

Only allow access if the node environment variable with the given key has the given value

import { allowEnvironmentValues } from 'payload-rbac';

const unfilteredAccess = allowEnvironmentValues('SERVICE_ENV', 'staging');
const filteredAccess = allowEnvironmentValues<Alert>('SERVICE_ENV', 'staging', { _status: { equals: 'published' } });

Block all requests

Blocks all requests. If used with payload-openapi or payload-swagger, endpoints with this access control function are excluded from documentation.

import { blockAll } from 'payload-rbac';

const access = blockAll();

Filters

All payload-rbac access functions accept an optional where parameter. If a where paremeter is provided it is used as a query if access is granted. See payload documentation for more information queries.

As filter you can use a payload Where query, but you can also use functions as operands, that receive the AccessArgs as input.

import { Access } from 'payload';
import { filtered } from 'payload-rbac';

const access: Access = filtered<Page>({
  or: [
    { _status: { equals: 'published' } }, // normal where
    { author: { equals: ({ req }) => req.user?.id || '#not-an-author#' } }, // active where
  ],
});

To get the most out of the typesystem, it is recommended to use the generic type parameter on the access control function to specify the collection you're using it on (Page in the example above). When you specify the collection the typesystem will be able to check that all paths are correct and your operands are of the correct type and it will be able to provide you autocomplete suggestions.

Composite access control functions

The composite access control functions allow you to easily combine access control functions, both the functions of payload-rbac as well as your own access control functions.

Require one

Allows access if at least one of the given control functions grants access. If all of the matching control functions return a query, those queries are combined with and or statement.

import { allowPublished, allowUserWithRole, requireOne } from 'payload-rbac';

// Anyone has access to published documents, but only editors can see draft documents
const requireOne(allowPublished(), allowUserWithRole('editor'));

Require all

Allows access if all of the given control functions grants access. If one or more of the access control functions return a query, those queries are combined with and and statement.

import { allowPublished, allowAnyUser, requireAll } from 'payload-rbac';

// User needs to login to see the published documents (and cannot see draft documents)
const requireAll(allowPublished(), allowAnyUser());

Combine composites

Composites can be nested:

import { allowPublished, allowAnyUser, allowUserWithRole, requireAll, requireOne } from 'payload-rbac';

const compositeAccess = requireOne(
  requireAll(allowPublished(), allowAnyUser()), // any logged in user can access published documents
  allowUserWithRole('editor'), // editors can access all documents
);

Version history

See changelog

Similar plugins