@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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Payload CMS Role-Based Access Control (RBAC) & Capability Plugin.
|
|
3
|
+
|
|
4
|
+
Provides a type-safe, declarative capability engine and Payload Access combinators.
|
|
5
|
+
|
|
6
|
+
@packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import { Access, AccessArgs, FieldAccess, FieldAccess as FieldAccess$1, FieldAccessArgs, FieldAccessArgs as FieldAccessArgs$1, Plugin, TypeWithID, TypeWithID as TypeWithID$1, Where } from "payload";
|
|
9
|
+
/**
|
|
10
|
+
Combines multiple access rules with logical OR.
|
|
11
|
+
|
|
12
|
+
Evaluation semantics:
|
|
13
|
+
1. If any rule evaluates to `true` (unrestricted access), access is granted immediately (`true`).
|
|
14
|
+
2. If any rules return `Where` query constraints (row-level security filters):
|
|
15
|
+
- If exactly one `Where` constraint exists, it is returned directly.
|
|
16
|
+
- If multiple `Where` constraints exist, they are merged via `{ or: [...] }`.
|
|
17
|
+
3. If all rules evaluate to `false`, access is denied (`false`).
|
|
18
|
+
|
|
19
|
+
@param rules - One or more Payload Access rules to evaluate.
|
|
20
|
+
@returns A composite Payload Access rule.
|
|
21
|
+
|
|
22
|
+
@example
|
|
23
|
+
```typescript
|
|
24
|
+
// Grant access if user has orders:read_all capability, OR only to their own orders:
|
|
25
|
+
access: {
|
|
26
|
+
read: or(
|
|
27
|
+
requireCapability('orders:read_all'),
|
|
28
|
+
({ req: { user } }) => ({ 'customer.email': { equals: user?.email } })
|
|
29
|
+
),
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
*/
|
|
33
|
+
export declare function or<TData = unknown>(...rules: readonly Access<TData>[]): Access<TData>;
|
|
34
|
+
/**
|
|
35
|
+
Combines multiple access rules with logical AND.
|
|
36
|
+
|
|
37
|
+
Evaluation semantics:
|
|
38
|
+
1. If any rule evaluates to `false` (denial), access is denied immediately (`false`).
|
|
39
|
+
2. If any rules return `Where` query constraints:
|
|
40
|
+
- If exactly one `Where` constraint exists, it is returned directly.
|
|
41
|
+
- If multiple `Where` constraints exist, they are merged via `{ and: [...] }`.
|
|
42
|
+
3. If all rules evaluate to `true` (and no `Where` constraints are returned), access is granted (`true`).
|
|
43
|
+
|
|
44
|
+
@param rules - One or more Payload Access rules to evaluate.
|
|
45
|
+
@returns A composite Payload Access rule.
|
|
46
|
+
|
|
47
|
+
@example
|
|
48
|
+
```typescript
|
|
49
|
+
// User must be authenticated AND have the capability to create:
|
|
50
|
+
access: {
|
|
51
|
+
create: and(
|
|
52
|
+
requireAuth(),
|
|
53
|
+
requireCapability('orders:create')
|
|
54
|
+
),
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
*/
|
|
58
|
+
export declare function and<TData = unknown>(...rules: readonly Access<TData>[]): Access<TData>;
|
|
59
|
+
/**
|
|
60
|
+
Inverts the outcome of a boolean access rule.
|
|
61
|
+
|
|
62
|
+
Note: Negating a Payload `Where` query condition is not supported because database-level
|
|
63
|
+
inverted filters cannot be generically inferred without schema reflection. For `Where` filters,
|
|
64
|
+
specify explicit query constraints instead.
|
|
65
|
+
|
|
66
|
+
@param rule - A boolean-returning Payload Access rule.
|
|
67
|
+
@returns An inverted Payload Access rule.
|
|
68
|
+
@throws {TypeError} If the evaluated rule returns a Where query object instead of a boolean.
|
|
69
|
+
|
|
70
|
+
@example
|
|
71
|
+
```typescript
|
|
72
|
+
access: {
|
|
73
|
+
delete: not(requireRole('customer')),
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
*/
|
|
77
|
+
export declare function not<TData = unknown>(rule: Access<TData>): Access<TData>;
|
|
78
|
+
/**
|
|
79
|
+
Combines multiple field-level access rules with logical OR.
|
|
80
|
+
|
|
81
|
+
Evaluation semantics:
|
|
82
|
+
1. If any rule evaluates to `true`, access to the field is granted (`true`).
|
|
83
|
+
2. If all rules evaluate to `false`, access to the field is denied (`false`).
|
|
84
|
+
3. If no rules are provided, fails closed by returning `false`.
|
|
85
|
+
|
|
86
|
+
@param rules - One or more FieldAccess rules to evaluate.
|
|
87
|
+
@returns A composite FieldAccess rule.
|
|
88
|
+
|
|
89
|
+
@example
|
|
90
|
+
```typescript
|
|
91
|
+
{
|
|
92
|
+
name: 'contactPhone',
|
|
93
|
+
type: 'text',
|
|
94
|
+
access: {
|
|
95
|
+
read: fieldOr(
|
|
96
|
+
requireFieldCapability('users:read_sensitive'),
|
|
97
|
+
({ req: { user }, doc }) => user?.id === doc?.id
|
|
98
|
+
),
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
*/
|
|
103
|
+
export declare function fieldOr<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(...rules: readonly FieldAccess$1<TData, TSiblingData>[]): FieldAccess$1<TData, TSiblingData>;
|
|
104
|
+
/**
|
|
105
|
+
Combines multiple field-level access rules with logical AND.
|
|
106
|
+
|
|
107
|
+
Evaluation semantics:
|
|
108
|
+
1. If any rule evaluates to `false`, access to the field is denied (`false`).
|
|
109
|
+
2. If all rules evaluate to `true`, access to the field is granted (`true`).
|
|
110
|
+
3. If no rules are provided, returns `true`.
|
|
111
|
+
|
|
112
|
+
@param rules - One or more FieldAccess rules to evaluate.
|
|
113
|
+
@returns A composite FieldAccess rule.
|
|
114
|
+
|
|
115
|
+
@example
|
|
116
|
+
```typescript
|
|
117
|
+
{
|
|
118
|
+
name: 'billingAddress',
|
|
119
|
+
type: 'text',
|
|
120
|
+
access: {
|
|
121
|
+
update: fieldAnd(
|
|
122
|
+
requireFieldAuth(),
|
|
123
|
+
requireFieldCapability('orders:update')
|
|
124
|
+
),
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
*/
|
|
129
|
+
export declare function fieldAnd<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(...rules: readonly FieldAccess$1<TData, TSiblingData>[]): FieldAccess$1<TData, TSiblingData>;
|
|
130
|
+
/**
|
|
131
|
+
Inverts the outcome of a field-level access rule.
|
|
132
|
+
|
|
133
|
+
@param rule - A FieldAccess rule to invert.
|
|
134
|
+
@returns An inverted FieldAccess rule.
|
|
135
|
+
|
|
136
|
+
@example
|
|
137
|
+
```typescript
|
|
138
|
+
{
|
|
139
|
+
name: 'guestToken',
|
|
140
|
+
type: 'text',
|
|
141
|
+
access: {
|
|
142
|
+
read: fieldNot(requireFieldRole('customer')),
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
*/
|
|
147
|
+
export declare function fieldNot<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(rule: FieldAccess$1<TData, TSiblingData>): FieldAccess$1<TData, TSiblingData>;
|
|
148
|
+
/**
|
|
149
|
+
Wildcard capability tokens.
|
|
150
|
+
- `'*'`: grants access to all capabilities.
|
|
151
|
+
- `${string}:*`: grants access to all actions within a domain (e.g. `'orders:*'`).
|
|
152
|
+
*/
|
|
153
|
+
type WildcardCapability = "*" | `${string}:*`;
|
|
154
|
+
/**
|
|
155
|
+
Definition of a single role including its granted capabilities, optional label,
|
|
156
|
+
and parent roles from which it inherits capabilities.
|
|
157
|
+
*/
|
|
158
|
+
interface RoleDefinition<TCapability extends string = string, TRole extends string = string> {
|
|
159
|
+
/**
|
|
160
|
+
Human-readable label for admin UI or displays, supporting plain string or i18n dictionary.
|
|
161
|
+
*/
|
|
162
|
+
readonly label?: string | Record<string, string>;
|
|
163
|
+
/**
|
|
164
|
+
Optional description explaining the responsibilities of this role.
|
|
165
|
+
*/
|
|
166
|
+
readonly description?: string;
|
|
167
|
+
/**
|
|
168
|
+
Capabilities directly assigned to this role.
|
|
169
|
+
Supports specific capabilities, domain wildcards (`'orders:*'`), and full wildcard (`'*'`).
|
|
170
|
+
*/
|
|
171
|
+
readonly capabilities: readonly (TCapability | WildcardCapability)[];
|
|
172
|
+
/**
|
|
173
|
+
Other roles whose capabilities are inherited by this role.
|
|
174
|
+
*/
|
|
175
|
+
readonly inherits?: readonly NoInfer<TRole>[];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
Dictionary mapping role identifiers to their definitions.
|
|
179
|
+
*/
|
|
180
|
+
type RolesConfig<TRole extends string = string, TCapability extends string = string> = Record<TRole, RoleDefinition<TCapability, NoInfer<TRole>>>;
|
|
181
|
+
/**
|
|
182
|
+
Generic representation of a user entity with potential role and capability assignments.
|
|
183
|
+
*/
|
|
184
|
+
interface UserLike {
|
|
185
|
+
readonly id?: string | number;
|
|
186
|
+
readonly email?: string | null;
|
|
187
|
+
readonly role?: string | null;
|
|
188
|
+
readonly roles?: readonly (string | null | undefined)[] | null;
|
|
189
|
+
readonly capabilities?: readonly string[] | null;
|
|
190
|
+
readonly [key: string]: unknown;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
Options used to customize the behavior of capability checks.
|
|
194
|
+
*/
|
|
195
|
+
interface CapabilityCheckOptions<TRole extends string = string, TCapability extends string = string> {
|
|
196
|
+
/**
|
|
197
|
+
Custom roles configuration. If omitted, the globally registered configuration is used.
|
|
198
|
+
*/
|
|
199
|
+
readonly roles?: RolesConfig<TRole, TCapability>;
|
|
200
|
+
/**
|
|
201
|
+
Name of the role that automatically bypasses capability checks (grants all capabilities).
|
|
202
|
+
@default 'admin'
|
|
203
|
+
*/
|
|
204
|
+
readonly superAdminRole?: TRole | null;
|
|
205
|
+
/**
|
|
206
|
+
Name of the role representing unauthenticated/anonymous visitors.
|
|
207
|
+
If provided and user is null/undefined, resolves capabilities from this role.
|
|
208
|
+
@default 'public'
|
|
209
|
+
*/
|
|
210
|
+
readonly anonymousRole?: TRole | null;
|
|
211
|
+
/**
|
|
212
|
+
Name of the field on the user object storing their role(s).
|
|
213
|
+
@default 'role'
|
|
214
|
+
*/
|
|
215
|
+
readonly rolesFieldName?: string;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
Options used to customize the behavior of role checks.
|
|
219
|
+
*/
|
|
220
|
+
interface RoleCheckOptions {
|
|
221
|
+
/**
|
|
222
|
+
Name of the field on the user object storing their role(s).
|
|
223
|
+
@default 'role'
|
|
224
|
+
*/
|
|
225
|
+
readonly rolesFieldName?: string;
|
|
226
|
+
/**
|
|
227
|
+
Name of the role representing unauthenticated/anonymous visitors.
|
|
228
|
+
If provided and user is null/undefined, resolveUserRoles returns this role.
|
|
229
|
+
@default 'public'
|
|
230
|
+
*/
|
|
231
|
+
readonly anonymousRole?: string | null;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
Configuration options for the RBAC plugin.
|
|
235
|
+
*/
|
|
236
|
+
interface RbacPluginOptions<TRole extends string = string, TCapability extends string = string> {
|
|
237
|
+
/**
|
|
238
|
+
Map of roles and their capability sets.
|
|
239
|
+
*/
|
|
240
|
+
readonly roles: RolesConfig<TRole, TCapability>;
|
|
241
|
+
/**
|
|
242
|
+
Optional exhaustive list of valid capabilities for verification or documentation.
|
|
243
|
+
*/
|
|
244
|
+
readonly capabilities?: readonly TCapability[];
|
|
245
|
+
/**
|
|
246
|
+
Default role to assign to new users when not explicitly specified.
|
|
247
|
+
*/
|
|
248
|
+
readonly defaultRole?: NoInfer<TRole>;
|
|
249
|
+
/**
|
|
250
|
+
Role that bypasses all capability checks (has wildcard access).
|
|
251
|
+
Set to `null` to disable super-admin bypass.
|
|
252
|
+
@default 'admin'
|
|
253
|
+
*/
|
|
254
|
+
readonly superAdminRole?: NoInfer<TRole> | null;
|
|
255
|
+
/**
|
|
256
|
+
Role representing unauthenticated/anonymous public visitors.
|
|
257
|
+
When user is null/undefined, this role's capabilities are granted.
|
|
258
|
+
@default 'public'
|
|
259
|
+
*/
|
|
260
|
+
readonly anonymousRole?: NoInfer<TRole> | null;
|
|
261
|
+
/**
|
|
262
|
+
Slug of the user collection to integrate with.
|
|
263
|
+
@default 'users'
|
|
264
|
+
*/
|
|
265
|
+
readonly userCollectionSlug?: string;
|
|
266
|
+
/**
|
|
267
|
+
Name of the field in the user collection storing the role (or roles).
|
|
268
|
+
@default 'role'
|
|
269
|
+
*/
|
|
270
|
+
readonly rolesFieldName?: string;
|
|
271
|
+
/**
|
|
272
|
+
Whether to automatically synchronize the user collection's role field options
|
|
273
|
+
with the roles configured in this plugin.
|
|
274
|
+
@default false
|
|
275
|
+
*/
|
|
276
|
+
readonly syncUserRolesField?: boolean;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
Typed Payload Access function type alias for convenient readability.
|
|
280
|
+
*/
|
|
281
|
+
type RbacAccess<TData = unknown> = Access<TData>;
|
|
282
|
+
/**
|
|
283
|
+
Payload Access argument bundle for readability.
|
|
284
|
+
*/
|
|
285
|
+
type RbacAccessArguments<TData = unknown> = AccessArgs<TData>;
|
|
286
|
+
/**
|
|
287
|
+
Typed Payload FieldAccess function type alias.
|
|
288
|
+
*/
|
|
289
|
+
type RbacFieldAccess<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown> = FieldAccess$1<TData, TSiblingData>;
|
|
290
|
+
/**
|
|
291
|
+
Payload FieldAccess argument bundle.
|
|
292
|
+
*/
|
|
293
|
+
type RbacFieldAccessArguments<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown> = FieldAccessArgs$1<TData, TSiblingData>;
|
|
294
|
+
/**
|
|
295
|
+
Payload Where query condition.
|
|
296
|
+
*/
|
|
297
|
+
type RbacWhere = Where;
|
|
298
|
+
/**
|
|
299
|
+
Additional options when checking capabilities via `requireCapability` or `requireFieldCapability`.
|
|
300
|
+
*/
|
|
301
|
+
interface RequireCapabilityOptions<TRole extends string = string, TCapability extends string = string> extends CapabilityCheckOptions<TRole, TCapability> {
|
|
302
|
+
/**
|
|
303
|
+
When an array of capabilities is provided:
|
|
304
|
+
- `'any'`: The user must satisfy at least one capability. (Default)
|
|
305
|
+
- `'all'`: The user must satisfy all provided capabilities.
|
|
306
|
+
@default 'any'
|
|
307
|
+
*/
|
|
308
|
+
readonly mode?: "any" | "all";
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
Creates a field-level access rule requiring that the requesting client is an authenticated user.
|
|
312
|
+
|
|
313
|
+
@returns A Payload FieldAccess function returning true if user is authenticated, false otherwise.
|
|
314
|
+
*/
|
|
315
|
+
export declare function requireFieldAuth<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(): FieldAccess$1<TData, TSiblingData>;
|
|
316
|
+
/**
|
|
317
|
+
Creates a field-level access rule requiring that the requesting user possesses one or more specified roles.
|
|
318
|
+
|
|
319
|
+
@param role - A single role name or array of acceptable roles.
|
|
320
|
+
@param options - Custom options for role extraction.
|
|
321
|
+
@returns A Payload FieldAccess rule returning boolean.
|
|
322
|
+
*/
|
|
323
|
+
export declare function requireFieldRole<TRole extends string = string, TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(role: TRole | readonly TRole[], options?: RoleCheckOptions): FieldAccess$1<TData, TSiblingData>;
|
|
324
|
+
/**
|
|
325
|
+
Creates a field-level access rule requiring that the requesting user possesses one or more specified capabilities.
|
|
326
|
+
|
|
327
|
+
@param capability - A single capability or array of capabilities.
|
|
328
|
+
@param options - Options for capability verification, including matching mode (`'any'` | `'all'`).
|
|
329
|
+
@returns A Payload FieldAccess rule returning boolean.
|
|
330
|
+
*/
|
|
331
|
+
export declare function requireFieldCapability<TCapability extends string = string, TRole extends string = string, TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(capability: TCapability | readonly TCapability[], options?: RequireCapabilityOptions<TRole, TCapability>): FieldAccess$1<TData, TSiblingData>;
|
|
332
|
+
/**
|
|
333
|
+
Universal field access rule that strictly denies field access to everyone (public & authenticated).
|
|
334
|
+
|
|
335
|
+
@returns Always returns `false`.
|
|
336
|
+
*/
|
|
337
|
+
export declare const nobodyField: FieldAccess$1;
|
|
338
|
+
/**
|
|
339
|
+
Universal field access rule that permits field access to everyone.
|
|
340
|
+
|
|
341
|
+
@returns Always returns `true`.
|
|
342
|
+
*/
|
|
343
|
+
export declare const anyoneField: FieldAccess$1;
|
|
344
|
+
/**
|
|
345
|
+
Functional alias for `nobodyField`.
|
|
346
|
+
*/
|
|
347
|
+
export declare const denyField: FieldAccess$1;
|
|
348
|
+
/**
|
|
349
|
+
Functional alias for `anyoneField`.
|
|
350
|
+
*/
|
|
351
|
+
export declare const allowField: FieldAccess$1;
|
|
352
|
+
/**
|
|
353
|
+
Configures field access for a strictly read-only field (cannot be created or updated via API).
|
|
354
|
+
The field remains readable according to the collection's read access rules, but API writes are denied.
|
|
355
|
+
|
|
356
|
+
@returns Field access object with create and update set to `nobodyField`.
|
|
357
|
+
*/
|
|
358
|
+
export declare function readOnlyField<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(): {
|
|
359
|
+
create: FieldAccess$1<TData, TSiblingData>;
|
|
360
|
+
update: FieldAccess$1<TData, TSiblingData>;
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
Configures field access for an internal system field that cannot be read, created, or updated via public API.
|
|
364
|
+
Only local server operations (with `overrideAccess: true` or internal services) can access or modify it.
|
|
365
|
+
|
|
366
|
+
@returns Field access object with create, read, and update set to `nobodyField`.
|
|
367
|
+
*/
|
|
368
|
+
export declare function systemOnlyField<TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(): {
|
|
369
|
+
create: FieldAccess$1<TData, TSiblingData>;
|
|
370
|
+
read: FieldAccess$1<TData, TSiblingData>;
|
|
371
|
+
update: FieldAccess$1<TData, TSiblingData>;
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
Creates an access rule requiring that the requesting client is an authenticated user.
|
|
375
|
+
|
|
376
|
+
@returns A Payload Access function returning true if user is present, false otherwise.
|
|
377
|
+
|
|
378
|
+
@example
|
|
379
|
+
```typescript
|
|
380
|
+
export const Orders = {
|
|
381
|
+
access: {
|
|
382
|
+
create: requireAuth(),
|
|
383
|
+
},
|
|
384
|
+
};
|
|
385
|
+
```
|
|
386
|
+
*/
|
|
387
|
+
export declare function requireAuth<TData = unknown>(): Access<TData>;
|
|
388
|
+
/**
|
|
389
|
+
Creates an access rule requiring that the requesting user possesses one or more specified roles.
|
|
390
|
+
|
|
391
|
+
@param role - A single role name or array of acceptable roles.
|
|
392
|
+
@param options - Custom options for role extraction.
|
|
393
|
+
@returns A Payload Access rule returning boolean.
|
|
394
|
+
|
|
395
|
+
@example
|
|
396
|
+
```typescript
|
|
397
|
+
access: {
|
|
398
|
+
delete: requireRole('admin'),
|
|
399
|
+
update: requireRole(['admin', 'operator']),
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
*/
|
|
403
|
+
export declare function requireRole<TRole extends string = string, TData = unknown>(role: TRole | readonly TRole[], options?: RoleCheckOptions): Access<TData>;
|
|
404
|
+
/**
|
|
405
|
+
Creates an access rule requiring that the requesting user possesses one or more specified capabilities.
|
|
406
|
+
|
|
407
|
+
@param capability - A single capability or array of capabilities.
|
|
408
|
+
@param options - Options for capability verification, including matching mode (`'any'` | `'all'`).
|
|
409
|
+
@returns A Payload Access rule returning boolean.
|
|
410
|
+
|
|
411
|
+
@example
|
|
412
|
+
```typescript
|
|
413
|
+
access: {
|
|
414
|
+
read: requireCapability('orders:read'),
|
|
415
|
+
update: requireCapability(['orders:update', 'orders:manage'], { mode: 'any' }),
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
*/
|
|
419
|
+
export declare function requireCapability<TCapability extends string = string, TRole extends string = string, TData = unknown>(capability: TCapability | readonly TCapability[], options?: RequireCapabilityOptions<TRole, TCapability>): Access<TData>;
|
|
420
|
+
/**
|
|
421
|
+
Universal access rule that strictly denies collection or global access to everyone (public & authenticated).
|
|
422
|
+
Ideal for immutable collections, disabled actions (e.g. `delete: nobody`), or server-only models.
|
|
423
|
+
|
|
424
|
+
@returns Always returns `false`.
|
|
425
|
+
*/
|
|
426
|
+
export declare const nobody: Access;
|
|
427
|
+
/**
|
|
428
|
+
Universal access rule that permits public access to everyone.
|
|
429
|
+
Ideal for public-facing collections, marketing pages, site settings, or public legal documents.
|
|
430
|
+
|
|
431
|
+
@returns Always returns `true`.
|
|
432
|
+
*/
|
|
433
|
+
export declare const anyone: Access;
|
|
434
|
+
/**
|
|
435
|
+
Functional alias for `nobody`.
|
|
436
|
+
*/
|
|
437
|
+
export declare const denyAll: Access;
|
|
438
|
+
/**
|
|
439
|
+
Functional alias for `anyone`.
|
|
440
|
+
*/
|
|
441
|
+
export declare const allowAll: Access;
|
|
442
|
+
/**
|
|
443
|
+
Evaluates whether a granted capability pattern satisfies a requested capability.
|
|
444
|
+
|
|
445
|
+
Supported pattern formats:
|
|
446
|
+
- `'*'`: Global wildcard, matches any requested capability.
|
|
447
|
+
- `'domain:*'`: Domain wildcard, matches any capability starting with `'domain:'` (e.g., `'orders:*'` matches `'orders:create'`).
|
|
448
|
+
- `'exact:match'`: Exact match against requested capability.
|
|
449
|
+
|
|
450
|
+
@param grantedPattern - The capability or wildcard pattern possessed by the role or user.
|
|
451
|
+
@param requestedCapability - The specific capability required for an operation.
|
|
452
|
+
@returns True if the pattern satisfies the requested capability, false otherwise.
|
|
453
|
+
|
|
454
|
+
@example
|
|
455
|
+
```typescript
|
|
456
|
+
matchesCapability('*', 'orders:delete'); // true
|
|
457
|
+
matchesCapability('orders:*', 'orders:delete'); // true
|
|
458
|
+
matchesCapability('orders:read', 'orders:delete'); // false
|
|
459
|
+
```
|
|
460
|
+
*/
|
|
461
|
+
export declare function doesMatchCapability(grantedPattern: string, requestedCapability: string): boolean;
|
|
462
|
+
/**
|
|
463
|
+
Creates and initializes the Payload CMS RBAC (Role-Based Access Control) plugin.
|
|
464
|
+
|
|
465
|
+
@param options - Configuration options defining roles, capabilities, and defaults.
|
|
466
|
+
@returns A Payload plugin function.
|
|
467
|
+
|
|
468
|
+
@example
|
|
469
|
+
```typescript
|
|
470
|
+
// In payload.config.ts:
|
|
471
|
+
export default buildConfig({
|
|
472
|
+
plugins: [
|
|
473
|
+
rbacPlugin({
|
|
474
|
+
roles: {
|
|
475
|
+
admin: { capabilities: ['*'], label: 'Administrador' },
|
|
476
|
+
operator: { capabilities: ['orders:read', 'orders:update'], label: 'Operador' },
|
|
477
|
+
customer: { capabilities: ['orders:create'], label: 'Cliente' },
|
|
478
|
+
},
|
|
479
|
+
defaultRole: 'customer',
|
|
480
|
+
superAdminRole: 'admin',
|
|
481
|
+
}),
|
|
482
|
+
],
|
|
483
|
+
});
|
|
484
|
+
```
|
|
485
|
+
*/
|
|
486
|
+
export declare function rbacPlugin<TRole extends string = string, TCapability extends string = string>(options: RbacPluginOptions<TRole, TCapability>): Plugin;
|
|
487
|
+
/**
|
|
488
|
+
Creates an isolated, type-safe suite of RBAC helper functions bound to a specific configuration.
|
|
489
|
+
|
|
490
|
+
@param options - Configuration options for the isolated instance.
|
|
491
|
+
@returns An object containing bound checkers, access guards, and combinators.
|
|
492
|
+
|
|
493
|
+
@example
|
|
494
|
+
```typescript
|
|
495
|
+
const rbac = createRbac({
|
|
496
|
+
roles: {
|
|
497
|
+
admin: { capabilities: ['*'] },
|
|
498
|
+
editor: { capabilities: ['articles:*'] },
|
|
499
|
+
},
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
const canEdit = rbac.hasCapability(user, 'articles:publish');
|
|
503
|
+
```
|
|
504
|
+
*/
|
|
505
|
+
export declare function createRbac<TRole extends string = string, TCapability extends string = string>(options: RbacPluginOptions<TRole, TCapability>): {
|
|
506
|
+
options: RbacPluginOptions<TRole, TCapability>;
|
|
507
|
+
plugin: () => Plugin;
|
|
508
|
+
resolveRoles: (user: UserLike | null | undefined) => readonly string[];
|
|
509
|
+
hasRole: (user: UserLike | null | undefined, role: TRole | (string & {}), checkOptions?: RoleCheckOptions) => boolean;
|
|
510
|
+
hasAnyRole: (user: UserLike | null | undefined, roles: readonly (TRole | (string & {}))[], checkOptions?: RoleCheckOptions) => boolean;
|
|
511
|
+
hasAllRoles: (user: UserLike | null | undefined, roles: readonly (TRole | (string & {}))[], checkOptions?: RoleCheckOptions) => boolean;
|
|
512
|
+
hasCapability: (user: UserLike | null | undefined, capability: TCapability | (string & {}), checkOptions?: CapabilityCheckOptions<TRole, TCapability>) => boolean;
|
|
513
|
+
hasAnyCapability: (user: UserLike | null | undefined, capabilities: readonly (TCapability | (string & {}))[], checkOptions?: CapabilityCheckOptions<TRole, TCapability>) => boolean;
|
|
514
|
+
hasAllCapabilities: (user: UserLike | null | undefined, capabilities: readonly (TCapability | (string & {}))[], checkOptions?: CapabilityCheckOptions<TRole, TCapability>) => boolean;
|
|
515
|
+
requireAuth: typeof requireAuth;
|
|
516
|
+
requireRole: <TData = unknown>(role: TRole | readonly TRole[], checkOptions?: RoleCheckOptions) => import("payload").Access<TData>;
|
|
517
|
+
requireCapability: <TData = unknown>(capability: TCapability | readonly TCapability[], guardOptions?: CapabilityCheckOptions<TRole, TCapability> & {
|
|
518
|
+
mode?: "any" | "all";
|
|
519
|
+
}) => import("payload").Access<TData>;
|
|
520
|
+
requireFieldAuth: typeof requireFieldAuth;
|
|
521
|
+
requireFieldRole: <TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(role: TRole | readonly TRole[], checkOptions?: RoleCheckOptions) => import("payload").FieldAccess<TData, TSiblingData>;
|
|
522
|
+
requireFieldCapability: <TData extends TypeWithID$1 = TypeWithID$1, TSiblingData = unknown>(capability: TCapability | readonly TCapability[], guardOptions?: CapabilityCheckOptions<TRole, TCapability> & {
|
|
523
|
+
mode?: "any" | "all";
|
|
524
|
+
}) => import("payload").FieldAccess<TData, TSiblingData>;
|
|
525
|
+
and: typeof and;
|
|
526
|
+
or: typeof or;
|
|
527
|
+
not: typeof not;
|
|
528
|
+
fieldAnd: typeof fieldAnd;
|
|
529
|
+
fieldOr: typeof fieldOr;
|
|
530
|
+
fieldNot: typeof fieldNot;
|
|
531
|
+
doesMatchCapability: typeof doesMatchCapability;
|
|
532
|
+
resolveRoleCapabilities: (role: TRole) => Set<string>;
|
|
533
|
+
resolveUserCapabilities: (user: UserLike | null | undefined) => Set<string>;
|
|
534
|
+
};
|
|
535
|
+
/**
|
|
536
|
+
Registers an RBAC configuration into the active runtime context.
|
|
537
|
+
|
|
538
|
+
@param config - The RBAC configuration options to activate.
|
|
539
|
+
*/
|
|
540
|
+
export declare function registerRbacConfig<TRole extends string = string, TCapability extends string = string>(config: RbacPluginOptions<TRole, TCapability>): void;
|
|
541
|
+
/**
|
|
542
|
+
Retrieves the currently registered RBAC configuration.
|
|
543
|
+
|
|
544
|
+
@returns The active configuration, or undefined if not yet configured.
|
|
545
|
+
*/
|
|
546
|
+
export declare function getRbacConfig<TRole extends string = string, TCapability extends string = string>(): RbacPluginOptions<TRole, TCapability> | undefined;
|
|
547
|
+
/**
|
|
548
|
+
Clears the active RBAC configuration. Primarily utilized in test suites to prevent leaks.
|
|
549
|
+
*/
|
|
550
|
+
export declare function resetRbacConfig(): void;
|
|
551
|
+
/**
|
|
552
|
+
Extracts and normalizes the assigned roles of a user into an array of string identifiers.
|
|
553
|
+
|
|
554
|
+
@param user - The user object to inspect, or null/undefined.
|
|
555
|
+
@param rolesFieldName - Name of the field containing the role(s). Defaults to plugin config or `'role'`.
|
|
556
|
+
@returns An array of trimmed, non-empty role identifiers.
|
|
557
|
+
|
|
558
|
+
@example
|
|
559
|
+
```typescript
|
|
560
|
+
resolveUserRoles({ role: 'admin' }); // ['admin']
|
|
561
|
+
resolveUserRoles({ roles: ['editor', 'operator'] }); // ['editor', 'operator']
|
|
562
|
+
resolveUserRoles(null); // []
|
|
563
|
+
```
|
|
564
|
+
*/
|
|
565
|
+
export declare function resolveUserRoles(user?: UserLike | null, optionsOrFieldName?: RoleCheckOptions | string): readonly string[];
|
|
566
|
+
/**
|
|
567
|
+
Verifies whether a user has been assigned a specific role.
|
|
568
|
+
|
|
569
|
+
@param user - The user to evaluate.
|
|
570
|
+
@param role - The expected role identifier.
|
|
571
|
+
@param options - Optional overrides for role extraction.
|
|
572
|
+
@returns True if the user has the specified role, false otherwise.
|
|
573
|
+
|
|
574
|
+
@example
|
|
575
|
+
```typescript
|
|
576
|
+
if (hasRole(user, 'admin')) {
|
|
577
|
+
// Authorized for administrator role
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
*/
|
|
581
|
+
export declare function hasRole<TRole extends string = string>(user: UserLike | null | undefined, role: TRole, options?: RoleCheckOptions): boolean;
|
|
582
|
+
/**
|
|
583
|
+
Verifies whether a user has at least one of the specified roles.
|
|
584
|
+
|
|
585
|
+
@param user - The user to evaluate.
|
|
586
|
+
@param roles - The list of acceptable roles.
|
|
587
|
+
@param options - Optional overrides for role extraction.
|
|
588
|
+
@returns True if the user has at least one matching role, false otherwise.
|
|
589
|
+
|
|
590
|
+
@example
|
|
591
|
+
```typescript
|
|
592
|
+
if (hasAnyRole(user, ['admin', 'manager'])) {
|
|
593
|
+
// Authorized for either admin or manager
|
|
594
|
+
}
|
|
595
|
+
```
|
|
596
|
+
*/
|
|
597
|
+
export declare function hasAnyRole<TRole extends string = string>(user: UserLike | null | undefined, roles: readonly TRole[], options?: RoleCheckOptions): boolean;
|
|
598
|
+
/**
|
|
599
|
+
Verifies whether a user has all of the specified roles.
|
|
600
|
+
|
|
601
|
+
@param user - The user to evaluate.
|
|
602
|
+
@param roles - The list of required roles.
|
|
603
|
+
@param options - Optional overrides for role extraction.
|
|
604
|
+
@returns True if the user possesses every required role, false otherwise.
|
|
605
|
+
|
|
606
|
+
@example
|
|
607
|
+
```typescript
|
|
608
|
+
if (hasAllRoles(user, ['verified', 'subscriber'])) {
|
|
609
|
+
// Authorized only if both roles are present
|
|
610
|
+
}
|
|
611
|
+
```
|
|
612
|
+
*/
|
|
613
|
+
export declare function hasAllRoles<TRole extends string = string>(user: UserLike | null | undefined, roles: readonly TRole[], options?: RoleCheckOptions): boolean;
|
|
614
|
+
/**
|
|
615
|
+
Checks whether a user possesses the required capability, either directly, through assigned roles,
|
|
616
|
+
role inheritance, wildcard patterns, anonymous/public role, or super-admin privilege.
|
|
617
|
+
|
|
618
|
+
@param user - The user to evaluate, or null/undefined for anonymous visitors.
|
|
619
|
+
@param capability - The specific capability string required (e.g., `'site:read'`).
|
|
620
|
+
@param options - Custom options to override roles configuration or super-admin role.
|
|
621
|
+
@returns True if the user has the capability, false otherwise.
|
|
622
|
+
|
|
623
|
+
@example
|
|
624
|
+
```typescript
|
|
625
|
+
if (hasCapability(user, 'orders:delete')) {
|
|
626
|
+
// Proceed with order deletion
|
|
627
|
+
}
|
|
628
|
+
```
|
|
629
|
+
*/
|
|
630
|
+
export declare function hasCapability<TRole extends string = string, TCapability extends string = string>(user: UserLike | null | undefined, capability: TCapability, options?: CapabilityCheckOptions<TRole, TCapability>): boolean;
|
|
631
|
+
/**
|
|
632
|
+
Checks whether a user possesses at least one capability from a list of capabilities.
|
|
633
|
+
|
|
634
|
+
@param user - The user to evaluate, or null/undefined for anonymous visitors.
|
|
635
|
+
@param capabilities - An array of capabilities to check against.
|
|
636
|
+
@param options - Custom options to override roles configuration or super-admin role.
|
|
637
|
+
@returns True if the user satisfies at least one capability, false otherwise.
|
|
638
|
+
|
|
639
|
+
@example
|
|
640
|
+
```typescript
|
|
641
|
+
if (hasAnyCapability(user, ['orders:read', 'orders:read_all'])) {
|
|
642
|
+
// Authorized to view orders
|
|
643
|
+
}
|
|
644
|
+
```
|
|
645
|
+
*/
|
|
646
|
+
export declare function hasAnyCapability<TRole extends string = string, TCapability extends string = string>(user: UserLike | null | undefined, capabilities: readonly TCapability[], options?: CapabilityCheckOptions<TRole, TCapability>): boolean;
|
|
647
|
+
/**
|
|
648
|
+
Checks whether a user possesses all capabilities from a list of capabilities.
|
|
649
|
+
|
|
650
|
+
@param user - The user to evaluate, or null/undefined for anonymous visitors.
|
|
651
|
+
@param capabilities - An array of required capabilities that must all be satisfied.
|
|
652
|
+
@param options - Custom options to override roles configuration or super-admin role.
|
|
653
|
+
@returns True if the user satisfies every capability in the list, false otherwise.
|
|
654
|
+
|
|
655
|
+
@example
|
|
656
|
+
```typescript
|
|
657
|
+
if (hasAllCapabilities(user, ['orders:export', 'reports:generate'])) {
|
|
658
|
+
// Authorized for batch export and report generation
|
|
659
|
+
}
|
|
660
|
+
```
|
|
661
|
+
*/
|
|
662
|
+
export declare function hasAllCapabilities<TRole extends string = string, TCapability extends string = string>(user: UserLike | null | undefined, capabilities: readonly TCapability[], options?: CapabilityCheckOptions<TRole, TCapability>): boolean;
|
|
663
|
+
/**
|
|
664
|
+
Recursively resolves all granted capabilities for a specified role, including
|
|
665
|
+
capabilities inherited from ancestor roles. Employs cycle-detection to prevent infinite recursion.
|
|
666
|
+
|
|
667
|
+
@param role - The role identifier to resolve.
|
|
668
|
+
@param rolesConfig - The dictionary of defined roles and their capabilities.
|
|
669
|
+
@param visitedRoles - Internal set tracking visited roles during recursion to avoid cycles.
|
|
670
|
+
@returns A Set containing all unique capability patterns granted to this role.
|
|
671
|
+
|
|
672
|
+
@example
|
|
673
|
+
```typescript
|
|
674
|
+
const caps = resolveRoleCapabilities('manager', rolesConfig);
|
|
675
|
+
```
|
|
676
|
+
*/
|
|
677
|
+
export declare function resolveRoleCapabilities<TRole extends string = string, TCapability extends string = string>(role: TRole, rolesConfig: RolesConfig<TRole, TCapability>, visitedRoles?: Set<string>): Set<string>;
|
|
678
|
+
/**
|
|
679
|
+
Aggregates all granted capabilities for a user, combining capabilities from
|
|
680
|
+
all their assigned roles and any direct capabilities explicitly assigned to the user object.
|
|
681
|
+
|
|
682
|
+
@param user - The user whose capabilities should be resolved.
|
|
683
|
+
@param options - Configuration options containing role definitions and field mapping.
|
|
684
|
+
@returns A Set of all capabilities granted to the user.
|
|
685
|
+
|
|
686
|
+
@example
|
|
687
|
+
```typescript
|
|
688
|
+
const caps = resolveUserCapabilities(user, { roles: rolesConfig });
|
|
689
|
+
```
|
|
690
|
+
*/
|
|
691
|
+
export declare function resolveUserCapabilities<TRole extends string = string, TCapability extends string = string>(user: UserLike | null | undefined, options?: CapabilityCheckOptions<TRole, TCapability>): Set<string>;
|
|
692
|
+
export type { CapabilityCheckOptions, FieldAccess, FieldAccessArgs, RbacAccess, RbacAccessArguments, RbacFieldAccess, RbacFieldAccessArguments, RbacPluginOptions, RbacWhere, RequireCapabilityOptions, RoleCheckOptions, RoleDefinition, RolesConfig, TypeWithID, UserLike, WildcardCapability };
|
|
693
|
+
//# sourceMappingURL=index.d.ts.map
|