NouanceLabs

payload simple rbac

by NouanceLabs
Roles & permissions
v1
^1.6.22
MIT

Overview

Summary generated from the README

Adds a role field to chosen auth collections and applies per-operation access rules to collections and globals from a single plugin config. Roles are listed in order of priority, so higher roles inherit the access of lower ones. Built for Payload 1.x and marked as beta by the author.

Features

  • Priority-ordered roles where later roles inherit earlier access
  • Adds a configurable role field with a default role to auth collections
  • Per-collection read, create, update and delete permissions
  • Per-global read and update permissions
  • Built-in publishedOnly and public access options
  • Exported helpers hasRole, hasRoleField, publicAccess and publishedOnly

Use cases

  • Restrict editing and deletion of posts to editor and manager roles
  • Expose published content publicly while keeping drafts private
  • Reuse role checks in custom field-level access control

From the author

PayloadCMS plugin to help handle simple RBAC permissions.

Installation

npm i @nouance/payload-simple-rbac

40

Stars

1

Forks

2

Open Issues

2 years ago

Last Updated

27

Weekly Downloads

v0.2.0

npm Version

31.3 KB

Unpacked Size

0

Dependencies

GitHub stars

400 since Sep 7

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

27+8 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 202627
Dec 5, 202627
Dec 4, 202618
Dec 3, 202621
Dec 2, 202616
Dec 1, 202620
Nov 30, 202616
Nov 29, 202622
Nov 28, 202622
Nov 27, 202619
Nov 26, 202619
Nov 25, 202616
Nov 24, 202613
Nov 23, 202613
Nov 22, 20267
Nov 21, 20267
Nov 20, 20267
Nov 19, 20266
Nov 18, 202615
Nov 17, 202614
Nov 16, 202614
Nov 15, 202612
Nov 14, 202612
Nov 13, 202612
Nov 12, 202610
Nov 11, 20261
Nov 10, 20267
Nov 9, 20267
Nov 8, 202613
Nov 7, 202613
Nov 6, 202613
Nov 5, 202615
Nov 4, 202616
Nov 3, 202610
Nov 2, 202610
Nov 1, 20264
Oct 31, 20265
Oct 30, 20265
Oct 29, 20263
Oct 28, 20262
Oct 27, 20269
Oct 26, 202610
Oct 25, 20269
Oct 24, 20268
Oct 23, 20268
Oct 22, 202611
Oct 21, 202613
Oct 20, 20266
Oct 19, 20268
Oct 18, 20269
Oct 17, 202611
Oct 16, 202611
Oct 15, 20268
Oct 14, 202618
Oct 13, 202618
Oct 12, 202615
Oct 11, 202615
Oct 10, 202613
Oct 9, 202614
Oct 8, 202614
Oct 7, 20263
Oct 6, 20263
Oct 5, 20263
Oct 4, 20262
Oct 3, 20262
Oct 2, 20261
Oct 1, 20261
Sep 30, 20260
Sep 29, 20261
Sep 28, 20261
Sep 27, 20261
Sep 26, 20267
Sep 25, 20267
Sep 24, 20267
Sep 23, 20267
Sep 22, 20267
Sep 21, 202613
Sep 20, 202613
Sep 19, 20267
Sep 18, 20267
Sep 17, 20268
Sep 16, 20269
Sep 15, 20269
Sep 14, 20264
Sep 13, 20267
Sep 12, 202610
Sep 11, 202612
Sep 10, 202614
Sep 9, 202617
Sep 8, 202618
Sep 7, 202619

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

Health Score

42/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/616924057-root.svg)](https://payloaddirectory.dev/plugins/616924057-root)
HTML
<a href="https://payloaddirectory.dev/plugins/616924057-root"><img src="https://payloaddirectory.dev/badge/616924057-root.svg" alt="Payload Directory"></a>
Updated Dec 25, 2023Created Mar 21, 2023Published Apr 17, 2023

README

Payload RBAC Plugin (BETA)

Needs more testing!

A plugin for Payload CMS to provide a baseline to help handle user roles and permissions.

Installation

  yarn add @nouance/payload-simple-rbac
  # OR
  npm i @nouance/payload-simple-rbac

How it works

On the face of it, this plugin only aims to get you up and started with some form of Role-Based Access Control.
You create an array of roles in order of priority with the latter being the most important, for example this means that an admin can do everything an editor can and more but an editor will be limited to their level of access.

Basic Usage

In the plugins array of your Payload config, call the plugin with options:

import { buildConfig } from "payload/config";
import payloadSimpleRBAC, { starterRoles } from "@nouance/payload-simple-rbac";

const config = buildConfig({
  // ... rest of my config
  plugins: [
    payloadSimpleRBAC({
      roles: starterRoles,
      users: [Users.slug],
      defaultRole: "editor", // set a default
      collections: [
        {
          slug: Posts.slug,
          permissions: {
            read: "publishedOnly",
            update: "editor",
            create: "editor",
            delete: "manager",
          },
        },
      ],
    }),
  ],
});

export default config;

Options

  • Roles

    Array | Required

    An array of roles as strings in order of priority, roles at the end have a higher priority.

  • users

    Array | Required

    An array of user slugs to attach the role field to.

  • defaultRole

    String | Required

    Set the default role for users to have.

  • fieldName

    String | Optional | Defaults to role

    Allows you to configure the machine name of the role field to avoid conflicts.

  • collections

    Array | Optional

    Must contain the following

    • slug : String
    • permissions : read | update | create | delete
  • globals

    Array | Optional

    Must contain the following

    • slug : String
    • permissions : read | update

Available permissions

  • role

    This is your custom role name, any user with the same role or higher will pass this permission.

  • publishedOnly

    Allow access only to published content, based on Payload's draft system. So your collection must have it enabled.

  • public

    This allows full public access. The actual function is

    publicAccess = () => true;
    

Starter roles

import { starterRoles } from "@nouance/payload-simple-rbac";

// ...
  roles: starterRoles,
// ...

starterRoles is an array containing 3 roles, editor | manager | admin .

Functions

We've exposed our internal functions in case you want to use them in your own code or in combination with other access controls.

  • hasRole

    Takes in your target role and full list of roles.

    read: hasRole("editor", myRoles);
    
  • hasRoleField

    Takes in your target role and full list of roles. Specifically used for field access control.

    {
      name: "metadata",
      type: "json",
      access: {
        read: hasRoleField("admin", myRoles),
      },
    },
    
  • publicAccess

    Anyone can pass this access control.

    read: publicAccess();
    
  • publishedOnly

    Any logged in user can pass this access control, non-authenticated requests can only pass published checks. You can also pass an array of strings for auth collection slugs to limit the auth'd access only to a specific set of users.

    read: publishedOnly();
    
    // limited to 'user' collection slug, other auth collections will not pass
    read: publishedOnly(["user"]);
    

Full example

import { buildConfig } from "payload/config";
import payloadSimpleRBAC from "@nouance/payload-simple-rbac";

const config = buildConfig({
  plugins: [
    payloadSimpleRBAC({
      roles: ["customer", "editor", "manager", "admin"],
      users: [Users.slug],
      defaultRole: "customer",
      collections: [
        {
          slug: Posts.slug,
          permissions: {
            read: "publishedOnly",
            update: "editor",
            create: "editor",
            delete: "manager",
          },
        },
        {
          slug: Categories.slug,
          permissions: {
            read: "public",
          },
        },
        {
          slug: Tags.slug,
          permissions: {
            read: "publishedOnly",
            create: "manager",
            update: "manager",
            delete: "admin",
          },
        },
      ],
    }),
  ],
});

export default config;

Development

For development purposes, there is a full working example of how this plugin might be used in the demo of this repo:

git clone [email protected]:NouanceLabs/payload-simple-rbac.git \
  cd payload-simple-rbac && yarn \
  cd demo && yarn \
  cp .env.example .env \
  vim .env \ # add your creds to this file
  yarn dev

Similar plugins