sinloss

payload warding

by sinloss
Roles & permissions
v2
^2.1.0
MIT

Overview

Summary generated from the README

Generates user and role collections and adds access functions to collections, globals, fields and endpoints. Permissions are built from features, traits and verbs, and can be edited on the role editing page. A root role and root user with full privileges are created automatically. Targets Payload 2.x.

Features

  • Generates customizable user and role collections
  • Fine-grained permissions per collection, global, field and endpoint
  • Endpoint check functions map HTTP methods to create, read, update and delete verbs
  • Automatically populated root role and optional root user
  • Per-collection, field or endpoint opt-out via custom warding flags
  • Extension synopses and a mod hook for custom access control

Use cases

  • Define editor and admin roles with field-level permissions managed from the admin panel
  • Protect custom endpoints with the same role-based checks as collections
  • Add custom permission features on top of the user and role system

From the author

A Collections / Globals Backed Payload RBAC Plugin

Installation

npm i payload-warding

17

Stars

2

Forks

0

Open Issues

3 years ago

Last Updated

9

Weekly Downloads

v0.1.10

npm Version

127.4 KB

Unpacked Size

0

Dependencies

GitHub stars

170 since Sep 6

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

11+5 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 202611
Dec 4, 20265
Dec 3, 20265
Dec 2, 20265
Dec 1, 20269
Nov 30, 202612
Nov 29, 202616
Nov 28, 202614
Nov 27, 202617
Nov 26, 202619
Nov 25, 202620
Nov 24, 202618
Nov 23, 202616
Nov 22, 202616
Nov 21, 202619
Nov 20, 202623
Nov 19, 202629
Nov 18, 202636
Nov 17, 202635
Nov 16, 202636
Nov 15, 202633
Nov 14, 202632
Nov 13, 202626
Nov 12, 202621
Nov 11, 202616
Nov 10, 202616
Nov 9, 202614
Nov 8, 202613
Nov 7, 202611
Nov 6, 20269
Nov 5, 20267
Nov 4, 20263
Nov 3, 20264
Nov 2, 20266
Nov 1, 20267
Oct 31, 20267
Oct 30, 20269
Oct 29, 20268
Oct 28, 20268
Oct 27, 20268
Oct 26, 20267
Oct 25, 20265
Oct 24, 20266
Oct 23, 20264
Oct 22, 20264
Oct 21, 20266
Oct 20, 20264
Oct 19, 20263
Oct 18, 20265
Oct 17, 20267
Oct 16, 20267
Oct 15, 20269
Oct 14, 20269
Oct 13, 20269
Oct 12, 202611
Oct 11, 202610
Oct 10, 20267
Oct 9, 20267
Oct 8, 20265
Oct 7, 20264
Oct 6, 20264
Oct 5, 20267
Oct 4, 20267
Oct 3, 202610
Oct 2, 202612
Oct 1, 202612
Sep 30, 202614
Sep 29, 202617
Sep 28, 202613
Sep 27, 202624
Sep 26, 202633
Sep 25, 202634
Sep 24, 202635
Sep 23, 202632
Sep 22, 202631
Sep 21, 202630
Sep 20, 202619
Sep 19, 20267
Sep 18, 20264
Sep 17, 20264
Sep 16, 20265
Sep 15, 20263
Sep 14, 20265
Sep 13, 20265
Sep 12, 20265
Sep 11, 20265
Sep 10, 20264
Sep 9, 20265
Sep 8, 20265
Sep 7, 20267
Sep 6, 20266

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

Health Score

39/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/717984562-root.svg)](https://payloaddirectory.dev/plugins/717984562-root)
HTML
<a href="https://payloaddirectory.dev/plugins/717984562-root"><img src="https://payloaddirectory.dev/badge/717984562-root.svg" alt="Payload Directory"></a>
Updated Nov 17, 2023Created Nov 13, 2023Published Nov 14, 2023

README

A Collections / Globals Backed Payload RBAC Plugin

  • Easily customizable user and role collection generation.
  • Fine-grained access control configuration. All are editable in the role editing page!
  • Fully customizable access functions and access control handler functions (for endpoints).
  • Automatically populated root role with all access privileges. And a root user with this role as well!

📖 Table of Contents

✨ Synopsis

  • This plugin collects information from collections, globals and user provided extra synopses to build a collection of unified features which looks like the following data structure:
{
  feature: string, // the slug of a collection or a global
  traits: string[], // a collection of field names or endpoint (which belongs to the collection or global) paths
  verbs: string[], // create, read, update, delete
}[] // a collection of this of all the collections, globals and user provided synopses
  • Then the data is used to bring to us this:

Warding example

⚙️ Usage

yarn add payload-warding
npm i payload-warding
// ...
import warding, { convention } from "payload-warding";

//...
export default buildConfig({
  // ...
  plugins: [
    // ...
    warding(
      convention.opts({
        root: { email: process.env.YOUR_ROOT_EMAIL!, password: process.env.YOUR_ROOT_PASSWORD! },
      }),
    ),
    // ...
  ],
  // ...
});
// ...

The convention.opts will deeply merge your options with predefined default options using a function implements RFC-7386 JSON Merge Patch, see convention.ts for details.

🎨 Options

  • root
/**
 * The root user's email and password. If not provided, the root user will not
 * be created.
 */
root?: {
  email: string;
  password: string;
};

Please note that the user created in the create-first-user page will be automatically bound with a root role. So it's fine to skip the root user creation.

  • user
/**
 * The user {@link Options}.
 */
user?: Options;

type Options = {
  slug: string;
  fields?: Field[]; // payload's Field type
  title?: string;
  tag?: SpecLabels;
};

type SpecLabels = {
  plural?: Record<string, string> | string;
  singular?: Record<string, string> | string;
};
  • role
/**
 * The role {@link Options}.
 */
role?: Options;
  • label
/**
 * All the labels for plugin introduced collections, fields and select options.
 */
label?: Label;
  • ext
/**
 * Extended features in the form of {@link Synopsis}s.
 */
ext?: Synopsis[];

type Synopsis = {
  slug: string;
  traits: [string, Label | undefined][];
  label?: Label;
};

type Label = Record<string, string> | string;

This paired with the check function in convention.ts can be used to build custom access control upon the user + role system provided by this plugin.

  • mod
/**
 * Modifies the {@link Built} result after {@link Warding.build}.
 */
mod?: (w: Built) => Built;

type Built = {
  collections: { user: CollectionConfig; role: CollectionConfig };
  warden: Warden;
  populate: Populate;
  initialize: Initialize;
  initializationExtra?: { user: any; role: any };
};

Additional config options could be easily added to the user and role via this, See factory/spec.ts for details.

  • mute
/**
 * Disable this plugin without uninstalling it.
 */
mute?: boolean;

🔐 Authorization

📚 Collections

Authorizations of Collections are provided by adding access functions to all possible collections, fields...and so on. The said access functions will ask for a specific feature, traits, verbs combination to be present, or otherwise reject the operation it is assigned to.

For example, the expected feature, traits, verbs combination for which an access.create of a collection chad will ask looks like this:

{
  feature: "chad",
  trais: [],
  verbs: ["create"]
}

Any user who has a role with a feature chad and any trait (which, if is left empty in the role editing page, will have a default "_" trait assigned) of verb create can pass the check. As for the access.update of a field age of the collection chad, it looks like this:

{
  feature: "chad",
  trais: ["age"],
  verbs: ["update"]
}

🧭 Globals

Authorizations of Globals are provided the same way as Collections

🌠 Endpoints

Authorizations of Endpoints are provided by prepending check functions in the handler function arrays. The check functions basically do the same as those access functions mentioned above, in which case the verbs are determined by the endpoint's http method as such:

{
  post: "create",
  connect: "read",
  options: "read",
  head: "read",
  get: "read",
  put: "update",
  patch: "update",
  delete: "delete",
}

✏️ Note


You can also choose to skip the authorization for specific collections, globals or some of their fields or endpoints by assigning the custom.warding.should to false or custom.warding.should.[verb] flags of corresponding verbs to false.


Alternatively, if an access function is customly assigned in collections, globals or fields, the authorization will always be skipped but a plugin generated authorization function req.context.warding will be attached to the access function argument. So that plugin users can decide how and when to call the plugin generated authorization function.


The custom.warding.should and custom.warding.should.[verb] flags assigned in fields or endpoints can override those assigned in their corresponding collections and globals.


The plugin generated authorization function req.context.warding will always be attached to the handler function's argument when endpoints have the custom.warding.should flag or custom.warding.should.[verb] flags assigned to false.


Similar plugins