casbin 5.18.0 → 5.19.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.19.0](https://github.com/casbin/node-casbin/compare/v5.18.0...v5.19.0) (2022-09-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * add `fieldIndexMap` ([#391](https://github.com/casbin/node-casbin/issues/391)) ([8f6e2c3](https://github.com/casbin/node-casbin/commit/8f6e2c39a19725e467aafda8b7fa948e3e471ce8))
7
+
1
8
  # [5.18.0](https://github.com/casbin/node-casbin/compare/v5.17.0...v5.18.0) (2022-09-14)
2
9
 
3
10
 
@@ -0,0 +1,13 @@
1
+ export declare const enum EffectExpress {
2
+ ALLOW = "some(where (p_eft == allow))",
3
+ DENY = "!some(where (p_eft == deny))",
4
+ ALLOW_AND_DENY = "some(where (p_eft == allow)) && !some(where (p_eft == deny))",
5
+ PRIORITY = "priority(p_eft) || deny",
6
+ SUBJECT_PRIORITY = "subjectPriority(p_eft) || deny"
7
+ }
8
+ export declare const enum FieldIndex {
9
+ Domain = "dom",
10
+ Subject = "sub",
11
+ Object = "obj",
12
+ Priority = "priority"
13
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // Copyright 2022 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 });
@@ -30,14 +30,14 @@ class DefaultEffectorStream {
30
30
  }
31
31
  pushEffect(eft) {
32
32
  switch (this.expr) {
33
- case 'some(where (p_eft == allow))':
33
+ case "some(where (p_eft == allow))" /* 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))':
40
+ case "!some(where (p_eft == deny))" /* 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))':
48
+ case "some(where (p_eft == allow)) && !some(where (p_eft == deny))" /* ALLOW_AND_DENY */:
49
49
  if (eft === effector_1.Effect.Allow) {
50
50
  this.res = true;
51
51
  this.rec = true;
@@ -59,7 +59,7 @@ class DefaultEffectorStream {
59
59
  this.rec = false;
60
60
  }
61
61
  break;
62
- case 'priority(p_eft) || deny':
62
+ case "priority(p_eft) || deny" /* PRIORITY */:
63
63
  if (eft !== effector_1.Effect.Indeterminate) {
64
64
  this.res = eft === effector_1.Effect.Allow;
65
65
  this.done = true;
@@ -172,7 +172,8 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
172
172
  */
173
173
  async deleteRolesForUser(user, domain) {
174
174
  if (domain === undefined) {
175
- return this.removeFilteredGroupingPolicy(0, user);
175
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
176
+ return this.removeFilteredGroupingPolicy(subIndex, user);
176
177
  }
177
178
  else {
178
179
  return this.removeFilteredGroupingPolicy(0, user, '', domain);
@@ -186,8 +187,9 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
186
187
  * @return succeeds or not.
187
188
  */
188
189
  async deleteUser(user) {
189
- const res1 = await this.removeFilteredGroupingPolicy(0, user);
190
- const res2 = await this.removeFilteredPolicy(0, user);
190
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
191
+ const res1 = await this.removeFilteredGroupingPolicy(subIndex, user);
192
+ const res2 = await this.removeFilteredPolicy(subIndex, user);
191
193
  return res1 || res2;
192
194
  }
193
195
  /**
@@ -198,8 +200,9 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
198
200
  * @return succeeds or not.
199
201
  */
200
202
  async deleteRole(role) {
201
- const res1 = await this.removeFilteredGroupingPolicy(1, role);
202
- const res2 = await this.removeFilteredPolicy(0, role);
203
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
204
+ const res1 = await this.removeFilteredGroupingPolicy(subIndex, role);
205
+ const res2 = await this.removeFilteredPolicy(subIndex, role);
203
206
  return res1 || res2;
204
207
  }
205
208
  /**
@@ -244,7 +247,8 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
244
247
  * @return succeeds or not.
245
248
  */
246
249
  async deletePermissionsForUser(user) {
247
- return this.removeFilteredPolicy(0, user);
250
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
251
+ return this.removeFilteredPolicy(subIndex, user);
248
252
  }
249
253
  /**
250
254
  * getPermissionsForUser gets permissions for a user or role.
@@ -253,7 +257,8 @@ class Enforcer extends managementEnforcer_1.ManagementEnforcer {
253
257
  * @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
254
258
  */
255
259
  async getPermissionsForUser(user) {
256
- return this.getFilteredPolicy(0, user);
260
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
261
+ return this.getFilteredPolicy(subIndex, user);
257
262
  }
258
263
  /**
259
264
  * hasPermissionForUser determines whether a user has a permission.
@@ -21,4 +21,12 @@ export declare class InternalEnforcer extends CoreEnforcer {
21
21
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
22
22
  */
23
23
  protected removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[], useWatcher: boolean): Promise<boolean>;
24
+ /**
25
+ * get field index in model.fieldMap.
26
+ */
27
+ getFieldIndex(ptype: string, field: string): number;
28
+ /**
29
+ * set index of field
30
+ */
31
+ setFieldIndex(ptype: string, field: string, index: number): void;
24
32
  }
@@ -234,5 +234,19 @@ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer {
234
234
  }
235
235
  return ok;
236
236
  }
237
+ /**
238
+ * get field index in model.fieldMap.
239
+ */
240
+ getFieldIndex(ptype, field) {
241
+ return this.model.getFieldIndex(ptype, field);
242
+ }
243
+ /**
244
+ * set index of field
245
+ */
246
+ setFieldIndex(ptype, field, index) {
247
+ var _a;
248
+ const assertion = (_a = this.model.model.get('p')) === null || _a === void 0 ? void 0 : _a.get(ptype);
249
+ assertion === null || assertion === void 0 ? void 0 : assertion.fieldIndexMap.set(field, index);
250
+ }
237
251
  }
238
252
  exports.InternalEnforcer = InternalEnforcer;
@@ -6,6 +6,7 @@ export declare class Assertion {
6
6
  tokens: string[];
7
7
  policy: string[][];
8
8
  rm: rbac.RoleManager;
9
+ fieldIndexMap: Map<string, number>;
9
10
  /**
10
11
  * constructor is the constructor for Assertion.
11
12
  */
@@ -48,6 +48,7 @@ class Assertion {
48
48
  this.tokens = [];
49
49
  this.policy = [];
50
50
  this.rm = new rbac.DefaultRoleManager(10);
51
+ this.fieldIndexMap = new Map();
51
52
  }
52
53
  async buildIncrementalRoleLinks(rm, op, rules) {
53
54
  this.rm = rm;
@@ -39,6 +39,10 @@ export declare class Model {
39
39
  getValuesForFieldInPolicy(sec: string, key: string, fieldIndex: number): string[];
40
40
  getValuesForFieldInPolicyAllTypes(sec: string, fieldIndex: number): string[];
41
41
  printPolicy(): void;
42
+ /**
43
+ * return the field index in fieldMap, if no this field in fieldMap, add it.
44
+ */
45
+ getFieldIndex(ptype: string, field: string): number;
42
46
  }
43
47
  /**
44
48
  * newModel creates a model.
@@ -88,6 +88,7 @@ class Model {
88
88
  const ast = new assertion_1.Assertion();
89
89
  ast.key = key;
90
90
  ast.value = value;
91
+ ast.fieldIndexMap = new Map();
91
92
  if (sec === 'r' || sec === 'p') {
92
93
  const tokens = value.split(',').map((n) => n.trim());
93
94
  for (let i = 0; i < tokens.length; i++) {
@@ -417,6 +418,33 @@ class Model {
417
418
  }
418
419
  });
419
420
  }
421
+ /**
422
+ * return the field index in fieldMap, if no this field in fieldMap, add it.
423
+ */
424
+ getFieldIndex(ptype, field) {
425
+ var _a;
426
+ const assertion = (_a = this.model.get('p')) === null || _a === void 0 ? void 0 : _a.get(ptype);
427
+ if (!assertion) {
428
+ return -1;
429
+ }
430
+ let index = assertion.fieldIndexMap.get(field);
431
+ if (index) {
432
+ return index;
433
+ }
434
+ const pattern = ptype + '_' + field;
435
+ index = -1;
436
+ for (let i = 0; i < assertion.tokens.length; i++) {
437
+ if (assertion.tokens[i] === pattern) {
438
+ index = i;
439
+ break;
440
+ }
441
+ }
442
+ if (index === -1) {
443
+ return index;
444
+ }
445
+ assertion.fieldIndexMap.set(field, index);
446
+ return index;
447
+ }
420
448
  }
421
449
  exports.Model = Model;
422
450
  /**
@@ -0,0 +1,13 @@
1
+ export declare const enum EffectExpress {
2
+ ALLOW = "some(where (p_eft == allow))",
3
+ DENY = "!some(where (p_eft == deny))",
4
+ ALLOW_AND_DENY = "some(where (p_eft == allow)) && !some(where (p_eft == deny))",
5
+ PRIORITY = "priority(p_eft) || deny",
6
+ SUBJECT_PRIORITY = "subjectPriority(p_eft) || deny"
7
+ }
8
+ export declare const enum FieldIndex {
9
+ Domain = "dom",
10
+ Subject = "sub",
11
+ Object = "obj",
12
+ Priority = "priority"
13
+ }
@@ -0,0 +1,13 @@
1
+ // Copyright 2022 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.
@@ -27,14 +27,14 @@ export class DefaultEffectorStream {
27
27
  }
28
28
  pushEffect(eft) {
29
29
  switch (this.expr) {
30
- case 'some(where (p_eft == allow))':
30
+ case "some(where (p_eft == allow))" /* ALLOW */:
31
31
  if (eft === Effect.Allow) {
32
32
  this.res = true;
33
33
  this.done = true;
34
34
  this.rec = true;
35
35
  }
36
36
  break;
37
- case '!some(where (p_eft == deny))':
37
+ case "!some(where (p_eft == deny))" /* DENY */:
38
38
  this.res = true;
39
39
  if (eft === Effect.Deny) {
40
40
  this.res = false;
@@ -42,7 +42,7 @@ export class DefaultEffectorStream {
42
42
  this.rec = true;
43
43
  }
44
44
  break;
45
- case 'some(where (p_eft == allow)) && !some(where (p_eft == deny))':
45
+ case "some(where (p_eft == allow)) && !some(where (p_eft == deny))" /* ALLOW_AND_DENY */:
46
46
  if (eft === Effect.Allow) {
47
47
  this.res = true;
48
48
  this.rec = true;
@@ -56,7 +56,7 @@ export class DefaultEffectorStream {
56
56
  this.rec = false;
57
57
  }
58
58
  break;
59
- case 'priority(p_eft) || deny':
59
+ case "priority(p_eft) || deny" /* PRIORITY */:
60
60
  if (eft !== Effect.Indeterminate) {
61
61
  this.res = eft === Effect.Allow;
62
62
  this.done = true;
@@ -169,7 +169,8 @@ export class Enforcer extends ManagementEnforcer {
169
169
  */
170
170
  async deleteRolesForUser(user, domain) {
171
171
  if (domain === undefined) {
172
- return this.removeFilteredGroupingPolicy(0, user);
172
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
173
+ return this.removeFilteredGroupingPolicy(subIndex, user);
173
174
  }
174
175
  else {
175
176
  return this.removeFilteredGroupingPolicy(0, user, '', domain);
@@ -183,8 +184,9 @@ export class Enforcer extends ManagementEnforcer {
183
184
  * @return succeeds or not.
184
185
  */
185
186
  async deleteUser(user) {
186
- const res1 = await this.removeFilteredGroupingPolicy(0, user);
187
- const res2 = await this.removeFilteredPolicy(0, user);
187
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
188
+ const res1 = await this.removeFilteredGroupingPolicy(subIndex, user);
189
+ const res2 = await this.removeFilteredPolicy(subIndex, user);
188
190
  return res1 || res2;
189
191
  }
190
192
  /**
@@ -195,8 +197,9 @@ export class Enforcer extends ManagementEnforcer {
195
197
  * @return succeeds or not.
196
198
  */
197
199
  async deleteRole(role) {
198
- const res1 = await this.removeFilteredGroupingPolicy(1, role);
199
- const res2 = await this.removeFilteredPolicy(0, role);
200
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
201
+ const res1 = await this.removeFilteredGroupingPolicy(subIndex, role);
202
+ const res2 = await this.removeFilteredPolicy(subIndex, role);
200
203
  return res1 || res2;
201
204
  }
202
205
  /**
@@ -241,7 +244,8 @@ export class Enforcer extends ManagementEnforcer {
241
244
  * @return succeeds or not.
242
245
  */
243
246
  async deletePermissionsForUser(user) {
244
- return this.removeFilteredPolicy(0, user);
247
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
248
+ return this.removeFilteredPolicy(subIndex, user);
245
249
  }
246
250
  /**
247
251
  * getPermissionsForUser gets permissions for a user or role.
@@ -250,7 +254,8 @@ export class Enforcer extends ManagementEnforcer {
250
254
  * @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
251
255
  */
252
256
  async getPermissionsForUser(user) {
253
- return this.getFilteredPolicy(0, user);
257
+ const subIndex = this.getFieldIndex('p', "sub" /* Subject */);
258
+ return this.getFilteredPolicy(subIndex, user);
254
259
  }
255
260
  /**
256
261
  * hasPermissionForUser determines whether a user has a permission.
@@ -21,4 +21,12 @@ export declare class InternalEnforcer extends CoreEnforcer {
21
21
  * removeFilteredPolicyInternal removes rules based on field filters from the current policy.
22
22
  */
23
23
  protected removeFilteredPolicyInternal(sec: string, ptype: string, fieldIndex: number, fieldValues: string[], useWatcher: boolean): Promise<boolean>;
24
+ /**
25
+ * get field index in model.fieldMap.
26
+ */
27
+ getFieldIndex(ptype: string, field: string): number;
28
+ /**
29
+ * set index of field
30
+ */
31
+ setFieldIndex(ptype: string, field: string, index: number): void;
24
32
  }
@@ -231,4 +231,18 @@ export class InternalEnforcer extends CoreEnforcer {
231
231
  }
232
232
  return ok;
233
233
  }
234
+ /**
235
+ * get field index in model.fieldMap.
236
+ */
237
+ getFieldIndex(ptype, field) {
238
+ return this.model.getFieldIndex(ptype, field);
239
+ }
240
+ /**
241
+ * set index of field
242
+ */
243
+ setFieldIndex(ptype, field, index) {
244
+ var _a;
245
+ const assertion = (_a = this.model.model.get('p')) === null || _a === void 0 ? void 0 : _a.get(ptype);
246
+ assertion === null || assertion === void 0 ? void 0 : assertion.fieldIndexMap.set(field, index);
247
+ }
234
248
  }
@@ -6,6 +6,7 @@ export declare class Assertion {
6
6
  tokens: string[];
7
7
  policy: string[][];
8
8
  rm: rbac.RoleManager;
9
+ fieldIndexMap: Map<string, number>;
9
10
  /**
10
11
  * constructor is the constructor for Assertion.
11
12
  */
@@ -26,6 +26,7 @@ export class Assertion {
26
26
  this.tokens = [];
27
27
  this.policy = [];
28
28
  this.rm = new rbac.DefaultRoleManager(10);
29
+ this.fieldIndexMap = new Map();
29
30
  }
30
31
  async buildIncrementalRoleLinks(rm, op, rules) {
31
32
  this.rm = rm;
@@ -39,6 +39,10 @@ export declare class Model {
39
39
  getValuesForFieldInPolicy(sec: string, key: string, fieldIndex: number): string[];
40
40
  getValuesForFieldInPolicyAllTypes(sec: string, fieldIndex: number): string[];
41
41
  printPolicy(): void;
42
+ /**
43
+ * return the field index in fieldMap, if no this field in fieldMap, add it.
44
+ */
45
+ getFieldIndex(ptype: string, field: string): number;
42
46
  }
43
47
  /**
44
48
  * newModel creates a model.
@@ -66,6 +66,7 @@ export class Model {
66
66
  const ast = new Assertion();
67
67
  ast.key = key;
68
68
  ast.value = value;
69
+ ast.fieldIndexMap = new Map();
69
70
  if (sec === 'r' || sec === 'p') {
70
71
  const tokens = value.split(',').map((n) => n.trim());
71
72
  for (let i = 0; i < tokens.length; i++) {
@@ -395,6 +396,33 @@ export class Model {
395
396
  }
396
397
  });
397
398
  }
399
+ /**
400
+ * return the field index in fieldMap, if no this field in fieldMap, add it.
401
+ */
402
+ getFieldIndex(ptype, field) {
403
+ var _a;
404
+ const assertion = (_a = this.model.get('p')) === null || _a === void 0 ? void 0 : _a.get(ptype);
405
+ if (!assertion) {
406
+ return -1;
407
+ }
408
+ let index = assertion.fieldIndexMap.get(field);
409
+ if (index) {
410
+ return index;
411
+ }
412
+ const pattern = ptype + '_' + field;
413
+ index = -1;
414
+ for (let i = 0; i < assertion.tokens.length; i++) {
415
+ if (assertion.tokens[i] === pattern) {
416
+ index = i;
417
+ break;
418
+ }
419
+ }
420
+ if (index === -1) {
421
+ return index;
422
+ }
423
+ assertion.fieldIndexMap.set(field, index);
424
+ return index;
425
+ }
398
426
  }
399
427
  /**
400
428
  * newModel creates a model.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "casbin",
3
- "version": "5.18.0",
3
+ "version": "5.19.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",