npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nouance/payload-simple-rbac

v0.2.0

Published

PayloadCMS plugin to help handle simple RBAC permissions.

Downloads

30

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