casbin 5.25.0 → 5.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [5.26.0](https://github.com/casbin/node-casbin/compare/v5.25.0...v5.26.0) (2023-03-26)
2
+
3
+
4
+ ### Features
5
+
6
+ * Multiple sections type ([#442](https://github.com/casbin/node-casbin/issues/442)) ([48b0d8f](https://github.com/casbin/node-casbin/commit/48b0d8fbbf302f61aed20bb6d0f2354354df9941))
7
+
1
8
  # [5.25.0](https://github.com/casbin/node-casbin/compare/v5.24.4...v5.25.0) (2023-03-20)
2
9
 
3
10
 
@@ -0,0 +1,2 @@
1
+ p2, alice, data1, read
2
+ p2, bob, data2, write
@@ -13,6 +13,7 @@ export declare class CoreEnforcer {
13
13
  protected fm: FunctionMap;
14
14
  protected eft: Effector;
15
15
  private matcherMap;
16
+ private defaultEnforceContext;
16
17
  protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter;
17
18
  protected watcher: Watcher | null;
18
19
  protected watcherEx: WatcherEx | null;
@@ -18,6 +18,7 @@ const expression_eval_1 = require("expression-eval");
18
18
  const effect_1 = require("./effect");
19
19
  const model_1 = require("./model");
20
20
  const rbac_1 = require("./rbac");
21
+ const enforceContext_1 = require("./enforceContext");
21
22
  const util_1 = require("./util");
22
23
  const log_1 = require("./log");
23
24
  /**
@@ -28,6 +29,7 @@ class CoreEnforcer {
28
29
  this.fm = model_1.FunctionMap.loadFunctionMap();
29
30
  this.eft = new effect_1.DefaultEffector();
30
31
  this.matcherMap = new Map();
32
+ this.defaultEnforceContext = new enforceContext_1.EnforceContext('r', 'p', 'e', 'm');
31
33
  this.watcher = null;
32
34
  this.watcherEx = null;
33
35
  this.enabled = true;
@@ -347,7 +349,7 @@ class CoreEnforcer {
347
349
  await this.model.buildRoleLinks(this.rmMap);
348
350
  }
349
351
  }
350
- *privateEnforce(asyncCompile = true, explain = false, ...rvals) {
352
+ *privateEnforce(asyncCompile = true, explain = false, enforceContext = new enforceContext_1.EnforceContext('r', 'p', 'e', 'm'), ...rvals) {
351
353
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
352
354
  if (!this.enabled) {
353
355
  return true;
@@ -362,19 +364,19 @@ class CoreEnforcer {
362
364
  const rm = value.rm;
363
365
  functions[key] = asyncCompile ? util_1.generateGFunction(rm) : util_1.generateSyncedGFunction(rm);
364
366
  });
365
- const expString = (_b = (_a = this.model.model.get('m')) === null || _a === void 0 ? void 0 : _a.get('m')) === null || _b === void 0 ? void 0 : _b.value;
367
+ const 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;
366
368
  if (!expString) {
367
369
  throw new Error('Unable to find matchers in model');
368
370
  }
369
- const effectExpr = (_d = (_c = this.model.model.get('e')) === null || _c === void 0 ? void 0 : _c.get('e')) === null || _d === void 0 ? void 0 : _d.value;
371
+ const effectExpr = (_d = (_c = this.model.model.get('e')) === null || _c === void 0 ? void 0 : _c.get(enforceContext.eType)) === null || _d === void 0 ? void 0 : _d.value;
370
372
  if (!effectExpr) {
371
373
  throw new Error('Unable to find policy_effect in model');
372
374
  }
373
375
  const HasEval = util_1.hasEval(expString);
374
376
  let expression = undefined;
375
- const p = (_e = this.model.model.get('p')) === null || _e === void 0 ? void 0 : _e.get('p');
377
+ const p = (_e = this.model.model.get('p')) === null || _e === void 0 ? void 0 : _e.get(enforceContext.pType);
376
378
  const policyLen = (_f = p === null || p === void 0 ? void 0 : p.policy) === null || _f === void 0 ? void 0 : _f.length;
377
- const rTokens = (_h = (_g = this.model.model.get('r')) === null || _g === void 0 ? void 0 : _g.get('r')) === null || _h === void 0 ? void 0 : _h.tokens;
379
+ const rTokens = (_h = (_g = this.model.model.get('r')) === null || _g === void 0 ? void 0 : _g.get(enforceContext.rType)) === null || _h === void 0 ? void 0 : _h.tokens;
378
380
  const rTokensLen = rTokens === null || rTokens === void 0 ? void 0 : rTokens.length;
379
381
  const effectStream = this.eft.newStream(effectExpr);
380
382
  if (policyLen && policyLen !== 0) {
@@ -509,7 +511,11 @@ class CoreEnforcer {
509
511
  * @return whether to allow the request.
510
512
  */
511
513
  enforceSync(...rvals) {
512
- return util_1.generatorRunSync(this.privateEnforce(false, false, ...rvals));
514
+ if (rvals[0] instanceof enforceContext_1.EnforceContext) {
515
+ const enforceContext = rvals.shift();
516
+ return util_1.generatorRunSync(this.privateEnforce(false, false, enforceContext, ...rvals));
517
+ }
518
+ return util_1.generatorRunSync(this.privateEnforce(false, false, this.defaultEnforceContext, ...rvals));
513
519
  }
514
520
  /**
515
521
  * If the matchers does not contain an asynchronous method, call it faster.
@@ -522,7 +528,11 @@ class CoreEnforcer {
522
528
  * @return whether to allow the request and the reason rule.
523
529
  */
524
530
  enforceExSync(...rvals) {
525
- return util_1.generatorRunSync(this.privateEnforce(false, true, ...rvals));
531
+ if (rvals[0] instanceof enforceContext_1.EnforceContext) {
532
+ const enforceContext = rvals.shift();
533
+ return util_1.generatorRunSync(this.privateEnforce(false, true, enforceContext, ...rvals));
534
+ }
535
+ return util_1.generatorRunSync(this.privateEnforce(false, true, this.defaultEnforceContext, ...rvals));
526
536
  }
527
537
  /**
528
538
  * Same as enforceSync. To be removed.
@@ -539,7 +549,11 @@ class CoreEnforcer {
539
549
  * @return whether to allow the request.
540
550
  */
541
551
  async enforce(...rvals) {
542
- return util_1.generatorRunAsync(this.privateEnforce(true, false, ...rvals));
552
+ if (rvals[0] instanceof enforceContext_1.EnforceContext) {
553
+ const enforceContext = rvals.shift();
554
+ return util_1.generatorRunAsync(this.privateEnforce(true, false, enforceContext, ...rvals));
555
+ }
556
+ return util_1.generatorRunAsync(this.privateEnforce(true, false, this.defaultEnforceContext, ...rvals));
543
557
  }
544
558
  /**
545
559
  * enforce decides whether a "subject" can access a "object" with
@@ -550,7 +564,11 @@ class CoreEnforcer {
550
564
  * @return whether to allow the request and the reason rule.
551
565
  */
552
566
  async enforceEx(...rvals) {
553
- return util_1.generatorRunAsync(this.privateEnforce(true, true, ...rvals));
567
+ if (rvals[0] instanceof enforceContext_1.EnforceContext) {
568
+ const enforceContext = rvals.shift();
569
+ return util_1.generatorRunAsync(this.privateEnforce(true, true, enforceContext, ...rvals));
570
+ }
571
+ return util_1.generatorRunAsync(this.privateEnforce(true, true, this.defaultEnforceContext, ...rvals));
554
572
  }
555
573
  /**
556
574
  * batchEnforce enforces each request and returns result in a bool array.
@@ -0,0 +1,8 @@
1
+ export declare class EnforceContext {
2
+ pType: string;
3
+ rType: string;
4
+ eType: string;
5
+ mType: string;
6
+ constructor(rType: string, pType: string, eType: string, mType: string);
7
+ }
8
+ export declare const newEnforceContext: (index: string) => EnforceContext;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2023 The Casbin Authors. All Rights Reserved.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.newEnforceContext = exports.EnforceContext = void 0;
17
+ class EnforceContext {
18
+ constructor(rType, pType, eType, mType) {
19
+ this.pType = pType;
20
+ this.eType = eType;
21
+ this.mType = mType;
22
+ this.rType = rType;
23
+ }
24
+ }
25
+ exports.EnforceContext = EnforceContext;
26
+ exports.newEnforceContext = (index) => {
27
+ return new EnforceContext('r' + index, 'p' + index, 'e' + index, 'm' + index);
28
+ };
@@ -8,5 +8,6 @@ export * from './model';
8
8
  export * from './persist';
9
9
  export * from './rbac';
10
10
  export * from './log';
11
+ export * from './enforceContext';
11
12
  export * from './frontend';
12
13
  export { Util };
package/lib/cjs/index.js CHANGED
@@ -47,4 +47,5 @@ __exportStar(require("./model"), exports);
47
47
  __exportStar(require("./persist"), exports);
48
48
  __exportStar(require("./rbac"), exports);
49
49
  __exportStar(require("./log"), exports);
50
+ __exportStar(require("./enforceContext"), exports);
50
51
  __exportStar(require("./frontend"), exports);
@@ -18,8 +18,12 @@ exports.bracketCompatible = exports.customIn = exports.deepCopy = exports.genera
18
18
  // because the expression evaluation doesn't support such variable names.
19
19
  const persist_1 = require("../persist");
20
20
  function escapeAssertion(s) {
21
- s = s.replace(/(?<!\w)r\./g, 'r_');
22
- s = s.replace(/(?<!\w)p\./g, 'p_');
21
+ s = s.replace(/(?<!\w)r[0-9]*\./g, (match) => {
22
+ return match.replace('.', '_');
23
+ });
24
+ s = s.replace(/(?<!\w)p[0-9]*\./g, (match) => {
25
+ return match.replace('.', '_');
26
+ });
23
27
  return s;
24
28
  }
25
29
  exports.escapeAssertion = escapeAssertion;
@@ -13,6 +13,7 @@ export declare class CoreEnforcer {
13
13
  protected fm: FunctionMap;
14
14
  protected eft: Effector;
15
15
  private matcherMap;
16
+ private defaultEnforceContext;
16
17
  protected adapter: UpdatableAdapter | FilteredAdapter | Adapter | BatchAdapter;
17
18
  protected watcher: Watcher | null;
18
19
  protected watcherEx: WatcherEx | null;
@@ -15,6 +15,7 @@ import { compile, compileAsync, addBinaryOp } from 'expression-eval';
15
15
  import { DefaultEffector, Effect } from './effect';
16
16
  import { FunctionMap, newModelFromFile } from './model';
17
17
  import { DefaultRoleManager } from './rbac';
18
+ import { EnforceContext } from './enforceContext';
18
19
  import { escapeAssertion, generateGFunction, generateSyncedGFunction, getEvalValue, hasEval, replaceEval, generatorRunSync, generatorRunAsync, customIn, bracketCompatible, } from './util';
19
20
  import { getLogger, logPrint } from './log';
20
21
  /**
@@ -25,6 +26,7 @@ export class CoreEnforcer {
25
26
  this.fm = FunctionMap.loadFunctionMap();
26
27
  this.eft = new DefaultEffector();
27
28
  this.matcherMap = new Map();
29
+ this.defaultEnforceContext = new EnforceContext('r', 'p', 'e', 'm');
28
30
  this.watcher = null;
29
31
  this.watcherEx = null;
30
32
  this.enabled = true;
@@ -344,7 +346,7 @@ export class CoreEnforcer {
344
346
  await this.model.buildRoleLinks(this.rmMap);
345
347
  }
346
348
  }
347
- *privateEnforce(asyncCompile = true, explain = false, ...rvals) {
349
+ *privateEnforce(asyncCompile = true, explain = false, enforceContext = new EnforceContext('r', 'p', 'e', 'm'), ...rvals) {
348
350
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
349
351
  if (!this.enabled) {
350
352
  return true;
@@ -359,19 +361,19 @@ export class CoreEnforcer {
359
361
  const rm = value.rm;
360
362
  functions[key] = asyncCompile ? generateGFunction(rm) : generateSyncedGFunction(rm);
361
363
  });
362
- const expString = (_b = (_a = this.model.model.get('m')) === null || _a === void 0 ? void 0 : _a.get('m')) === null || _b === void 0 ? void 0 : _b.value;
364
+ const 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;
363
365
  if (!expString) {
364
366
  throw new Error('Unable to find matchers in model');
365
367
  }
366
- const effectExpr = (_d = (_c = this.model.model.get('e')) === null || _c === void 0 ? void 0 : _c.get('e')) === null || _d === void 0 ? void 0 : _d.value;
368
+ const effectExpr = (_d = (_c = this.model.model.get('e')) === null || _c === void 0 ? void 0 : _c.get(enforceContext.eType)) === null || _d === void 0 ? void 0 : _d.value;
367
369
  if (!effectExpr) {
368
370
  throw new Error('Unable to find policy_effect in model');
369
371
  }
370
372
  const HasEval = hasEval(expString);
371
373
  let expression = undefined;
372
- const p = (_e = this.model.model.get('p')) === null || _e === void 0 ? void 0 : _e.get('p');
374
+ const p = (_e = this.model.model.get('p')) === null || _e === void 0 ? void 0 : _e.get(enforceContext.pType);
373
375
  const policyLen = (_f = p === null || p === void 0 ? void 0 : p.policy) === null || _f === void 0 ? void 0 : _f.length;
374
- const rTokens = (_h = (_g = this.model.model.get('r')) === null || _g === void 0 ? void 0 : _g.get('r')) === null || _h === void 0 ? void 0 : _h.tokens;
376
+ const rTokens = (_h = (_g = this.model.model.get('r')) === null || _g === void 0 ? void 0 : _g.get(enforceContext.rType)) === null || _h === void 0 ? void 0 : _h.tokens;
375
377
  const rTokensLen = rTokens === null || rTokens === void 0 ? void 0 : rTokens.length;
376
378
  const effectStream = this.eft.newStream(effectExpr);
377
379
  if (policyLen && policyLen !== 0) {
@@ -506,7 +508,11 @@ export class CoreEnforcer {
506
508
  * @return whether to allow the request.
507
509
  */
508
510
  enforceSync(...rvals) {
509
- return generatorRunSync(this.privateEnforce(false, false, ...rvals));
511
+ if (rvals[0] instanceof EnforceContext) {
512
+ const enforceContext = rvals.shift();
513
+ return generatorRunSync(this.privateEnforce(false, false, enforceContext, ...rvals));
514
+ }
515
+ return generatorRunSync(this.privateEnforce(false, false, this.defaultEnforceContext, ...rvals));
510
516
  }
511
517
  /**
512
518
  * If the matchers does not contain an asynchronous method, call it faster.
@@ -519,7 +525,11 @@ export class CoreEnforcer {
519
525
  * @return whether to allow the request and the reason rule.
520
526
  */
521
527
  enforceExSync(...rvals) {
522
- return generatorRunSync(this.privateEnforce(false, true, ...rvals));
528
+ if (rvals[0] instanceof EnforceContext) {
529
+ const enforceContext = rvals.shift();
530
+ return generatorRunSync(this.privateEnforce(false, true, enforceContext, ...rvals));
531
+ }
532
+ return generatorRunSync(this.privateEnforce(false, true, this.defaultEnforceContext, ...rvals));
523
533
  }
524
534
  /**
525
535
  * Same as enforceSync. To be removed.
@@ -536,7 +546,11 @@ export class CoreEnforcer {
536
546
  * @return whether to allow the request.
537
547
  */
538
548
  async enforce(...rvals) {
539
- return generatorRunAsync(this.privateEnforce(true, false, ...rvals));
549
+ if (rvals[0] instanceof EnforceContext) {
550
+ const enforceContext = rvals.shift();
551
+ return generatorRunAsync(this.privateEnforce(true, false, enforceContext, ...rvals));
552
+ }
553
+ return generatorRunAsync(this.privateEnforce(true, false, this.defaultEnforceContext, ...rvals));
540
554
  }
541
555
  /**
542
556
  * enforce decides whether a "subject" can access a "object" with
@@ -547,7 +561,11 @@ export class CoreEnforcer {
547
561
  * @return whether to allow the request and the reason rule.
548
562
  */
549
563
  async enforceEx(...rvals) {
550
- return generatorRunAsync(this.privateEnforce(true, true, ...rvals));
564
+ if (rvals[0] instanceof EnforceContext) {
565
+ const enforceContext = rvals.shift();
566
+ return generatorRunAsync(this.privateEnforce(true, true, enforceContext, ...rvals));
567
+ }
568
+ return generatorRunAsync(this.privateEnforce(true, true, this.defaultEnforceContext, ...rvals));
551
569
  }
552
570
  /**
553
571
  * batchEnforce enforces each request and returns result in a bool array.
@@ -0,0 +1,8 @@
1
+ export declare class EnforceContext {
2
+ pType: string;
3
+ rType: string;
4
+ eType: string;
5
+ mType: string;
6
+ constructor(rType: string, pType: string, eType: string, mType: string);
7
+ }
8
+ export declare const newEnforceContext: (index: string) => EnforceContext;
@@ -0,0 +1,24 @@
1
+ // Copyright 2023 The Casbin Authors. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export class EnforceContext {
15
+ constructor(rType, pType, eType, mType) {
16
+ this.pType = pType;
17
+ this.eType = eType;
18
+ this.mType = mType;
19
+ this.rType = rType;
20
+ }
21
+ }
22
+ export const newEnforceContext = (index) => {
23
+ return new EnforceContext('r' + index, 'p' + index, 'e' + index, 'm' + index);
24
+ };
@@ -8,5 +8,6 @@ export * from './model';
8
8
  export * from './persist';
9
9
  export * from './rbac';
10
10
  export * from './log';
11
+ export * from './enforceContext';
11
12
  export * from './frontend';
12
13
  export { Util };
package/lib/esm/index.js CHANGED
@@ -21,5 +21,6 @@ export * from './model';
21
21
  export * from './persist';
22
22
  export * from './rbac';
23
23
  export * from './log';
24
+ export * from './enforceContext';
24
25
  export * from './frontend';
25
26
  export { Util };
@@ -15,8 +15,12 @@
15
15
  // because the expression evaluation doesn't support such variable names.
16
16
  import { mustGetDefaultFileSystem } from '../persist';
17
17
  function escapeAssertion(s) {
18
- s = s.replace(/(?<!\w)r\./g, 'r_');
19
- s = s.replace(/(?<!\w)p\./g, 'p_');
18
+ s = s.replace(/(?<!\w)r[0-9]*\./g, (match) => {
19
+ return match.replace('.', '_');
20
+ });
21
+ s = s.replace(/(?<!\w)p[0-9]*\./g, (match) => {
22
+ return match.replace('.', '_');
23
+ });
20
24
  return s;
21
25
  }
22
26
  // removeComments removes the comments starting with # in the text.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casbin",
3
- "version": "5.25.0",
3
+ "version": "5.26.0",
4
4
  "description": "An authorization library that supports access control models like ACL, RBAC, ABAC in Node.JS",
5
5
  "main": "lib/cjs/index.js",
6
6
  "typings": "lib/cjs/index.d.ts",