@payload-kit/rbac 0.0.0-stage → 0.1.0-alpha.1
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 +191 -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,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @payload-kit/rbac
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Role-Based Access Control (RBAC) and capability engine for Payload CMS. Maps business roles to atomic capabilities with inheritance, domain wildcards (`orders:*`, `*`), collection and field access guards, and logical combinators (`and`, `or`, `not`) that preserve Payload `Where` query constraints.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @payload-kit/rbac
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependency: `payload` (`^3.0.0`).
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### 1. Register the plugin
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { buildConfig } from "payload";
|
|
19
|
+
import { rbacPlugin } from "@payload-kit/rbac";
|
|
20
|
+
|
|
21
|
+
export default buildConfig({
|
|
22
|
+
plugins: [
|
|
23
|
+
rbacPlugin({
|
|
24
|
+
roles: {
|
|
25
|
+
customer: {
|
|
26
|
+
label: "Customer",
|
|
27
|
+
capabilities: ["orders:create", "orders:read-own"],
|
|
28
|
+
},
|
|
29
|
+
operator: {
|
|
30
|
+
label: "Operator",
|
|
31
|
+
capabilities: ["orders:read", "orders:update", "inventory:manage"],
|
|
32
|
+
inherits: ["customer"],
|
|
33
|
+
},
|
|
34
|
+
admin: {
|
|
35
|
+
label: "Administrator",
|
|
36
|
+
capabilities: ["*"],
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
defaultRole: "customer",
|
|
40
|
+
superAdminRole: "admin",
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`superAdminRole` defaults to `"admin"` and bypasses capability checks. Capability resolution runs in memory with no extra database queries and handles circular inheritance safely.
|
|
47
|
+
|
|
48
|
+
### 2. Protect collections
|
|
49
|
+
|
|
50
|
+
Combinators (`or`, `and`, `not`) merge boolean capability checks with Payload Row-Level Security (`Where`) filters:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import type { CollectionConfig } from "payload";
|
|
54
|
+
import { and, nobody, or, requireAuth, requireCapability, requireRole } from "@payload-kit/rbac";
|
|
55
|
+
|
|
56
|
+
export const Orders: CollectionConfig = {
|
|
57
|
+
slug: "orders",
|
|
58
|
+
access: {
|
|
59
|
+
read: or(requireCapability("orders:read"), ({ req: { user } }) => ({
|
|
60
|
+
"customer.email": { equals: user?.email },
|
|
61
|
+
})),
|
|
62
|
+
create: and(requireAuth(), requireCapability("orders:create")),
|
|
63
|
+
update: requireCapability("orders:update"),
|
|
64
|
+
delete: requireCapability("orders:delete"),
|
|
65
|
+
},
|
|
66
|
+
fields: [],
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const AuditLogs: CollectionConfig = {
|
|
70
|
+
slug: "audit-logs",
|
|
71
|
+
access: {
|
|
72
|
+
create: nobody,
|
|
73
|
+
update: nobody,
|
|
74
|
+
delete: requireRole("admin"),
|
|
75
|
+
},
|
|
76
|
+
fields: [],
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### 3. Protect fields
|
|
81
|
+
|
|
82
|
+
Use field guards to restrict sensitive fields or prevent privilege escalation on role fields:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import {
|
|
86
|
+
readOnlyField,
|
|
87
|
+
requireFieldCapability,
|
|
88
|
+
requireFieldRole,
|
|
89
|
+
systemOnlyField,
|
|
90
|
+
} from "@payload-kit/rbac";
|
|
91
|
+
|
|
92
|
+
export const Users = {
|
|
93
|
+
slug: "users",
|
|
94
|
+
fields: [
|
|
95
|
+
{
|
|
96
|
+
name: "role",
|
|
97
|
+
type: "select",
|
|
98
|
+
access: {
|
|
99
|
+
update: requireFieldCapability("users:manage"),
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "internalNotes",
|
|
104
|
+
type: "textarea",
|
|
105
|
+
access: {
|
|
106
|
+
read: requireFieldRole(["admin", "operator"]),
|
|
107
|
+
update: requireFieldCapability("users:manage"),
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "oidcSub",
|
|
112
|
+
type: "text",
|
|
113
|
+
access: readOnlyField(),
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "webhookSecret",
|
|
117
|
+
type: "text",
|
|
118
|
+
access: systemOnlyField(),
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 4. Check capabilities in server code
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { hasAnyCapability, hasCapability, hasRole } from "@payload-kit/rbac";
|
|
128
|
+
|
|
129
|
+
if (hasCapability(user, "inventory:manage")) {
|
|
130
|
+
// allow stock update
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (hasAnyCapability(user, ["orders:update", "orders:delete"])) {
|
|
134
|
+
// show management actions
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (hasRole(user, "admin")) {
|
|
138
|
+
// admin-only branch
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 5. Isolated instances with `createRbac`
|
|
143
|
+
|
|
144
|
+
When you need an RBAC engine scoped to a specific tenant or module instead of the global registry:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { createRbac } from "@payload-kit/rbac";
|
|
148
|
+
|
|
149
|
+
const appRbac = createRbac({
|
|
150
|
+
roles: {
|
|
151
|
+
viewer: { capabilities: ["reports:view"] },
|
|
152
|
+
editor: { capabilities: ["reports:*"], inherits: ["viewer"] },
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const canExport = appRbac.hasCapability(user, "reports:export");
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## API reference
|
|
160
|
+
|
|
161
|
+
### Role and capability checkers
|
|
162
|
+
|
|
163
|
+
- `hasRole(user, role, options?)`: Checks whether the user has the given role.
|
|
164
|
+
- `hasAnyRole(user, roles, options?)`: Checks whether the user has at least one listed role.
|
|
165
|
+
- `hasAllRoles(user, roles, options?)`: Checks whether the user has every listed role.
|
|
166
|
+
- `hasCapability(user, capability, options?)`: Evaluates wildcards, inheritance, and `superAdminRole`.
|
|
167
|
+
- `hasAnyCapability(user, capabilities, options?)`: Checks whether the user has at least one capability.
|
|
168
|
+
- `hasAllCapabilities(user, capabilities, options?)`: Checks whether the user has every listed capability.
|
|
169
|
+
|
|
170
|
+
### Collection guards and combinators
|
|
171
|
+
|
|
172
|
+
- `nobody` / `denyAll`: Always returns `false`.
|
|
173
|
+
- `anyone` / `allowAll`: Always returns `true`.
|
|
174
|
+
- `requireAuth()`: Requires an authenticated user.
|
|
175
|
+
- `requireRole(role | role[])`: Requires one of the listed roles.
|
|
176
|
+
- `requireCapability(capability | capability[], options?)`: Requires matching capability.
|
|
177
|
+
- `or(...rules)`: Returns `true` if any rule returns `true`, merges `Where` clauses with `{ or: [...] }`, or returns `false` if all deny.
|
|
178
|
+
- `and(...rules)`: Returns `false` if any rule returns `false`, merges `Where` clauses with `{ and: [...] }`, or returns `true` if all allow.
|
|
179
|
+
- `not(rule)`: Inverts a boolean access decision.
|
|
180
|
+
|
|
181
|
+
### Field guards and combinators
|
|
182
|
+
|
|
183
|
+
- `readOnlyField()`: Returns `{ create: nobodyField, update: nobodyField }`.
|
|
184
|
+
- `systemOnlyField()`: Returns `{ create: nobodyField, read: nobodyField, update: nobodyField }`.
|
|
185
|
+
- `nobodyField` / `denyField`: Always returns `false`.
|
|
186
|
+
- `anyoneField` / `allowField`: Always returns `true`.
|
|
187
|
+
- `requireFieldAuth()`, `requireFieldRole(role | role[])`, `requireFieldCapability(capability | capability[], options?)`: Field-level equivalents of the collection guards.
|
|
188
|
+
- `fieldOr(...rules)`, `fieldAnd(...rules)`, `fieldNot(rule)`: Boolean combinators for field access.
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT
|