@payload-kit/rbac 0.1.0-alpha.0 → 0.1.0-alpha.2

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/dist/index.js CHANGED
@@ -26,10 +26,12 @@ requireCapability('orders:read_all'),
26
26
  function or(...rules) {
27
27
  return async (arguments_) => {
28
28
  if (rules.length === 0) return false;
29
- const results = await Promise.all(rules.map(async (rule) => rule(arguments_)));
30
- if (results.includes(true)) return true;
31
29
  const whereClauses = [];
32
- for (const resultItem of results) if (typeof resultItem === "object" && resultItem !== null) whereClauses.push(resultItem);
30
+ for (const rule of rules) {
31
+ const resultItem = await rule(arguments_);
32
+ if (resultItem === true) return true;
33
+ if (typeof resultItem === "object" && resultItem !== null) whereClauses.push(resultItem);
34
+ }
33
35
  if (whereClauses.length === 0) return false;
34
36
  if (whereClauses.length === 1) return whereClauses[0];
35
37
  return { or: whereClauses };
@@ -39,11 +41,12 @@ function or(...rules) {
39
41
  Combines multiple access rules with logical AND.
40
42
 
41
43
  Evaluation semantics:
42
- 1. If any rule evaluates to `false` (denial), access is denied immediately (`false`).
43
- 2. If any rules return `Where` query constraints:
44
+ 1. If no rules are provided, fails closed by returning `false`.
45
+ 2. If any rule evaluates to `false` or an unexpected non-boolean, non-object value (e.g. `undefined`, `null`), access is denied immediately (`false`).
46
+ 3. If any rules return `Where` query constraints:
44
47
  - If exactly one `Where` constraint exists, it is returned directly.
45
48
  - If multiple `Where` constraints exist, they are merged via `{ and: [...] }`.
46
- 3. If all rules evaluate to `true` (and no `Where` constraints are returned), access is granted (`true`).
49
+ 4. If all rules evaluate to `true` (and no `Where` constraints are returned), access is granted (`true`).
47
50
 
48
51
  @param rules - One or more Payload Access rules to evaluate.
49
52
  @returns A composite Payload Access rule.
@@ -61,11 +64,17 @@ requireCapability('orders:create')
61
64
  */
62
65
  function and(...rules) {
63
66
  return async (arguments_) => {
64
- if (rules.length === 0) return true;
65
- const results = await Promise.all(rules.map(async (rule) => rule(arguments_)));
66
- if (results.includes(false)) return false;
67
+ if (rules.length === 0) return false;
67
68
  const whereClauses = [];
68
- for (const resultItem of results) if (typeof resultItem === "object" && resultItem !== null) whereClauses.push(resultItem);
69
+ for (const rule of rules) {
70
+ const resultItem = await rule(arguments_);
71
+ if (resultItem === true) continue;
72
+ if (typeof resultItem === "object" && resultItem !== null) {
73
+ whereClauses.push(resultItem);
74
+ continue;
75
+ }
76
+ return false;
77
+ }
69
78
  if (whereClauses.length === 0) return true;
70
79
  if (whereClauses.length === 1) return whereClauses[0];
71
80
  return { and: whereClauses };
@@ -125,16 +134,17 @@ requireFieldCapability('users:read_sensitive'),
125
134
  function fieldOr(...rules) {
126
135
  return async (arguments_) => {
127
136
  if (rules.length === 0) return false;
128
- return (await Promise.all(rules.map(async (rule) => rule(arguments_)))).includes(true);
137
+ for (const rule of rules) if (await rule(arguments_) === true) return true;
138
+ return false;
129
139
  };
130
140
  }
131
141
  /**
132
142
  Combines multiple field-level access rules with logical AND.
133
143
 
134
144
  Evaluation semantics:
135
- 1. If any rule evaluates to `false`, access to the field is denied (`false`).
136
- 2. If all rules evaluate to `true`, access to the field is granted (`true`).
137
- 3. If no rules are provided, returns `true`.
145
+ 1. If no rules are provided, fails closed by returning `false`.
146
+ 2. If any rule evaluates to `false`, access to the field is denied (`false`).
147
+ 3. If all rules evaluate to `true`, access to the field is granted (`true`).
138
148
 
139
149
  @param rules - One or more FieldAccess rules to evaluate.
140
150
  @returns A composite FieldAccess rule.
@@ -155,8 +165,9 @@ requireFieldCapability('orders:update')
155
165
  */
156
166
  function fieldAnd(...rules) {
157
167
  return async (arguments_) => {
158
- if (rules.length === 0) return true;
159
- return !(await Promise.all(rules.map(async (rule) => rule(arguments_)))).includes(false);
168
+ if (rules.length === 0) return false;
169
+ for (const rule of rules) if (await rule(arguments_) !== true) return false;
170
+ return true;
160
171
  };
161
172
  }
162
173
  /**
@@ -178,9 +189,56 @@ read: fieldNot(requireFieldRole('customer')),
178
189
  */
179
190
  function fieldNot(rule) {
180
191
  return async (arguments_) => {
181
- return !await rule(arguments_);
192
+ const isResult = await rule(arguments_);
193
+ if (typeof isResult !== "boolean") throw new TypeError("[RBAC] fieldNot() combinator requires a boolean-returning FieldAccess rule.");
194
+ return !isResult;
182
195
  };
183
196
  }
197
+ // ../../internal/utils/src/guards.ts
198
+ /**
199
+ Checks whether a value is a non-null, non-array plain/object record.
200
+ */
201
+ function isRecord(value) {
202
+ return typeof value === "object" && value !== null && !Array.isArray(value);
203
+ }
204
+ /**
205
+ Checks whether a value is a string containing at least one non-whitespace character.
206
+ */
207
+ function isNonEmptyString(value) {
208
+ return typeof value === "string" && value.trim().length > 0;
209
+ }
210
+ /**
211
+ Reads an own property from a record safely without traversing the prototype chain.
212
+ */
213
+ function getOwnProperty(record, key) {
214
+ return Object.hasOwn(record, key) ? record[key] : void 0;
215
+ }
216
+ /**
217
+ Reads an own string property from a record safely without traversing the prototype chain.
218
+ */
219
+ function getOwnString(record, key) {
220
+ const value = getOwnProperty(record, key);
221
+ return typeof value === "string" ? value : void 0;
222
+ }
223
+ /**
224
+ Reads an own non-empty string property (containing at least one non-whitespace character)
225
+ from a record safely without traversing the prototype chain.
226
+ */
227
+ function getOwnNonEmptyString(record, key) {
228
+ const value = getOwnProperty(record, key);
229
+ return isNonEmptyString(value) ? value : void 0;
230
+ }
231
+ /**
232
+ Reads an own array property from a record and extracts its trimmed, non-empty string items.
233
+ Returns `undefined` if the own property does not exist or is not an array.
234
+ */
235
+ function getOwnStringArray(record, key) {
236
+ const value = getOwnProperty(record, key);
237
+ if (!Array.isArray(value)) return;
238
+ const items = [];
239
+ for (const entry of value) if (isNonEmptyString(entry)) items.push(entry.trim());
240
+ return items;
241
+ }
184
242
  // src/registry.ts
185
243
  /**
186
244
  Module-level storage holding the active RBAC configuration.
@@ -208,12 +266,54 @@ Clears the active RBAC configuration. Primarily utilized in test suites to preve
208
266
  function resetRbacConfig() {
209
267
  rbacRegistryState.activeConfig = void 0;
210
268
  }
269
+ // src/options.ts
270
+ /**
271
+ Resolves an optional tri-state override against a global registry default:
272
+ - `undefined` inherits the global registry value,
273
+ - `null` explicitly disables / ignores the global registry value,
274
+ - any other value is used as-is.
275
+ */
276
+ function resolveTriState(override, globalValue = null) {
277
+ return override === void 0 ? globalValue : override;
278
+ }
279
+ function resolveEffectiveUserCollectionSlug(userCollectionSlug) {
280
+ return resolveTriState(userCollectionSlug, getRbacConfig()?.userCollectionSlug);
281
+ }
282
+ function resolveEffectiveRoleOptions(options) {
283
+ const globalConfig = getRbacConfig();
284
+ return {
285
+ rolesFieldName: resolveTriState(options?.rolesFieldName, globalConfig?.rolesFieldName),
286
+ anonymousRole: resolveTriState(options?.anonymousRole, globalConfig?.anonymousRole),
287
+ userCollectionSlug: resolveTriState(options?.userCollectionSlug, globalConfig?.userCollectionSlug)
288
+ };
289
+ }
290
+ function resolveEffectiveCapabilityOptions(options) {
291
+ const globalConfig = getRbacConfig();
292
+ return {
293
+ roles: options?.roles ?? globalConfig?.roles,
294
+ superAdminRole: resolveTriState(options?.superAdminRole, globalConfig?.superAdminRole),
295
+ anonymousRole: resolveTriState(options?.anonymousRole, globalConfig?.anonymousRole),
296
+ rolesFieldName: resolveTriState(options?.rolesFieldName, globalConfig?.rolesFieldName),
297
+ userCollectionSlug: resolveTriState(options?.userCollectionSlug, globalConfig?.userCollectionSlug),
298
+ allowDirectUserCapabilities: options?.allowDirectUserCapabilities ?? globalConfig?.allowDirectUserCapabilities ?? false
299
+ };
300
+ }
211
301
  // src/roles/checker.ts
212
302
  /**
303
+ Validates whether an authenticated user belongs to the expected Payload auth collection.
304
+ When no `userCollectionSlug` is configured (`undefined` or `null`), any user object is accepted.
305
+ When configured, fails closed unless `user.collection` matches the expected slug.
306
+ */
307
+ function isUserFromAllowedCollection(user, userCollectionSlug) {
308
+ if (!isRecord(user)) return false;
309
+ const expectedCollection = resolveEffectiveUserCollectionSlug(userCollectionSlug);
310
+ return !expectedCollection || getOwnString(user, "collection") === expectedCollection;
311
+ }
312
+ /**
213
313
  Extracts and normalizes the assigned roles of a user into an array of string identifiers.
214
314
 
215
315
  @param user - The user object to inspect, or null/undefined.
216
- @param rolesFieldName - Name of the field containing the role(s). Defaults to plugin config or `'role'`.
316
+ @param optionsOrFieldName - Role check options or explicit field name containing the role(s). Defaults to plugin config or `'role'`.
217
317
  @returns An array of trimmed, non-empty role identifiers.
218
318
 
219
319
  @example
@@ -224,16 +324,20 @@ resolveUserRoles(null); // []
224
324
  ```
225
325
  */
226
326
  function resolveUserRoles(user, optionsOrFieldName) {
227
- const options = typeof optionsOrFieldName === "object" ? optionsOrFieldName : void 0;
228
- const fieldName = typeof optionsOrFieldName === "string" ? optionsOrFieldName : options?.rolesFieldName ?? getRbacConfig()?.rolesFieldName ?? "role";
229
- if (!user || typeof user !== "object") {
230
- const anonymousRole = options?.anonymousRole === void 0 ? getRbacConfig()?.anonymousRole : options.anonymousRole;
231
- return anonymousRole ? [anonymousRole] : [];
232
- }
233
- const candidateArray = Array.isArray(user.roles) ? user.roles : Array.isArray(user[fieldName]) ? user[fieldName] : void 0;
234
- if (candidateArray) return candidateArray.filter((role) => typeof role === "string" && role.trim().length > 0).map((role) => role.trim());
235
- const singular = user[fieldName] ?? user.role;
236
- return typeof singular === "string" && singular.trim().length > 0 ? [singular.trim()] : [];
327
+ const effective = resolveEffectiveRoleOptions(typeof optionsOrFieldName === "object" ? optionsOrFieldName : void 0);
328
+ if (!isRecord(user)) return effective.anonymousRole ? [effective.anonymousRole] : [];
329
+ if (!isUserFromAllowedCollection(user, effective.userCollectionSlug)) return [];
330
+ const explicitFieldName = typeof optionsOrFieldName === "string" ? optionsOrFieldName : effective.rolesFieldName;
331
+ const fieldName = explicitFieldName ?? "role";
332
+ const configuredArray = getOwnStringArray(user, fieldName);
333
+ if (configuredArray) return configuredArray;
334
+ const configuredSingle = getOwnNonEmptyString(user, fieldName);
335
+ if (configuredSingle) return [configuredSingle.trim()];
336
+ if (explicitFieldName !== null) return [];
337
+ const fallbackRoles = getOwnStringArray(user, "roles");
338
+ if (fallbackRoles) return fallbackRoles;
339
+ const fallbackRole = getOwnNonEmptyString(user, "role");
340
+ return fallbackRole ? [fallbackRole.trim()] : [];
237
341
  }
238
342
  /**
239
343
  Verifies whether a user has been assigned a specific role.
@@ -290,7 +394,7 @@ if (hasAllRoles(user, ['verified', 'subscriber'])) {
290
394
  ```
291
395
  */
292
396
  function hasAllRoles(user, roles, options) {
293
- if (roles.length === 0) return true;
397
+ if (roles.length === 0) return false;
294
398
  const userRoles = resolveUserRoles(user, options);
295
399
  const userRolesSet = new Set(userRoles);
296
400
  return roles.every((role) => userRolesSet.has(role));
@@ -318,6 +422,7 @@ matchesCapability('orders:read', 'orders:delete'); // false
318
422
  function doesMatchCapability(grantedPattern, requestedCapability) {
319
423
  const pattern = grantedPattern.trim();
320
424
  const requested = requestedCapability.trim();
425
+ if (!pattern || !requested) return false;
321
426
  if (pattern === "*" || pattern === requested) return true;
322
427
  if (pattern.endsWith(":*")) {
323
428
  const domainPrefix = pattern.slice(0, -1);
@@ -344,6 +449,7 @@ function resolveRoleCapabilities(role, rolesConfig, visitedRoles = /* @__PURE__
344
449
  const result = /* @__PURE__ */ new Set();
345
450
  if (visitedRoles.has(role)) return result;
346
451
  visitedRoles.add(role);
452
+ if (!Object.hasOwn(rolesConfig, role)) return result;
347
453
  const definition = rolesConfig[role];
348
454
  if (!definition) return result;
349
455
  for (const capability of definition.capabilities) result.add(capability);
@@ -368,24 +474,24 @@ const caps = resolveUserCapabilities(user, { roles: rolesConfig });
368
474
  */
369
475
  function resolveUserCapabilities(user, options) {
370
476
  const result = /* @__PURE__ */ new Set();
371
- const globalConfig = getRbacConfig();
372
- const rolesConfig = options?.roles ?? globalConfig?.roles;
373
- if (!user || typeof user !== "object") {
374
- const anonymousRole = options?.anonymousRole === void 0 ? globalConfig?.anonymousRole : options.anonymousRole;
375
- return anonymousRole && rolesConfig && Object.hasOwn(rolesConfig, anonymousRole) ? resolveRoleCapabilities(anonymousRole, rolesConfig) : result;
376
- }
477
+ const effective = resolveEffectiveCapabilityOptions(options);
478
+ const rolesConfig = effective.roles;
479
+ if (!isRecord(user)) return rolesConfig && effective.anonymousRole && Object.hasOwn(rolesConfig, effective.anonymousRole) ? resolveRoleCapabilities(effective.anonymousRole, rolesConfig) : result;
480
+ if (!isUserFromAllowedCollection(user, effective.userCollectionSlug)) return result;
377
481
  if (rolesConfig) {
378
482
  const userRoles = resolveUserRoles(user, {
379
- rolesFieldName: options?.rolesFieldName,
380
- anonymousRole: options?.anonymousRole
483
+ rolesFieldName: effective.rolesFieldName,
484
+ anonymousRole: effective.anonymousRole,
485
+ userCollectionSlug: effective.userCollectionSlug
381
486
  });
382
487
  for (const role of userRoles) {
383
488
  const roleCapabilities = resolveRoleCapabilities(role, rolesConfig);
384
489
  for (const cap of roleCapabilities) result.add(cap);
385
490
  }
386
491
  }
387
- if (Array.isArray(user.capabilities)) {
388
- for (const cap of user.capabilities) if (typeof cap === "string" && cap.trim().length > 0) result.add(cap.trim());
492
+ if (effective.allowDirectUserCapabilities) {
493
+ const directCapabilities = getOwnStringArray(user, "capabilities");
494
+ if (directCapabilities) for (const cap of directCapabilities) result.add(cap);
389
495
  }
390
496
  return result;
391
497
  }
@@ -407,8 +513,7 @@ if (hasCapability(user, 'orders:delete')) {
407
513
  ```
408
514
  */
409
515
  function hasCapability(user, capability, options) {
410
- const globalConfig = getRbacConfig();
411
- const superAdminRole = options?.superAdminRole === void 0 ? globalConfig?.superAdminRole ?? "admin" : options.superAdminRole;
516
+ const { superAdminRole } = resolveEffectiveCapabilityOptions(options);
412
517
  if (user && superAdminRole && hasRole(user, superAdminRole, options)) return true;
413
518
  const userCapabilities = resolveUserCapabilities(user, options);
414
519
  for (const grantedPattern of userCapabilities) if (doesMatchCapability(grantedPattern, capability)) return true;
@@ -448,32 +553,34 @@ if (hasAllCapabilities(user, ['orders:export', 'reports:generate'])) {
448
553
  ```
449
554
  */
450
555
  function hasAllCapabilities(user, capabilities, options) {
451
- return capabilities.every((cap) => hasCapability(user, cap, options));
556
+ return capabilities.length > 0 && capabilities.every((cap) => hasCapability(user, cap, options));
452
557
  }
453
558
  // src/access/shared.ts
454
559
  /**
455
560
  Evaluates whether a request user satisfies at least one of the required roles.
456
561
  */
457
562
  function hasRoleAccess(user, roles, options) {
458
- return Boolean(user) && hasAnyRole(user, roles, options);
563
+ return hasAnyRole(user, roles, options);
459
564
  }
460
565
  /**
461
566
  Evaluates whether a request user satisfies the required capability or capability list.
462
567
  */
463
568
  function hasCapabilityAccess(user, capability, options) {
464
- if (!user) return false;
465
569
  if (Array.isArray(capability)) return (options?.mode ?? "any") === "all" ? hasAllCapabilities(user, capability, options) : hasAnyCapability(user, capability, options);
466
570
  return hasCapability(user, capability, options);
467
571
  }
468
572
  // src/access/field-guards.ts
469
- const checkFieldAuthenticated = ({ req: { user } }) => Boolean(user);
573
+ const checkFieldAuthenticated = ({ req: { user } }) => isUserFromAllowedCollection(user);
470
574
  /**
471
575
  Creates a field-level access rule requiring that the requesting client is an authenticated user.
576
+ When `userCollectionSlug` is configured (either in `options` or in the global RBAC config),
577
+ only users from that auth collection are accepted.
472
578
 
473
- @returns A Payload FieldAccess function returning true if user is authenticated, false otherwise.
579
+ @param options - Optional authentication check overrides (e.g., `userCollectionSlug`).
580
+ @returns A Payload FieldAccess function returning true if an eligible user is authenticated, false otherwise.
474
581
  */
475
- function requireFieldAuth() {
476
- return checkFieldAuthenticated;
582
+ function requireFieldAuth(options) {
583
+ return options ? ({ req: { user } }) => isUserFromAllowedCollection(user, options.userCollectionSlug) : checkFieldAuthenticated;
477
584
  }
478
585
  /**
479
586
  Creates a field-level access rule requiring that the requesting user possesses one or more specified roles.
@@ -542,11 +649,14 @@ function systemOnlyField() {
542
649
  };
543
650
  }
544
651
  // src/access/guards.ts
545
- const checkAuthenticated = ({ req: { user } }) => Boolean(user);
652
+ const checkAuthenticated = ({ req: { user } }) => isUserFromAllowedCollection(user);
546
653
  /**
547
654
  Creates an access rule requiring that the requesting client is an authenticated user.
655
+ When `userCollectionSlug` is configured (either in `options` or in the global RBAC config),
656
+ only users from that auth collection are accepted.
548
657
 
549
- @returns A Payload Access function returning true if user is present, false otherwise.
658
+ @param options - Optional authentication check overrides (e.g., `userCollectionSlug`).
659
+ @returns A Payload Access function returning true if an eligible user is present, false otherwise.
550
660
 
551
661
  @example
552
662
  ```typescript
@@ -557,8 +667,8 @@ create: requireAuth(),
557
667
  };
558
668
  ```
559
669
  */
560
- function requireAuth() {
561
- return checkAuthenticated;
670
+ function requireAuth(options) {
671
+ return options ? ({ req: { user } }) => isUserFromAllowedCollection(user, options.userCollectionSlug) : checkAuthenticated;
562
672
  }
563
673
  /**
564
674
  Creates an access rule requiring that the requesting user possesses one or more specified roles.
@@ -620,6 +730,41 @@ Functional alias for `anyone`.
620
730
  */
621
731
  const allowAll = anyone;
622
732
  // src/plugin.ts
733
+ const passthroughPlugin = (incomingConfig) => incomingConfig;
734
+ /**
735
+ Builds a write-protected Payload `select` field for user roles, ready to be placed
736
+ anywhere in your users collection (`fields`, `tabs`, `row`, or `sidebar`).
737
+
738
+ Defaults `access.create` and `access.update` to `superAdminRole` (or `nobodyField`
739
+ when no `superAdminRole` is configured), while preserving any custom access overrides.
740
+
741
+ @param options - RBAC plugin options defining roles, default role, and field name.
742
+ @param overrides - Optional Payload `SelectField` overrides (e.g. `admin`, `hasMany`, `label`, `access`).
743
+ @returns A configured Payload `SelectField`.
744
+ */
745
+ function createRoleField(options, overrides) {
746
+ const rolesFieldName = options.rolesFieldName ?? "role";
747
+ const roleOptions = Object.entries(options.roles).map(([roleKey, roleDefinition]) => ({
748
+ label: roleDefinition.label ?? roleKey,
749
+ value: roleKey
750
+ }));
751
+ const defaultWriteAccess = options.superAdminRole ? requireFieldRole(options.superAdminRole, {
752
+ rolesFieldName: options.rolesFieldName ?? null,
753
+ userCollectionSlug: options.userCollectionSlug ?? null
754
+ }) : nobodyField;
755
+ return {
756
+ ...overrides,
757
+ name: rolesFieldName,
758
+ type: "select",
759
+ options: roleOptions,
760
+ defaultValue: overrides?.defaultValue ?? options.defaultRole,
761
+ access: {
762
+ ...overrides?.access,
763
+ create: overrides?.access?.create ?? defaultWriteAccess,
764
+ update: overrides?.access?.update ?? defaultWriteAccess
765
+ }
766
+ };
767
+ }
623
768
  /**
624
769
  Creates and initializes the Payload CMS RBAC (Role-Based Access Control) plugin.
625
770
 
@@ -646,35 +791,7 @@ superAdminRole: 'admin',
646
791
  */
647
792
  function rbacPlugin(options) {
648
793
  registerRbacConfig(options);
649
- return (incomingConfig) => {
650
- const config = { ...incomingConfig };
651
- if (!options.syncUserRolesField || !Array.isArray(config.collections)) return config;
652
- const userCollectionSlug = options.userCollectionSlug ?? "users";
653
- const rolesFieldName = options.rolesFieldName ?? "role";
654
- const roleOptions = Object.entries(options.roles).map(([roleKey, roleDefinition]) => ({
655
- label: roleDefinition.label ?? roleKey,
656
- value: roleKey
657
- }));
658
- config.collections = config.collections.map((collection) => {
659
- if (collection.slug !== userCollectionSlug) return collection;
660
- const updatedFields = collection.fields.map((field) => {
661
- if ("name" in field && field.name === rolesFieldName && field.type === "select") {
662
- const selectField = field;
663
- return {
664
- ...selectField,
665
- options: roleOptions,
666
- defaultValue: options.defaultRole ?? selectField.defaultValue
667
- };
668
- }
669
- return field;
670
- });
671
- return {
672
- ...collection,
673
- fields: updatedFields
674
- };
675
- });
676
- return config;
677
- };
794
+ return passthroughPlugin;
678
795
  }
679
796
  /**
680
797
  Creates an isolated, type-safe suite of RBAC helper functions bound to a specific configuration.
@@ -695,60 +812,70 @@ const canEdit = rbac.hasCapability(user, 'articles:publish');
695
812
  ```
696
813
  */
697
814
  function createRbac(options) {
815
+ const baseAuthOptions = { userCollectionSlug: options.userCollectionSlug ?? null };
816
+ const baseRoleOptions = {
817
+ ...baseAuthOptions,
818
+ rolesFieldName: options.rolesFieldName ?? null,
819
+ anonymousRole: options.anonymousRole ?? null
820
+ };
821
+ const baseCapabilityOptions = {
822
+ ...baseRoleOptions,
823
+ anonymousRole: options.anonymousRole ?? null,
824
+ roles: options.roles,
825
+ superAdminRole: options.superAdminRole ?? null,
826
+ allowDirectUserCapabilities: options.allowDirectUserCapabilities ?? false
827
+ };
698
828
  return {
699
829
  options,
700
830
  plugin: () => rbacPlugin(options),
701
- resolveRoles: (user) => resolveUserRoles(user, options.rolesFieldName),
831
+ roleField: (overrides) => createRoleField(options, overrides),
832
+ resolveRoles: (user) => resolveUserRoles(user, baseRoleOptions),
702
833
  hasRole: (user, role, checkOptions) => hasRole(user, role, {
703
- rolesFieldName: options.rolesFieldName,
834
+ ...baseRoleOptions,
704
835
  ...checkOptions
705
836
  }),
706
837
  hasAnyRole: (user, roles, checkOptions) => hasAnyRole(user, roles, {
707
- rolesFieldName: options.rolesFieldName,
838
+ ...baseRoleOptions,
708
839
  ...checkOptions
709
840
  }),
710
841
  hasAllRoles: (user, roles, checkOptions) => hasAllRoles(user, roles, {
711
- rolesFieldName: options.rolesFieldName,
842
+ ...baseRoleOptions,
712
843
  ...checkOptions
713
844
  }),
714
845
  hasCapability: (user, capability, checkOptions) => hasCapability(user, capability, {
715
- roles: options.roles,
716
- superAdminRole: options.superAdminRole,
717
- rolesFieldName: options.rolesFieldName,
846
+ ...baseCapabilityOptions,
718
847
  ...checkOptions
719
848
  }),
720
849
  hasAnyCapability: (user, capabilities, checkOptions) => hasAnyCapability(user, capabilities, {
721
- roles: options.roles,
722
- superAdminRole: options.superAdminRole,
723
- rolesFieldName: options.rolesFieldName,
850
+ ...baseCapabilityOptions,
724
851
  ...checkOptions
725
852
  }),
726
853
  hasAllCapabilities: (user, capabilities, checkOptions) => hasAllCapabilities(user, capabilities, {
727
- roles: options.roles,
728
- superAdminRole: options.superAdminRole,
729
- rolesFieldName: options.rolesFieldName,
854
+ ...baseCapabilityOptions,
730
855
  ...checkOptions
731
856
  }),
732
- requireAuth,
857
+ requireAuth: (authOptions) => requireAuth({
858
+ ...baseAuthOptions,
859
+ ...authOptions
860
+ }),
733
861
  requireRole: (role, checkOptions) => requireRole(role, {
734
- rolesFieldName: options.rolesFieldName,
862
+ ...baseRoleOptions,
735
863
  ...checkOptions
736
864
  }),
737
865
  requireCapability: (capability, guardOptions) => requireCapability(capability, {
738
- roles: options.roles,
739
- superAdminRole: options.superAdminRole,
740
- rolesFieldName: options.rolesFieldName,
866
+ ...baseCapabilityOptions,
741
867
  ...guardOptions
742
868
  }),
743
- requireFieldAuth,
869
+ requireFieldAuth: (authOptions) => requireFieldAuth({
870
+ ...baseAuthOptions,
871
+ ...authOptions
872
+ }),
744
873
  requireFieldRole: (role, checkOptions) => requireFieldRole(role, {
745
- rolesFieldName: options.rolesFieldName,
874
+ ...baseRoleOptions,
746
875
  ...checkOptions
747
876
  }),
748
877
  requireFieldCapability: (capability, guardOptions) => requireFieldCapability(capability, {
749
- roles: options.roles,
750
- superAdminRole: options.superAdminRole,
751
- rolesFieldName: options.rolesFieldName,
878
+ ...baseCapabilityOptions,
752
879
  ...guardOptions
753
880
  }),
754
881
  and,
@@ -759,12 +886,9 @@ function createRbac(options) {
759
886
  fieldNot,
760
887
  doesMatchCapability,
761
888
  resolveRoleCapabilities: (role) => resolveRoleCapabilities(role, options.roles),
762
- resolveUserCapabilities: (user) => resolveUserCapabilities(user, {
763
- roles: options.roles,
764
- rolesFieldName: options.rolesFieldName
765
- })
889
+ resolveUserCapabilities: (user) => resolveUserCapabilities(user, baseCapabilityOptions)
766
890
  };
767
891
  }
768
- export { allowAll, allowField, and, anyone, anyoneField, createRbac, denyAll, denyField, doesMatchCapability, fieldAnd, fieldNot, fieldOr, getRbacConfig, hasAllCapabilities, hasAllRoles, hasAnyCapability, hasAnyRole, hasCapability, hasRole, nobody, nobodyField, not, or, rbacPlugin, readOnlyField, registerRbacConfig, requireAuth, requireCapability, requireFieldAuth, requireFieldCapability, requireFieldRole, requireRole, resetRbacConfig, resolveRoleCapabilities, resolveUserCapabilities, resolveUserRoles, systemOnlyField };
892
+ export { allowAll, allowField, and, anyone, anyoneField, createRbac, createRoleField, denyAll, denyField, doesMatchCapability, fieldAnd, fieldNot, fieldOr, getRbacConfig, hasAllCapabilities, hasAllRoles, hasAnyCapability, hasAnyRole, hasCapability, hasRole, nobody, nobodyField, not, or, rbacPlugin, readOnlyField, registerRbacConfig, requireAuth, requireCapability, requireFieldAuth, requireFieldCapability, requireFieldRole, requireRole, resetRbacConfig, resolveRoleCapabilities, resolveUserCapabilities, resolveUserRoles, systemOnlyField };
769
893
 
770
894
  //# sourceMappingURL=index.js.map