Adds a role field to chosen user collections and applies per-operation access rules to collections and globals. Roles are listed in order of priority, so higher roles inherit the access of lower ones. Permissions can be a role name, published-only access based on drafts, or full public access. Marked as beta and built for Payload 1.x.
PayloadCMS plugin to help handle simple RBAC permissions.
Installation
npm i @nouance/payload-simple-rbac41
Stars
1
Forks
2
Open Issues
2 years ago
Last Updated
0
Weekly Downloads
v0.2.0
npm Version
31.3 KB
Unpacked Size
0
Dependencies
Trends appear after 7 days of data. Daily snapshots have not started yet.
Trends appear after 7 days of data. Daily snapshots have not started yet.
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/616924057-root)<a href="https://payloaddirectory.dev/plugins/616924057-root"><img src="https://payloaddirectory.dev/badge/616924057-root.svg" alt="Payload Directory"></a>README
Needs more testing!
A plugin for Payload CMS to provide a baseline to help handle user roles and permissions.
yarn add @nouance/payload-simple-rbac
# OR
npm i @nouance/payload-simple-rbac
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.
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;
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
Stringread | update | create | deleteglobals
Array | Optional
Must contain the following
Stringread | updaterole
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;
import { starterRoles } from "@nouance/payload-simple-rbac";
// ...
roles: starterRoles,
// ...
starterRoles is an array containing 3 roles, editor | manager | admin .
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"]);
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;
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
multi tenant
Manage multiple tenants with scoped content and access from a single Payload admin panel
10090.4k/wk45.4kv3
rbac
Database-stored roles with a per-collection CRUD permission matrix enforced across all collections and globals
6327/wk15v3
payload plugin rbac
Database-driven role-based access control with a permission matrix and data scoping for Payload
5627/wk10v3
rbac
Database-driven role-based access control with a per-collection permissions matrix in the Payload admin
530/wk0v4
abac
Attribute-based access control that compiles user attributes into Payload where clauses
520/wk181v3
Payload RBAC Plugin
Database-backed role and permission management with row-level conditions for Payload collections
500/wk6v3