casbin 5.47.0 → 5.49.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.
Files changed (58) hide show
  1. package/lib/cjs/cachedEnforcer.js +1 -1
  2. package/lib/cjs/config.js +1 -1
  3. package/lib/cjs/coreEnforcer.js +25 -25
  4. package/lib/cjs/effect/defaultEffectorStream.js +5 -5
  5. package/lib/cjs/effect/index.js +6 -2
  6. package/lib/cjs/enforceContext.js +2 -1
  7. package/lib/cjs/enforcer.js +17 -13
  8. package/lib/cjs/frontend.js +2 -2
  9. package/lib/cjs/index.js +7 -3
  10. package/lib/cjs/internalEnforcer.d.ts +8 -0
  11. package/lib/cjs/internalEnforcer.js +105 -11
  12. package/lib/cjs/log/index.js +6 -2
  13. package/lib/cjs/managementEnforcer.d.ts +4 -3
  14. package/lib/cjs/managementEnforcer.js +13 -10
  15. package/lib/cjs/model/assertion.js +7 -3
  16. package/lib/cjs/model/functionMap.d.ts +1 -1
  17. package/lib/cjs/model/functionMap.js +6 -2
  18. package/lib/cjs/model/index.js +6 -2
  19. package/lib/cjs/model/model.js +17 -13
  20. package/lib/cjs/persist/defaultFilteredAdapter.js +1 -1
  21. package/lib/cjs/persist/fileAdapter.js +4 -4
  22. package/lib/cjs/persist/fileSystem.d.ts +1 -1
  23. package/lib/cjs/persist/fileSystem.js +6 -3
  24. package/lib/cjs/persist/helper.js +1 -1
  25. package/lib/cjs/persist/index.js +6 -2
  26. package/lib/cjs/rbac/defaultRoleManager.d.ts +1 -1
  27. package/lib/cjs/rbac/defaultRoleManager.js +2 -2
  28. package/lib/cjs/rbac/index.js +6 -2
  29. package/lib/cjs/syncedEnforcer.js +1 -1
  30. package/lib/cjs/util/builtinOperators.js +1 -1
  31. package/lib/cjs/util/index.js +6 -2
  32. package/lib/cjs/util/ip.d.ts +3 -3
  33. package/lib/cjs/util/util.d.ts +2 -2
  34. package/lib/cjs/util/util.js +4 -4
  35. package/lib/esm/constants.js +1 -0
  36. package/lib/esm/effect/defaultEffectorStream.js +5 -5
  37. package/lib/esm/effect/effectorStream.js +1 -0
  38. package/lib/esm/enforcer.js +5 -5
  39. package/lib/esm/internalEnforcer.d.ts +8 -0
  40. package/lib/esm/internalEnforcer.js +105 -11
  41. package/lib/esm/log/logger.js +1 -0
  42. package/lib/esm/managementEnforcer.d.ts +4 -3
  43. package/lib/esm/managementEnforcer.js +13 -10
  44. package/lib/esm/model/functionMap.d.ts +1 -1
  45. package/lib/esm/model/model.js +6 -6
  46. package/lib/esm/persist/adapter.js +1 -0
  47. package/lib/esm/persist/batchAdapter.js +1 -0
  48. package/lib/esm/persist/fileSystem.d.ts +1 -1
  49. package/lib/esm/persist/filteredAdapter.js +1 -0
  50. package/lib/esm/persist/updatableAdapter.js +1 -0
  51. package/lib/esm/persist/watcher.js +1 -0
  52. package/lib/esm/persist/watcherEx.js +1 -0
  53. package/lib/esm/rbac/defaultRoleManager.d.ts +1 -1
  54. package/lib/esm/rbac/roleManager.js +1 -0
  55. package/lib/esm/util/ip.d.ts +3 -3
  56. package/lib/esm/util/util.d.ts +2 -2
  57. package/lib/esm/util/util.js +2 -2
  58. package/package.json +3 -3
@@ -67,6 +67,6 @@ class CachedEnforcer extends enforcer_1.Enforcer {
67
67
  exports.CachedEnforcer = CachedEnforcer;
68
68
  // newCachedEnforcer creates a cached enforcer via file or DB.
69
69
  async function newCachedEnforcer(...params) {
70
- return enforcer_1.newEnforcerWithClass(CachedEnforcer, ...params);
70
+ return (0, enforcer_1.newEnforcerWithClass)(CachedEnforcer, ...params);
71
71
  }
72
72
  exports.newCachedEnforcer = newCachedEnforcer;
package/lib/cjs/config.js CHANGED
@@ -74,7 +74,7 @@ class Config {
74
74
  }
75
75
  }
76
76
  parse(path) {
77
- const body = (this.fs ? this.fs : persist_1.mustGetDefaultFileSystem()).readFileSync(path);
77
+ const body = (this.fs ? this.fs : (0, persist_1.mustGetDefaultFileSystem)()).readFileSync(path);
78
78
  this.parseBuffer(Buffer.isBuffer(body) ? body : Buffer.from(body));
79
79
  }
80
80
  parseBuffer(buf) {
@@ -53,11 +53,11 @@ class CoreEnforcer {
53
53
  }
54
54
  getExpression(asyncCompile, exp) {
55
55
  const matcherKey = `${asyncCompile ? 'ASYNC[' : 'SYNC['}${exp}]`;
56
- expression_eval_1.addBinaryOp('in', 1, util_1.customIn);
56
+ (0, expression_eval_1.addBinaryOp)('in', 1, util_1.customIn);
57
57
  let expression = this.matcherMap.get(matcherKey);
58
58
  if (!expression) {
59
- exp = util_1.bracketCompatible(exp);
60
- expression = asyncCompile ? expression_eval_1.compileAsync(exp) : expression_eval_1.compile(exp);
59
+ exp = (0, util_1.bracketCompatible)(exp);
60
+ expression = asyncCompile ? (0, expression_eval_1.compileAsync)(exp) : (0, expression_eval_1.compile)(exp);
61
61
  this.matcherMap.set(matcherKey, expression);
62
62
  }
63
63
  return expression;
@@ -68,7 +68,7 @@ class CoreEnforcer {
68
68
  * so the policy is invalidated and needs to be reloaded by calling LoadPolicy().
69
69
  */
70
70
  loadModel() {
71
- this.model = model_1.newModelFromFile(this.modelPath, this.fs);
71
+ this.model = (0, model_1.newModelFromFile)(this.modelPath, this.fs);
72
72
  this.model.printModel();
73
73
  }
74
74
  /**
@@ -276,7 +276,7 @@ class CoreEnforcer {
276
276
  * @param enable whether to enable Casbin's log.
277
277
  */
278
278
  enableLog(enable) {
279
- log_1.getLogger().enableLog(enable);
279
+ (0, log_1.getLogger)().enableLog(enable);
280
280
  }
281
281
  /**
282
282
  * enableAutoSave controls whether to save a policy rule automatically to
@@ -373,14 +373,14 @@ class CoreEnforcer {
373
373
  const astMap = this.model.model.get('g');
374
374
  astMap === null || astMap === void 0 ? void 0 : astMap.forEach((value, key) => {
375
375
  const rm = value.rm;
376
- functions[key] = asyncCompile ? util_1.generateGFunction(rm) : util_1.generateSyncedGFunction(rm);
376
+ functions[key] = asyncCompile ? (0, util_1.generateGFunction)(rm) : (0, util_1.generateSyncedGFunction)(rm);
377
377
  });
378
378
  let expString;
379
379
  if (!matcher) {
380
380
  expString = (_b = (_a = this.model.model.get('m')) === null || _a === void 0 ? void 0 : _a.get(enforceContext.mType)) === null || _b === void 0 ? void 0 : _b.value;
381
381
  }
382
382
  else {
383
- expString = util_1.removeComments(util_1.escapeAssertion(matcher));
383
+ expString = (0, util_1.removeComments)((0, util_1.escapeAssertion)(matcher));
384
384
  }
385
385
  if (!expString) {
386
386
  throw new Error('Unable to find matchers in model');
@@ -389,7 +389,7 @@ class CoreEnforcer {
389
389
  if (!effectExpr) {
390
390
  throw new Error('Unable to find policy_effect in model');
391
391
  }
392
- const HasEval = util_1.hasEval(expString);
392
+ const HasEval = (0, util_1.hasEval)(expString);
393
393
  let expression = undefined;
394
394
  const p = (_e = this.model.model.get('p')) === null || _e === void 0 ? void 0 : _e.get(enforceContext.pType);
395
395
  const policyLen = (_f = p === null || p === void 0 ? void 0 : p.policy) === null || _f === void 0 ? void 0 : _f.length;
@@ -422,12 +422,12 @@ class CoreEnforcer {
422
422
  parameters[token] = p === null || p === void 0 ? void 0 : p.policy[i][j];
423
423
  });
424
424
  if (HasEval) {
425
- const ruleNames = util_1.getEvalValue(expString);
425
+ const ruleNames = (0, util_1.getEvalValue)(expString);
426
426
  let expWithRule = expString;
427
427
  for (const ruleName of ruleNames) {
428
428
  if (ruleName in parameters) {
429
- const rule = util_1.escapeAssertion(parameters[ruleName]);
430
- expWithRule = util_1.replaceEval(expWithRule, ruleName, rule);
429
+ const rule = (0, util_1.escapeAssertion)(parameters[ruleName]);
430
+ expWithRule = (0, util_1.replaceEval)(expWithRule, ruleName, rule);
431
431
  }
432
432
  else {
433
433
  throw new Error(`${ruleName} not in ${parameters}`);
@@ -509,7 +509,7 @@ class CoreEnforcer {
509
509
  const res = effectStream.current();
510
510
  // only generate the request --> result string if the message
511
511
  // is going to be logged.
512
- if (log_1.getLogger().isEnable()) {
512
+ if ((0, log_1.getLogger)().isEnable()) {
513
513
  let reqStr = 'Request: ';
514
514
  for (let i = 0; i < rvals.length; i++) {
515
515
  if (i !== rvals.length - 1) {
@@ -520,7 +520,7 @@ class CoreEnforcer {
520
520
  }
521
521
  }
522
522
  reqStr += ` ---> ${res}`;
523
- log_1.logPrint(reqStr);
523
+ (0, log_1.logPrint)(reqStr);
524
524
  }
525
525
  if (explain) {
526
526
  if (explainIndex === -1) {
@@ -543,9 +543,9 @@ class CoreEnforcer {
543
543
  enforceSync(...rvals) {
544
544
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
545
545
  const enforceContext = rvals.shift();
546
- return util_1.generatorRunSync(this.privateEnforce(false, false, '', enforceContext, ...rvals));
546
+ return (0, util_1.generatorRunSync)(this.privateEnforce(false, false, '', enforceContext, ...rvals));
547
547
  }
548
- return util_1.generatorRunSync(this.privateEnforce(false, false, '', this.defaultEnforceContext, ...rvals));
548
+ return (0, util_1.generatorRunSync)(this.privateEnforce(false, false, '', this.defaultEnforceContext, ...rvals));
549
549
  }
550
550
  /**
551
551
  * If the matchers does not contain an asynchronous method, call it faster.
@@ -560,9 +560,9 @@ class CoreEnforcer {
560
560
  enforceExSync(...rvals) {
561
561
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
562
562
  const enforceContext = rvals.shift();
563
- return util_1.generatorRunSync(this.privateEnforce(false, true, '', enforceContext, ...rvals));
563
+ return (0, util_1.generatorRunSync)(this.privateEnforce(false, true, '', enforceContext, ...rvals));
564
564
  }
565
- return util_1.generatorRunSync(this.privateEnforce(false, true, '', this.defaultEnforceContext, ...rvals));
565
+ return (0, util_1.generatorRunSync)(this.privateEnforce(false, true, '', this.defaultEnforceContext, ...rvals));
566
566
  }
567
567
  /**
568
568
  * Same as enforceSync. To be removed.
@@ -581,9 +581,9 @@ class CoreEnforcer {
581
581
  async enforce(...rvals) {
582
582
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
583
583
  const enforceContext = rvals.shift();
584
- return util_1.generatorRunAsync(this.privateEnforce(true, false, '', enforceContext, ...rvals));
584
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, false, '', enforceContext, ...rvals));
585
585
  }
586
- return util_1.generatorRunAsync(this.privateEnforce(true, false, '', this.defaultEnforceContext, ...rvals));
586
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, false, '', this.defaultEnforceContext, ...rvals));
587
587
  }
588
588
  /**
589
589
  * enforceWithMatcher decides whether a "subject" can access a "object" with
@@ -598,9 +598,9 @@ class CoreEnforcer {
598
598
  async enforceWithMatcher(matcher, ...rvals) {
599
599
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
600
600
  const enforceContext = rvals.shift();
601
- return util_1.generatorRunAsync(this.privateEnforce(true, false, matcher, enforceContext, ...rvals));
601
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, false, matcher, enforceContext, ...rvals));
602
602
  }
603
- return util_1.generatorRunAsync(this.privateEnforce(true, false, matcher, this.defaultEnforceContext, ...rvals));
603
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, false, matcher, this.defaultEnforceContext, ...rvals));
604
604
  }
605
605
  /**
606
606
  * enforce decides whether a "subject" can access a "object" with
@@ -613,9 +613,9 @@ class CoreEnforcer {
613
613
  async enforceEx(...rvals) {
614
614
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
615
615
  const enforceContext = rvals.shift();
616
- return util_1.generatorRunAsync(this.privateEnforce(true, true, '', enforceContext, ...rvals));
616
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, true, '', enforceContext, ...rvals));
617
617
  }
618
- return util_1.generatorRunAsync(this.privateEnforce(true, true, '', this.defaultEnforceContext, ...rvals));
618
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, true, '', this.defaultEnforceContext, ...rvals));
619
619
  }
620
620
  /**
621
621
  * enforceExWithMatcher decides whether a "subject" can access a "object" with
@@ -630,9 +630,9 @@ class CoreEnforcer {
630
630
  async enforceExWithMatcher(matcher, ...rvals) {
631
631
  if (rvals[0] instanceof enforceContext_1.EnforceContext) {
632
632
  const enforceContext = rvals.shift();
633
- return util_1.generatorRunAsync(this.privateEnforce(true, true, matcher, enforceContext, ...rvals));
633
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, true, matcher, enforceContext, ...rvals));
634
634
  }
635
- return util_1.generatorRunAsync(this.privateEnforce(true, true, matcher, this.defaultEnforceContext, ...rvals));
635
+ return (0, util_1.generatorRunAsync)(this.privateEnforce(true, true, matcher, this.defaultEnforceContext, ...rvals));
636
636
  }
637
637
  /**
638
638
  * batchEnforce enforces each request and returns result in a bool array.
@@ -30,14 +30,14 @@ class DefaultEffectorStream {
30
30
  }
31
31
  pushEffect(eft) {
32
32
  switch (this.expr) {
33
- case "some(where (p_eft == allow))" /* ALLOW */:
33
+ case "some(where (p_eft == allow))" /* EffectExpress.ALLOW */:
34
34
  if (eft === effector_1.Effect.Allow) {
35
35
  this.res = true;
36
36
  this.done = true;
37
37
  this.rec = true;
38
38
  }
39
39
  break;
40
- case "!some(where (p_eft == deny))" /* DENY */:
40
+ case "!some(where (p_eft == deny))" /* EffectExpress.DENY */:
41
41
  this.res = true;
42
42
  if (eft === effector_1.Effect.Deny) {
43
43
  this.res = false;
@@ -45,7 +45,7 @@ class DefaultEffectorStream {
45
45
  this.rec = true;
46
46
  }
47
47
  break;
48
- case "some(where (p_eft == allow)) && !some(where (p_eft == deny))" /* ALLOW_AND_DENY */:
48
+ case "some(where (p_eft == allow)) && !some(where (p_eft == deny))" /* EffectExpress.ALLOW_AND_DENY */:
49
49
  if (eft === effector_1.Effect.Allow) {
50
50
  this.res = true;
51
51
  this.rec = true;
@@ -59,8 +59,8 @@ class DefaultEffectorStream {
59
59
  this.rec = false;
60
60
  }
61
61
  break;
62
- case "priority(p_eft) || deny" /* PRIORITY */:
63
- case "subjectPriority(p_eft) || deny" /* SUBJECT_PRIORITY */:
62
+ case "priority(p_eft) || deny" /* EffectExpress.PRIORITY */:
63
+ case "subjectPriority(p_eft) || deny" /* EffectExpress.SUBJECT_PRIORITY */:
64
64
  if (eft !== effector_1.Effect.Indeterminate) {
65
65
  this.res = eft === effector_1.Effect.Allow;
66
66
  this.done = true;
@@ -14,13 +14,17 @@
14
14
  // limitations under the License.
15
15
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
16
  if (k2 === undefined) k2 = k;
17
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
17
+ var desc = Object.getOwnPropertyDescriptor(m, k);
18
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
19
+ desc = { enumerable: true, get: function() { return m[k]; } };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
18
22
  }) : (function(o, m, k, k2) {
19
23
  if (k2 === undefined) k2 = k;
20
24
  o[k2] = m[k];
21
25
  }));
22
26
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
23
- for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
27
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
28
  };
25
29
  Object.defineProperty(exports, "__esModule", { value: true });
26
30
  __exportStar(require("./effector"), exports);
@@ -23,6 +23,7 @@ class EnforceContext {
23
23
  }
24
24
  }
25
25
  exports.EnforceContext = EnforceContext;
26
- exports.newEnforceContext = (index) => {
26
+ const newEnforceContext = (index) => {
27
27
  return new EnforceContext('r' + index, 'p' + index, 'e' + index, 'm' + index);
28
28
  };
29
+ exports.newEnforceContext = newEnforceContext;
@@ -14,7 +14,11 @@
14
14
  // limitations under the License.
15
15
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
16
  if (k2 === undefined) k2 = k;
17
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
17
+ var desc = Object.getOwnPropertyDescriptor(m, k);
18
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
19
+ desc = { enumerable: true, get: function() { return m[k]; } };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
18
22
  }) : (function(o, m, k, k2) {
19
23
  if (k2 === undefined) k2 = k;
20
24
  o[k2] = m[k];
@@ -27,7 +31,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
27
31
  var __importStar = (this && this.__importStar) || function (mod) {
28
32
  if (mod && mod.__esModule) return mod;
29
33
  var result = {};
30
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
34
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31
35
  __setModuleDefault(result, mod);
32
36
  return result;
33
37
  };
@@ -69,7 +73,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
69
73
  * @param lazyLoad whether to load policy at initial time
70
74
  */
71
75
  async initWithAdapter(modelPath, adapter, lazyLoad = false) {
72
- const m = model_1.newModelFromFile(modelPath, this.fs);
76
+ const m = (0, model_1.newModelFromFile)(modelPath, this.fs);
73
77
  await this.initWithModelAndAdapter(m, adapter, lazyLoad);
74
78
  this.modelPath = modelPath;
75
79
  }
@@ -200,7 +204,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
200
204
  throw new Error('user must not be empty');
201
205
  }
202
206
  if (domain === undefined) {
203
- const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
207
+ const subIndex = this.getFieldIndex('p', "sub" /* FieldIndex.Subject */);
204
208
  return this.removeFilteredGroupingPolicy(subIndex, user);
205
209
  }
206
210
  else {
@@ -218,7 +222,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
218
222
  if (!user) {
219
223
  throw new Error('user must not be empty');
220
224
  }
221
- const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
225
+ const subIndex = this.getFieldIndex('p', "sub" /* FieldIndex.Subject */);
222
226
  const res1 = await this.removeFilteredGroupingPolicy(subIndex, user);
223
227
  const res2 = await this.removeFilteredPolicy(subIndex, user);
224
228
  return res1 || res2;
@@ -234,7 +238,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
234
238
  if (!role) {
235
239
  throw new Error('role must not be empty');
236
240
  }
237
- const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
241
+ const subIndex = this.getFieldIndex('p', "sub" /* FieldIndex.Subject */);
238
242
  const res1 = await this.removeFilteredGroupingPolicy(subIndex, role);
239
243
  const res2 = await this.removeFilteredPolicy(subIndex, role);
240
244
  return res1 || res2;
@@ -290,7 +294,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
290
294
  if (!user) {
291
295
  throw new Error('user must not be empty');
292
296
  }
293
- const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
297
+ const subIndex = this.getFieldIndex('p', "sub" /* FieldIndex.Subject */);
294
298
  return this.removeFilteredPolicy(subIndex, user);
295
299
  }
296
300
  /**
@@ -300,7 +304,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
300
304
  * @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
301
305
  */
302
306
  async getPermissionsForUser(user) {
303
- const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
307
+ const subIndex = this.getFieldIndex('p', "sub" /* FieldIndex.Subject */);
304
308
  return this.getFilteredPolicy(subIndex, user);
305
309
  }
306
310
  /**
@@ -468,7 +472,7 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
468
472
  async getImplicitUsersForPermission(...permission) {
469
473
  const res = [];
470
474
  const policySubjects = await this.getAllSubjects();
471
- const subjects = util_1.arrayRemoveDuplicates([...policySubjects, ...this.model.getValuesForFieldInPolicyAllTypes('g', 0)]);
475
+ const subjects = (0, util_1.arrayRemoveDuplicates)([...policySubjects, ...this.model.getValuesForFieldInPolicyAllTypes('g', 0)]);
472
476
  const inherits = this.model.getValuesForFieldInPolicyAllTypes('g', 1);
473
477
  for (const user of subjects) {
474
478
  const allowed = await this.enforce(user, ...permission);
@@ -498,14 +502,14 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
498
502
  const domain = await rm.getAllDomains();
499
503
  domains.push(...domain);
500
504
  }
501
- return util_1.arrayRemoveDuplicates(domains);
505
+ return (0, util_1.arrayRemoveDuplicates)(domains);
502
506
  }
503
507
  }
504
508
  exports.Enforcer = Enforcer;
505
509
  async function newEnforcerWithClass(enforcer, ...params) {
506
510
  var _a;
507
511
  // inject the FS
508
- if (!persist_1.getDefaultFileSystem()) {
512
+ if (!(0, persist_1.getDefaultFileSystem)()) {
509
513
  try {
510
514
  if (typeof process !== 'undefined' && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node)) {
511
515
  const fs = await Promise.resolve().then(() => __importStar(require('fs')));
@@ -517,7 +521,7 @@ async function newEnforcerWithClass(enforcer, ...params) {
517
521
  return fs.writeFileSync(path, text, encoding);
518
522
  },
519
523
  };
520
- persist_1.setDefaultFileSystem(defaultFileSystem);
524
+ (0, persist_1.setDefaultFileSystem)(defaultFileSystem);
521
525
  }
522
526
  }
523
527
  catch (ignored) { }
@@ -527,7 +531,7 @@ async function newEnforcerWithClass(enforcer, ...params) {
527
531
  if (params.length >= 1) {
528
532
  const enableLog = params[params.length - 1];
529
533
  if (typeof enableLog === 'boolean') {
530
- log_1.getLogger().enableLog(enableLog);
534
+ (0, log_1.getLogger)().enableLog(enableLog);
531
535
  parsedParamLen++;
532
536
  }
533
537
  }
@@ -40,8 +40,8 @@ async function casbinJsGetPermissionForUser(e, user) {
40
40
  s += '[matchers]\n';
41
41
  s += `m = ${(_l = (_k = m.get('m')) === null || _k === void 0 ? void 0 : _k.get('m')) === null || _l === void 0 ? void 0 : _l.value.replace(/_/g, '.')}`;
42
42
  obj['m'] = s;
43
- const policy = util_1.deepCopy(await e.getPolicy());
44
- const groupPolicy = util_1.deepCopy(await e.getGroupingPolicy());
43
+ const policy = (0, util_1.deepCopy)(await e.getPolicy());
44
+ const groupPolicy = (0, util_1.deepCopy)(await e.getGroupingPolicy());
45
45
  policy.forEach((item) => {
46
46
  item.unshift('p');
47
47
  });
package/lib/cjs/index.js CHANGED
@@ -14,7 +14,11 @@
14
14
  // limitations under the License.
15
15
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
16
  if (k2 === undefined) k2 = k;
17
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
17
+ var desc = Object.getOwnPropertyDescriptor(m, k);
18
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
19
+ desc = { enumerable: true, get: function() { return m[k]; } };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
18
22
  }) : (function(o, m, k, k2) {
19
23
  if (k2 === undefined) k2 = k;
20
24
  o[k2] = m[k];
@@ -27,12 +31,12 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
27
31
  var __importStar = (this && this.__importStar) || function (mod) {
28
32
  if (mod && mod.__esModule) return mod;
29
33
  var result = {};
30
- if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
34
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31
35
  __setModuleDefault(result, mod);
32
36
  return result;
33
37
  };
34
38
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
35
- for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
39
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
36
40
  };
37
41
  Object.defineProperty(exports, "__esModule", { value: true });
38
42
  exports.Util = void 0;
@@ -35,4 +35,12 @@ export declare class InternalEnforcer extends CoreEnforcer {
35
35
  * set index of field
36
36
  */
37
37
  setFieldIndex(ptype: string, field: string, index: number): void;
38
+ protected addPolicyWithoutNotify(sec: string, ptype: string, rule: string[]): Promise<boolean>;
39
+ protected addPoliciesWithoutNotify(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
40
+ protected addPoliciesWithoutNotifyEx(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
41
+ protected updatePolicyWithoutNotify(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean>;
42
+ protected removePolicyWithoutNotify(sec: string, ptype: string, rule: string[]): Promise<boolean>;
43
+ protected removePoliciesWithoutNotify(sec: string, ptype: string, rules: string[][]): Promise<boolean>;
44
+ protected removeFilteredPolicyWithoutNotify(sec: string, ptype: string, fieldIndex: number, fieldValues: string[]): Promise<boolean>;
45
+ protected updatePoliciesWithoutNotify(sec: string, ptype: string, oldRules: string[][], newRules: string[][]): Promise<boolean>;
38
46
  }
@@ -27,12 +27,13 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
27
27
  if (this.model.hasPolicy(sec, ptype, rule)) {
28
28
  return false;
29
29
  }
30
+ // Persist when an adapter is configured and autoSave is enabled.
30
31
  if (this.adapter && this.autoSave) {
31
32
  try {
32
33
  await this.adapter.addPolicy(sec, ptype, rule);
33
34
  }
34
35
  catch (e) {
35
- if (e.message !== 'not implemented') {
36
+ if (e instanceof Error && e.message !== 'not implemented') {
36
37
  throw e;
37
38
  }
38
39
  }
@@ -62,13 +63,14 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
62
63
  return false;
63
64
  }
64
65
  }
65
- if (this.autoSave) {
66
+ // Persist when an adapter is configured and autoSave is enabled.
67
+ if (this.adapter && this.autoSave) {
66
68
  if ('addPolicies' in this.adapter) {
67
69
  try {
68
70
  await this.adapter.addPolicies(sec, ptype, rules);
69
71
  }
70
72
  catch (e) {
71
- if (e.message !== 'not implemented') {
73
+ if (e instanceof Error && e.message !== 'not implemented') {
72
74
  throw e;
73
75
  }
74
76
  }
@@ -106,13 +108,14 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
106
108
  if (newRules.length === 0) {
107
109
  return false;
108
110
  }
109
- if (this.autoSave) {
111
+ // Persist when an adapter is configured and autoSave is enabled.
112
+ if (this.adapter && this.autoSave) {
110
113
  if ('addPolicies' in this.adapter) {
111
114
  try {
112
115
  await this.adapter.addPolicies(sec, ptype, newRules);
113
116
  }
114
117
  catch (e) {
115
- if (e.message !== 'not implemented') {
118
+ if (e instanceof Error && e.message !== 'not implemented') {
116
119
  throw e;
117
120
  }
118
121
  }
@@ -145,13 +148,14 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
145
148
  if (!this.model.hasPolicy(sec, ptype, oldRule)) {
146
149
  return false;
147
150
  }
148
- if (this.autoSave) {
151
+ // Persist when an adapter is configured and autoSave is enabled.
152
+ if (this.adapter && this.autoSave) {
149
153
  if ('updatePolicy' in this.adapter) {
150
154
  try {
151
155
  await this.adapter.updatePolicy(sec, ptype, oldRule, newRule);
152
156
  }
153
157
  catch (e) {
154
- if (e.message !== 'not implemented') {
158
+ if (e instanceof Error && e.message !== 'not implemented') {
155
159
  throw e;
156
160
  }
157
161
  }
@@ -183,12 +187,13 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
183
187
  if (!this.model.hasPolicy(sec, ptype, rule)) {
184
188
  return false;
185
189
  }
190
+ // Persist when an adapter is configured and autoSave is enabled.
186
191
  if (this.adapter && this.autoSave) {
187
192
  try {
188
193
  await this.adapter.removePolicy(sec, ptype, rule);
189
194
  }
190
195
  catch (e) {
191
- if (e.message !== 'not implemented') {
196
+ if (e instanceof Error && e.message !== 'not implemented') {
192
197
  throw e;
193
198
  }
194
199
  }
@@ -217,13 +222,14 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
217
222
  return false;
218
223
  }
219
224
  }
220
- if (this.autoSave) {
225
+ // Persist when an adapter is configured and autoSave is enabled.
226
+ if (this.adapter && this.autoSave) {
221
227
  if ('removePolicies' in this.adapter) {
222
228
  try {
223
229
  await this.adapter.removePolicies(sec, ptype, rules);
224
230
  }
225
231
  catch (e) {
226
- if (e.message !== 'not implemented') {
232
+ if (e instanceof Error && e.message !== 'not implemented') {
227
233
  throw e;
228
234
  }
229
235
  }
@@ -253,12 +259,13 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
253
259
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
254
260
  */
255
261
  async removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues, useWatcher) {
262
+ // Persist when an adapter is configured and autoSave is enabled.
256
263
  if (this.adapter && this.autoSave) {
257
264
  try {
258
265
  await this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
259
266
  }
260
267
  catch (e) {
261
- if (e.message !== 'not implemented') {
268
+ if (e instanceof Error && e.message !== 'not implemented') {
262
269
  throw e;
263
270
  }
264
271
  }
@@ -294,5 +301,92 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
294
301
  const assertion = (_a = this.model.model.get('p')) === null || _a === void 0 ? void 0 : _a.get(ptype);
295
302
  assertion === null || assertion === void 0 ? void 0 : assertion.fieldIndexMap.set(field, index);
296
303
  }
304
+ async addPolicyWithoutNotify(sec, ptype, rule) {
305
+ if (this.model.hasPolicy(sec, ptype, rule)) {
306
+ return false;
307
+ }
308
+ const ok = this.model.addPolicy(sec, ptype, rule);
309
+ if (sec === 'g' && ok) {
310
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, [rule]);
311
+ }
312
+ return ok;
313
+ }
314
+ async addPoliciesWithoutNotify(sec, ptype, rules) {
315
+ for (const rule of rules) {
316
+ if (this.model.hasPolicy(sec, ptype, rule)) {
317
+ return false;
318
+ }
319
+ }
320
+ const [ok, effects] = await this.model.addPolicies(sec, ptype, rules);
321
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
322
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, effects);
323
+ }
324
+ return ok;
325
+ }
326
+ async addPoliciesWithoutNotifyEx(sec, ptype, rules) {
327
+ const newRules = rules.filter((rule) => !this.model.hasPolicy(sec, ptype, rule));
328
+ if (newRules.length === 0) {
329
+ return false;
330
+ }
331
+ const [ok, effects] = await this.model.addPolicies(sec, ptype, newRules);
332
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
333
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, effects);
334
+ }
335
+ return ok;
336
+ }
337
+ async updatePolicyWithoutNotify(sec, ptype, oldRule, newRule) {
338
+ if (!this.model.hasPolicy(sec, ptype, oldRule)) {
339
+ return false;
340
+ }
341
+ const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule);
342
+ if (sec === 'g' && ok) {
343
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, [oldRule]);
344
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, [newRule]);
345
+ }
346
+ return ok;
347
+ }
348
+ async removePolicyWithoutNotify(sec, ptype, rule) {
349
+ if (!this.model.hasPolicy(sec, ptype, rule)) {
350
+ return false;
351
+ }
352
+ const ok = await this.model.removePolicy(sec, ptype, rule);
353
+ if (sec === 'g' && ok) {
354
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, [rule]);
355
+ }
356
+ return ok;
357
+ }
358
+ async removePoliciesWithoutNotify(sec, ptype, rules) {
359
+ for (const rule of rules) {
360
+ if (!this.model.hasPolicy(sec, ptype, rule)) {
361
+ return false;
362
+ }
363
+ }
364
+ const [ok, effects] = this.model.removePolicies(sec, ptype, rules);
365
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
366
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, effects);
367
+ }
368
+ return ok;
369
+ }
370
+ async removeFilteredPolicyWithoutNotify(sec, ptype, fieldIndex, fieldValues) {
371
+ const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues);
372
+ if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) {
373
+ await this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, effects);
374
+ }
375
+ return ok;
376
+ }
377
+ async updatePoliciesWithoutNotify(sec, ptype, oldRules, newRules) {
378
+ // Mirror the Go updatePoliciesWithoutNotify; reuse the existing internal flow.
379
+ // Because updatePoliciesInternal isn't implemented yet, fall back to per-item updates.
380
+ if (oldRules.length !== newRules.length) {
381
+ throw new Error('the length of oldRules should be equal to the length of newRules');
382
+ }
383
+ for (let i = 0; i < oldRules.length; i++) {
384
+ const ok = await this.updatePolicyWithoutNotify(sec, ptype, oldRules[i], newRules[i]);
385
+ if (!ok) {
386
+ return false;
387
+ }
388
+ }
389
+ return true;
390
+ }
297
391
  }
298
392
  exports.InternalEnforcer = InternalEnforcer;
@@ -14,13 +14,17 @@
14
14
  // limitations under the License.
15
15
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
16
  if (k2 === undefined) k2 = k;
17
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
17
+ var desc = Object.getOwnPropertyDescriptor(m, k);
18
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
19
+ desc = { enumerable: true, get: function() { return m[k]; } };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
18
22
  }) : (function(o, m, k, k2) {
19
23
  if (k2 === undefined) k2 = k;
20
24
  o[k2] = m[k];
21
25
  }));
22
26
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
23
- for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
27
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
28
  };
25
29
  Object.defineProperty(exports, "__esModule", { value: true });
26
30
  __exportStar(require("./defaultLogger"), exports);