@payload-kit/rbac 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +237 -2
- package/dist/index.d.ts +693 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +770 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gngz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,238 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @payload-kit/rbac
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
High-precision, type-safe **Role-Based Access Control (RBAC)** and Capability engine for **Payload CMS v3**.
|
|
4
|
+
|
|
5
|
+
Decouples business roles from atomic permissions (capabilities), supports role inheritance, domain wildcards (`domain:*`, `*`), and provides logical access combinators (`and`, `or`, `not`) with full support for Payload CMS Row-Level Security (`Where` query conditions).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
- **Decoupled RBAC Architecture**: Define roles as collections of atomic capabilities instead of checking rigid role strings in collection access files.
|
|
12
|
+
- **Atomic Capabilities**: Check fine-grained permissions such as `orders:create`, `orders:read`, `stock:manage`.
|
|
13
|
+
- **Wildcards & Inheritance**: Full support for wildcard permissions (`'*'`), domain prefixes (`'orders:*'`), and multi-level role inheritance without circular dependencies.
|
|
14
|
+
- **Payload RLS Combinators**:
|
|
15
|
+
- `or(...)`: Grants access if any rule returns `true`, merges row-level constraints with `{ or: [...] }`, or denies if all return `false`.
|
|
16
|
+
- `and(...)`: Denies access immediately if any rule returns `false`, merges constraints with `{ and: [...] }`, or allows if all return `true`.
|
|
17
|
+
- `not(...)`: Inverts boolean access decisions cleanly.
|
|
18
|
+
- **High Performance**: Pure in-memory capability resolution with zero extra database round-trips.
|
|
19
|
+
- **100% Type-Safe**: Full TypeScript autocomplete and compile-time type verification.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation & Configuration
|
|
24
|
+
|
|
25
|
+
### 1. Register the Plugin in `payload.config.ts`
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { rbacPlugin } from "@payload-kit/rbac";
|
|
29
|
+
|
|
30
|
+
export const rbac = rbacPlugin({
|
|
31
|
+
// 1. Define roles and their granted capabilities
|
|
32
|
+
roles: {
|
|
33
|
+
customer: {
|
|
34
|
+
label: "Cliente",
|
|
35
|
+
capabilities: ["orders:create", "orders:read-own"],
|
|
36
|
+
},
|
|
37
|
+
operator: {
|
|
38
|
+
label: "Operador de Impressão",
|
|
39
|
+
capabilities: ["orders:read", "orders:update", "stock:manage"],
|
|
40
|
+
inherits: ["customer"], // Inherits customer capabilities
|
|
41
|
+
},
|
|
42
|
+
admin: {
|
|
43
|
+
label: "Administrador",
|
|
44
|
+
capabilities: ["*"], // Wildcard access to all capabilities
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
// 2. Default role for new users
|
|
49
|
+
defaultRole: "customer",
|
|
50
|
+
|
|
51
|
+
// 3. Super-admin role with automatic bypass (defaults to 'admin')
|
|
52
|
+
superAdminRole: "admin",
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Usage Guide
|
|
59
|
+
|
|
60
|
+
### 1. Protecting Collections with Access Guards and Combinators
|
|
61
|
+
|
|
62
|
+
The combinators (`or`, `and`, `not`) seamlessly merge boolean capabilities with Payload CMS Row-Level Security (`Where` query filters). Declarative constant guards (`nobody`, `anyone`) are also provided for common lockouts:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import type { CollectionConfig } from "payload";
|
|
66
|
+
import {
|
|
67
|
+
requireCapability,
|
|
68
|
+
requireRole,
|
|
69
|
+
requireAuth,
|
|
70
|
+
nobody,
|
|
71
|
+
anyone,
|
|
72
|
+
or,
|
|
73
|
+
and,
|
|
74
|
+
} from "@payload-kit/rbac";
|
|
75
|
+
|
|
76
|
+
export const PrintOrders: CollectionConfig = {
|
|
77
|
+
slug: "orders",
|
|
78
|
+
access: {
|
|
79
|
+
// Read: Users with 'orders:read' see all orders; customers only see their own
|
|
80
|
+
read: or(requireCapability("orders:read"), ({ req: { user } }) => ({
|
|
81
|
+
"customer.email": {
|
|
82
|
+
equals: user?.email,
|
|
83
|
+
},
|
|
84
|
+
})),
|
|
85
|
+
|
|
86
|
+
// Create: Requires authentication AND 'orders:create' capability
|
|
87
|
+
create: and(requireAuth(), requireCapability("orders:create")),
|
|
88
|
+
|
|
89
|
+
// Update / Delete: Only users with explicit management capability
|
|
90
|
+
update: requireCapability("orders:update"),
|
|
91
|
+
delete: requireCapability("orders:delete"),
|
|
92
|
+
},
|
|
93
|
+
// ...
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const AuditLogs: CollectionConfig = {
|
|
97
|
+
slug: "audit-logs",
|
|
98
|
+
access: {
|
|
99
|
+
// Immutable collection: completely lock out creation or updates via API
|
|
100
|
+
create: nobody,
|
|
101
|
+
update: nobody,
|
|
102
|
+
delete: requireRole("admin"),
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 2. Protecting Fields (Field-Level Access Control)
|
|
108
|
+
|
|
109
|
+
Protect specific fields against unauthorized reads or privilege escalation using declarative helpers like `readOnlyField()` or granular field guards:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import {
|
|
113
|
+
requireFieldAuth,
|
|
114
|
+
requireFieldCapability,
|
|
115
|
+
requireFieldRole,
|
|
116
|
+
readOnlyField,
|
|
117
|
+
systemOnlyField,
|
|
118
|
+
nobodyField,
|
|
119
|
+
fieldOr,
|
|
120
|
+
} from "@payload-kit/rbac";
|
|
121
|
+
|
|
122
|
+
export const Users = {
|
|
123
|
+
slug: "users",
|
|
124
|
+
fields: [
|
|
125
|
+
{
|
|
126
|
+
name: "role",
|
|
127
|
+
type: "select",
|
|
128
|
+
access: {
|
|
129
|
+
// Prevent privilege escalation: only users with 'users:manage' can modify roles
|
|
130
|
+
update: requireFieldCapability("users:manage"),
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "internalNotes",
|
|
135
|
+
type: "textarea",
|
|
136
|
+
access: {
|
|
137
|
+
read: requireFieldRole(["admin", "operator"]),
|
|
138
|
+
update: requireFieldCapability("users:manage"),
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
// Completely read-only field: can be read, but never created or updated via API
|
|
143
|
+
name: "oidcSub",
|
|
144
|
+
type: "text",
|
|
145
|
+
access: readOnlyField(),
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
// Sensitive system internal field: completely hidden and immutable via public API
|
|
149
|
+
name: "paymentWebhookSecret",
|
|
150
|
+
type: "text",
|
|
151
|
+
access: systemOnlyField(),
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
};
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 3. Functional Checkers in Server / UI Components
|
|
158
|
+
|
|
159
|
+
Check capabilities or roles directly in custom endpoints, hooks, or server actions:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { hasCapability, hasAnyCapability, hasRole } from "@payload-kit/rbac";
|
|
163
|
+
|
|
164
|
+
// Check single capability:
|
|
165
|
+
if (hasCapability(user, "stock:manage")) {
|
|
166
|
+
// Allow spool adjustments
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Check any capability:
|
|
170
|
+
if (hasAnyCapability(user, ["orders:update", "orders:delete"])) {
|
|
171
|
+
// Display administrative toolbar
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Check role:
|
|
175
|
+
if (hasRole(user, "admin")) {
|
|
176
|
+
// Super-admin only operation
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 4. Isolated Instances with `createRbac`
|
|
181
|
+
|
|
182
|
+
If you need a scoped RBAC engine (for example, in a multi-tenant setup or micro-service):
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { createRbac } from "@payload-kit/rbac";
|
|
186
|
+
|
|
187
|
+
const appRbac = createRbac({
|
|
188
|
+
roles: {
|
|
189
|
+
viewer: { capabilities: ["models:view"] },
|
|
190
|
+
designer: { capabilities: ["models:*"], inherits: ["viewer"] },
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const canExport = appRbac.hasCapability(user, "models:export");
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## API Reference
|
|
200
|
+
|
|
201
|
+
### Verifiers
|
|
202
|
+
|
|
203
|
+
- `hasRole(user, role, options?)`: Checks if user has a specific role.
|
|
204
|
+
- `hasAnyRole(user, roles, options?)`: Checks if user has at least one of the roles.
|
|
205
|
+
- `hasAllRoles(user, roles, options?)`: Checks if user has all listed roles.
|
|
206
|
+
- `hasCapability(user, capability, options?)`: Checks if user has capability (evaluating wildcards, inheritance, and super-admin bypass).
|
|
207
|
+
- `hasAnyCapability(user, capabilities, options?)`: Checks if user has at least one capability.
|
|
208
|
+
- `hasAllCapabilities(user, capabilities, options?)`: Checks if user has all listed capabilities.
|
|
209
|
+
|
|
210
|
+
### Collection-Level Access Guards & Combinators
|
|
211
|
+
|
|
212
|
+
- `nobody` / `denyAll`: Access guard that always denies access (`() => false`). Ideal for locking immutable collections.
|
|
213
|
+
- `anyone` / `allowAll`: Access guard that always allows access (`() => true`). Ideal for public collections.
|
|
214
|
+
- `requireAuth()`: Access guard requiring an authenticated user.
|
|
215
|
+
- `requireRole(role | role[])`: Access guard checking user roles.
|
|
216
|
+
- `requireCapability(capability | capability[], options?)`: Access guard checking user capabilities.
|
|
217
|
+
- `or(...rules)`: Logical OR merging boolean checks and Payload `Where` queries.
|
|
218
|
+
- `and(...rules)`: Logical AND merging boolean checks and Payload `Where` queries.
|
|
219
|
+
- `not(rule)`: Logical NOT inverting boolean access decisions.
|
|
220
|
+
|
|
221
|
+
### Field-Level Access Guards & Combinators
|
|
222
|
+
|
|
223
|
+
- `readOnlyField()`: Returns `{ create: nobodyField, update: nobodyField }`, making a field completely immutable via API.
|
|
224
|
+
- `systemOnlyField()`: Returns `{ create: nobodyField, read: nobodyField, update: nobodyField }`, hiding and locking an internal field from public API access.
|
|
225
|
+
- `nobodyField` / `denyField`: Field guard that always denies modification (`() => false`).
|
|
226
|
+
- `anyoneField` / `allowField`: Field guard that always allows modification (`() => true`).
|
|
227
|
+
- `requireFieldAuth()`: Field guard requiring an authenticated user.
|
|
228
|
+
- `requireFieldRole(role | role[])`: Field guard checking user roles.
|
|
229
|
+
- `requireFieldCapability(capability | capability[], options?)`: Field guard checking user capabilities (ideal for privilege escalation prevention on `role` fields).
|
|
230
|
+
- `fieldOr(...rules)`: Logical OR for boolean field checks.
|
|
231
|
+
- `fieldAnd(...rules)`: Logical AND for boolean field checks.
|
|
232
|
+
- `fieldNot(rule)`: Logical NOT for boolean field checks.
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## License
|
|
237
|
+
|
|
238
|
+
MIT © gngz
|