shefing

abac

by shefing
payload-tools
Roles & permissions
v3
>=3.0.0
Apache-2.0

Overview

Summary generated from the README

Adds an attribute-based access control layer to Payload. Attribute providers such as tenant, role or custom ones like region are compiled into Payload where clauses and reuse data enriched in the user JWT. Collections opt in through a custom abac config. Requires Payload 3 or later.

Features

  • Pluggable attribute providers with match, toWhere and JWT enrichment functions
  • Built-in tenant and role attribute helpers
  • Multi-value attributes such as a tenants array
  • Endpoint that returns the current user's permissions
  • Relationship field filter helper for attribute-aware options
  • Fail-closed behavior and startup config validation

Use cases

  • Restrict documents to the tenant a user belongs to without extra database queries
  • Limit content by region or other custom user attributes
  • Combine attribute rules with role-based permissions in one Payload project

Installation

npm i @shefing/abac

183

Stars

8

Forks

89

Open Issues

3 months ago

Last Updated

8

Weekly Downloads

v0.0.4

npm Version

122.6 KB

Unpacked Size

0

Dependencies

Repository stars

183+4 since Sep 7

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

Shared by 4 plugins in shefing/payload-tools.

npm weekly downloads

90 since Sep 7

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

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/888484677-abac.svg)](https://payloaddirectory.dev/plugins/888484677-abac)
HTML
<a href="https://payloaddirectory.dev/plugins/888484677-abac"><img src="https://payloaddirectory.dev/badge/888484677-abac.svg" alt="Payload Directory"></a>
Updated Aug 18, 2026Created Nov 14, 2024Published May 26, 2026

README

ABAC Plugin

@shefing/abac adds a pluggable, Payload-native attribute-based access control layer.

Goals

  • Keep the implementation simple and Payload-first.
  • Compile attributes into Payload where clauses whenever possible.
  • Reuse JWT-enriched user data instead of introducing a parallel permission store.

Install

pnpm add @shefing/abac

Core contract

import type { AttributeProvider } from '@shefing/abac'

const tenantAttribute: AttributeProvider<string | null> = {
  key: 'tenant',
  fromUser: (user) => (typeof user.tenant === 'string' ? user.tenant : null),
  match: (userValue, docValue) => userValue != null && userValue === docValue,
  toWhere: (userValue) => ({ tenant: { equals: userValue } }),
  enrichJWT: (user) => ({ tenant: user.tenant ?? null }),
}

Tenant example

import { abacPlugin } from '@shefing/abac'

plugins: [
  abacPlugin({
    attributes: [
      {
        key: 'tenant',
        fromUser: (user) => user.tenant,
        match: (userValue, docValue) => userValue === docValue,
        toWhere: (userValue) => ({ tenant: { equals: userValue } }),
        enrichJWT: (user) => ({ tenant: user.tenant }),
      },
    ],
  }),
]

Then opt collections in with custom.abac:

custom: {
  abac: {
    tenant: {
      docField: 'tenant',
    },
  },
}

Geo example

geo is intentionally documented as an example for v1 rather than shipped as a built-in:

const geoAttribute = {
  key: 'geo',
  fromUser: (user) => user.region,
  match: (userRegion, docRegion) => userRegion === docRegion,
  toWhere: (userRegion) => ({ region: { equals: userRegion } }),
}

Multi-value attributes (v0.2 — coming next)

Current limitation (v1): each attribute provider resolves to a single value per user (e.g. user.tenant = 'tenant-a'). If a user belongs to multiple tenants the engine only sees the first one.

The v0.2 milestone will extend the engine and tenantAttribute to handle array-valued attributes natively:

// user.tenants = ['tenant-a', 'tenant-b']  ← multiple memberships

const multiTenantAttribute = {
  key: 'tenant',
  fromUser: (user) => Array.isArray(user.tenants) ? user.tenants : [],
  match: (userTenants, docTenant) =>
    Array.isArray(userTenants) && userTenants.includes(docTenant as string),
  // toWhere will emit { tenant: { in: ['tenant-a', 'tenant-b'] } }
  toWhere: (userTenants) => ({ tenant: { in: userTenants as string[] } }),
  enrichJWT: (user) => ({ tenants: Array.isArray(user.tenants) ? user.tenants : [] }),
}

This closes the main gap vs. Payload's official plugin-multi-tenant, which stores a tenants[] array per user. The same pattern applies to any set-valued attribute (multi-region, multi-clearance, etc.).


Notes

  • v1 keeps policy composition simple: matching providers are combined with implicit AND.
  • Built-in tenantAttribute() and roleAttribute() are exported from the package entry.
  • roleAttribute() is additive with @shefing/authorization: it preserves JWT role context and can short-circuit admins, but it does not replace RBAC collection permissions.
  • See the integration coverage in test-app/tests/e2e-plugins/abac.spec.ts once the feature is fully wired.

Roadmap

VersionItemStatus
v1Single-value attribute providers (tenant, role, custom)✅ Shipped
v1JWT enrichment — zero extra DB queries per request✅ Shipped
v1GET /api/me/permissions endpoint✅ Shipped
v1abacFilterOptions relationship field helper✅ Shipped
v1Fail-closed safety + startup config validation✅ Shipped
v0.2Multi-value attribute support (tenants[], { in: [...] } WHERE, array match)✅ Shipped
v0.2readVersions / unlock action guards✅ Shipped
v0.2pluginContext singleton → factory (test isolation)✅ Shipped
v0.3Optional admin UI: tenant switcher component💡 Planned
v0.3isGlobal collection mode (per-tenant globals)💡 Planned
v0.3Per-tenant roles (tenantRoleAttribute)💡 Planned
v0.3i18n support💡 Planned

Similar plugins