@nocobase/acl 0.7.0-alpha.4 → 0.7.0-alpha.56

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 (42) hide show
  1. package/lib/acl-available-action.js +14 -7
  2. package/lib/acl-available-strategy.js +108 -59
  3. package/lib/acl-resource.js +56 -40
  4. package/lib/acl-role.js +152 -80
  5. package/lib/acl.js +340 -189
  6. package/lib/allow-manager.js +123 -76
  7. package/lib/index.js +82 -18
  8. package/lib/skip-middleware.js +33 -24
  9. package/package.json +3 -8
  10. package/esm/acl-available-action.d.ts +0 -16
  11. package/esm/acl-available-action.js +0 -7
  12. package/esm/acl-available-action.js.map +0 -1
  13. package/esm/acl-available-strategy.d.ts +0 -29
  14. package/esm/acl-available-strategy.js +0 -60
  15. package/esm/acl-available-strategy.js.map +0 -1
  16. package/esm/acl-resource.d.ts +0 -25
  17. package/esm/acl-resource.js +0 -42
  18. package/esm/acl-resource.js.map +0 -1
  19. package/esm/acl-role.d.ts +0 -38
  20. package/esm/acl-role.js +0 -86
  21. package/esm/acl-role.js.map +0 -1
  22. package/esm/acl.d.ts +0 -64
  23. package/esm/acl.js +0 -202
  24. package/esm/acl.js.map +0 -1
  25. package/esm/allow-manager.d.ts +0 -13
  26. package/esm/allow-manager.js +0 -80
  27. package/esm/allow-manager.js.map +0 -1
  28. package/esm/index.d.ts +0 -6
  29. package/esm/index.js +0 -7
  30. package/esm/index.js.map +0 -1
  31. package/esm/skip-middleware.d.ts +0 -6
  32. package/esm/skip-middleware.js +0 -23
  33. package/esm/skip-middleware.js.map +0 -1
  34. package/lib/acl-available-action.js.map +0 -1
  35. package/lib/acl-available-strategy.js.map +0 -1
  36. package/lib/acl-resource.js.map +0 -1
  37. package/lib/acl-role.js.map +0 -1
  38. package/lib/acl.js.map +0 -1
  39. package/lib/allow-manager.js.map +0 -1
  40. package/lib/index.js.map +0 -1
  41. package/lib/skip-middleware.js.map +0 -1
  42. package/tsconfig.build.json +0 -9
@@ -1,11 +1,18 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.AclAvailableAction = void 0;
7
+
4
8
  class AclAvailableAction {
5
- constructor(name, options) {
6
- this.name = name;
7
- this.options = options;
8
- }
9
+ constructor(name, options) {
10
+ this.name = void 0;
11
+ this.options = void 0;
12
+ this.name = name;
13
+ this.options = options;
14
+ }
15
+
9
16
  }
10
- exports.AclAvailableAction = AclAvailableAction;
11
- //# sourceMappingURL=acl-available-action.js.map
17
+
18
+ exports.AclAvailableAction = AclAvailableAction;
@@ -1,68 +1,117 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ACLAvailableStrategy = exports.predicate = exports.strategyValueMatched = void 0;
7
- const lodash_1 = __importDefault(require("lodash"));
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.predicate = exports.ACLAvailableStrategy = void 0;
7
+ exports.strategyValueMatched = strategyValueMatched;
8
+
9
+ function _lodash() {
10
+ const data = _interopRequireDefault(require("lodash"));
11
+
12
+ _lodash = function _lodash() {
13
+ return data;
14
+ };
15
+
16
+ return data;
17
+ }
18
+
19
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
+
21
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
22
+
23
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
24
+
25
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
26
+
27
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
28
+
29
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
30
+
31
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
32
+
8
33
  function strategyValueMatched(strategy, value) {
9
- if (strategy === '*') {
10
- return true;
11
- }
12
- if (lodash_1.default.isString(strategy) && strategy === value) {
13
- return true;
14
- }
15
- if (lodash_1.default.isArray(strategy) && strategy.includes(value)) {
16
- return true;
17
- }
18
- return false;
34
+ if (strategy === '*') {
35
+ return true;
36
+ }
37
+
38
+ if (_lodash().default.isString(strategy) && strategy === value) {
39
+ return true;
40
+ }
41
+
42
+ if (_lodash().default.isArray(strategy) && strategy.includes(value)) {
43
+ return true;
44
+ }
45
+
46
+ return false;
19
47
  }
20
- exports.strategyValueMatched = strategyValueMatched;
21
- exports.predicate = {
22
- own: {
23
- filter: {
24
- createdById: '{{ ctx.state.currentUser.id }}',
25
- },
26
- },
27
- all: {},
48
+
49
+ const predicate = {
50
+ own: {
51
+ filter: {
52
+ createdById: '{{ ctx.state.currentUser.id }}'
53
+ }
54
+ },
55
+ all: {}
28
56
  };
57
+ exports.predicate = predicate;
58
+
29
59
  class ACLAvailableStrategy {
30
- constructor(acl, options) {
31
- this.acl = acl;
32
- this.options = options;
33
- this.allowConfigure = options.allowConfigure;
34
- let actions = this.options.actions;
35
- if (lodash_1.default.isString(actions) && actions != '*') {
36
- actions = [actions];
37
- }
38
- if (lodash_1.default.isArray(actions)) {
39
- this.actionsAsObject = actions.reduce((carry, action) => {
40
- const [actionName, predicate] = action.split(':');
41
- carry[actionName] = predicate;
42
- return carry;
43
- }, {});
44
- }
60
+ constructor(acl, options) {
61
+ this.acl = void 0;
62
+ this.options = void 0;
63
+ this.actionsAsObject = void 0;
64
+ this.allowConfigure = void 0;
65
+ this.acl = acl;
66
+ this.options = options;
67
+ this.allowConfigure = options.allowConfigure;
68
+ let actions = this.options.actions;
69
+
70
+ if (_lodash().default.isString(actions) && actions != '*') {
71
+ actions = [actions];
45
72
  }
46
- matchAction(actionName) {
47
- var _a;
48
- if (this.options.actions == '*') {
49
- return true;
50
- }
51
- if ((_a = this.actionsAsObject) === null || _a === void 0 ? void 0 : _a.hasOwnProperty(actionName)) {
52
- const predicateName = this.actionsAsObject[actionName];
53
- if (predicateName) {
54
- return exports.predicate[predicateName];
55
- }
56
- return true;
57
- }
58
- return false;
73
+
74
+ if (_lodash().default.isArray(actions)) {
75
+ this.actionsAsObject = actions.reduce((carry, action) => {
76
+ const _action$split = action.split(':'),
77
+ _action$split2 = _slicedToArray(_action$split, 2),
78
+ actionName = _action$split2[0],
79
+ predicate = _action$split2[1];
80
+
81
+ carry[actionName] = predicate;
82
+ return carry;
83
+ }, {});
59
84
  }
60
- allow(resourceName, actionName) {
61
- if (this.acl.isConfigResource(resourceName) && this.allowConfigure) {
62
- return true;
63
- }
64
- return this.matchAction(this.acl.resolveActionAlias(actionName));
85
+ }
86
+
87
+ matchAction(actionName) {
88
+ var _this$actionsAsObject;
89
+
90
+ if (this.options.actions == '*') {
91
+ return true;
92
+ }
93
+
94
+ if ((_this$actionsAsObject = this.actionsAsObject) === null || _this$actionsAsObject === void 0 ? void 0 : _this$actionsAsObject.hasOwnProperty(actionName)) {
95
+ const predicateName = this.actionsAsObject[actionName];
96
+
97
+ if (predicateName) {
98
+ return predicate[predicateName];
99
+ }
100
+
101
+ return true;
102
+ }
103
+
104
+ return false;
105
+ }
106
+
107
+ allow(resourceName, actionName) {
108
+ if (this.acl.isConfigResource(resourceName) && this.allowConfigure) {
109
+ return true;
65
110
  }
111
+
112
+ return this.matchAction(this.acl.resolveActionAlias(actionName));
113
+ }
114
+
66
115
  }
67
- exports.ACLAvailableStrategy = ACLAvailableStrategy;
68
- //# sourceMappingURL=acl-available-strategy.js.map
116
+
117
+ exports.ACLAvailableStrategy = ACLAvailableStrategy;
@@ -1,46 +1,62 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.ACLResource = void 0;
7
+
4
8
  class ACLResource {
5
- constructor(options) {
6
- this.actions = new Map();
7
- this.acl = options.role.acl;
8
- this.role = options.role;
9
- this.name = options.name;
10
- const actionsOption = options.actions || {};
11
- for (const actionName of Object.keys(actionsOption)) {
12
- this.actions.set(actionName, actionsOption[actionName]);
13
- }
9
+ constructor(options) {
10
+ this.actions = new Map();
11
+ this.acl = void 0;
12
+ this.role = void 0;
13
+ this.name = void 0;
14
+ this.acl = options.role.acl;
15
+ this.role = options.role;
16
+ this.name = options.name;
17
+ const actionsOption = options.actions || {};
18
+
19
+ for (var _i = 0, _Object$keys = Object.keys(actionsOption); _i < _Object$keys.length; _i++) {
20
+ const actionName = _Object$keys[_i];
21
+ this.actions.set(actionName, actionsOption[actionName]);
14
22
  }
15
- getActions() {
16
- return Array.from(this.actions.keys()).reduce((carry, key) => {
17
- carry[key] = this.actions.get(key);
18
- return carry;
19
- }, {});
20
- }
21
- getAction(name) {
22
- return this.actions.get(name) || this.actions.get(this.acl.resolveActionAlias(name));
23
- }
24
- setAction(name, params) {
25
- const context = {
26
- role: this.role,
27
- acl: this.role.acl,
28
- params: params || {},
29
- path: `${this.name}:${name}`,
30
- resourceName: this.name,
31
- actionName: name,
32
- };
33
- this.acl.emit('beforeGrantAction', context);
34
- this.actions.set(name, context.params);
35
- }
36
- setActions(actions) {
37
- for (const actionName of Object.keys(actions)) {
38
- this.setAction(actionName, actions[actionName]);
39
- }
40
- }
41
- removeAction(name) {
42
- this.actions.delete(name);
23
+ }
24
+
25
+ getActions() {
26
+ return Array.from(this.actions.keys()).reduce((carry, key) => {
27
+ carry[key] = this.actions.get(key);
28
+ return carry;
29
+ }, {});
30
+ }
31
+
32
+ getAction(name) {
33
+ return this.actions.get(name) || this.actions.get(this.acl.resolveActionAlias(name));
34
+ }
35
+
36
+ setAction(name, params) {
37
+ const context = {
38
+ role: this.role,
39
+ acl: this.role.acl,
40
+ params: params || {},
41
+ path: `${this.name}:${name}`,
42
+ resourceName: this.name,
43
+ actionName: name
44
+ };
45
+ this.acl.emit('beforeGrantAction', context);
46
+ this.actions.set(name, context.params);
47
+ }
48
+
49
+ setActions(actions) {
50
+ for (var _i2 = 0, _Object$keys2 = Object.keys(actions); _i2 < _Object$keys2.length; _i2++) {
51
+ const actionName = _Object$keys2[_i2];
52
+ this.setAction(actionName, actions[actionName]);
43
53
  }
54
+ }
55
+
56
+ removeAction(name) {
57
+ this.actions.delete(name);
58
+ }
59
+
44
60
  }
45
- exports.ACLResource = ACLResource;
46
- //# sourceMappingURL=acl-resource.js.map
61
+
62
+ exports.ACLResource = ACLResource;
package/lib/acl-role.js CHANGED
@@ -1,90 +1,162 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
3
6
  exports.ACLRole = void 0;
4
- const acl_resource_1 = require("./acl-resource");
7
+
8
+ var _aclResource = require("./acl-resource");
9
+
10
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
11
+
12
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
13
+
14
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
15
+
16
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
17
+
18
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
19
+
20
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
21
+
22
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
23
+
5
24
  class ACLRole {
6
- constructor(acl, name) {
7
- this.acl = acl;
8
- this.name = name;
9
- this.resources = new Map();
25
+ constructor(acl, name) {
26
+ this.acl = void 0;
27
+ this.name = void 0;
28
+ this.strategy = void 0;
29
+ this.resources = new Map();
30
+ this.acl = acl;
31
+ this.name = name;
32
+ }
33
+
34
+ getResource(name) {
35
+ return this.resources.get(name);
36
+ }
37
+
38
+ setResource(name, resource) {
39
+ this.resources.set(name, resource);
40
+ }
41
+
42
+ setStrategy(value) {
43
+ this.strategy = value;
44
+ }
45
+
46
+ grantResource(resourceName, options) {
47
+ const resource = new _aclResource.ACLResource({
48
+ role: this,
49
+ name: resourceName
50
+ });
51
+
52
+ for (var _i = 0, _Object$entries = Object.entries(options); _i < _Object$entries.length; _i++) {
53
+ const _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
54
+ actionName = _Object$entries$_i[0],
55
+ actionParams = _Object$entries$_i[1];
56
+
57
+ resource.setAction(actionName, actionParams);
10
58
  }
11
- getResource(name) {
12
- return this.resources.get(name);
59
+
60
+ this.resources.set(resourceName, resource);
61
+ }
62
+
63
+ getResourceActionsParams(resourceName) {
64
+ const resource = this.getResource(resourceName);
65
+ return resource.getActions();
66
+ }
67
+
68
+ revokeResource(resourceName) {
69
+ for (var _i2 = 0, _arr2 = [...this.resources.keys()]; _i2 < _arr2.length; _i2++) {
70
+ const key = _arr2[_i2];
71
+
72
+ if (key === resourceName || key.includes(`${resourceName}.`)) {
73
+ this.resources.delete(key);
74
+ }
13
75
  }
14
- setResource(name, resource) {
15
- this.resources.set(name, resource);
76
+ }
77
+
78
+ grantAction(path, options) {
79
+ let _this$getResourceActi = this.getResourceActionFromPath(path),
80
+ resource = _this$getResourceActi.resource,
81
+ resourceName = _this$getResourceActi.resourceName,
82
+ actionName = _this$getResourceActi.actionName;
83
+
84
+ if (!resource) {
85
+ resource = new _aclResource.ACLResource({
86
+ role: this,
87
+ name: resourceName
88
+ });
89
+ this.resources.set(resourceName, resource);
16
90
  }
17
- setStrategy(value) {
18
- this.strategy = value;
19
- }
20
- grantResource(resourceName, options) {
21
- const resource = new acl_resource_1.ACLResource({
22
- role: this,
23
- name: resourceName,
24
- });
25
- for (const [actionName, actionParams] of Object.entries(options)) {
26
- resource.setAction(actionName, actionParams);
27
- }
28
- this.resources.set(resourceName, resource);
29
- }
30
- getResourceActionsParams(resourceName) {
31
- const resource = this.getResource(resourceName);
32
- return resource.getActions();
33
- }
34
- revokeResource(resourceName) {
35
- for (const key of [...this.resources.keys()]) {
36
- if (key === resourceName || key.includes(`${resourceName}.`)) {
37
- this.resources.delete(key);
38
- }
39
- }
40
- }
41
- grantAction(path, options) {
42
- let { resource, resourceName, actionName } = this.getResourceActionFromPath(path);
43
- if (!resource) {
44
- resource = new acl_resource_1.ACLResource({
45
- role: this,
46
- name: resourceName,
47
- });
48
- this.resources.set(resourceName, resource);
91
+
92
+ resource.setAction(actionName, options);
93
+ }
94
+
95
+ getActionParams(path) {
96
+ const _this$getResourceActi2 = this.getResourceActionFromPath(path),
97
+ action = _this$getResourceActi2.action;
98
+
99
+ return action;
100
+ }
101
+
102
+ revokeAction(path) {
103
+ const _this$getResourceActi3 = this.getResourceActionFromPath(path),
104
+ resource = _this$getResourceActi3.resource,
105
+ actionName = _this$getResourceActi3.actionName;
106
+
107
+ resource.removeAction(actionName);
108
+ }
109
+
110
+ toJSON() {
111
+ const actions = {};
112
+
113
+ var _iterator = _createForOfIteratorHelper(this.resources.keys()),
114
+ _step;
115
+
116
+ try {
117
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
118
+ const resourceName = _step.value;
119
+ const resourceActions = this.getResourceActionsParams(resourceName);
120
+
121
+ for (var _i3 = 0, _Object$keys = Object.keys(resourceActions); _i3 < _Object$keys.length; _i3++) {
122
+ const actionName = _Object$keys[_i3];
123
+ actions[`${resourceName}:${actionName}`] = resourceActions[actionName];
49
124
  }
50
- resource.setAction(actionName, options);
125
+ }
126
+ } catch (err) {
127
+ _iterator.e(err);
128
+ } finally {
129
+ _iterator.f();
51
130
  }
52
- getActionParams(path) {
53
- const { action } = this.getResourceActionFromPath(path);
54
- return action;
55
- }
56
- revokeAction(path) {
57
- const { resource, actionName } = this.getResourceActionFromPath(path);
58
- resource.removeAction(actionName);
59
- }
60
- toJSON() {
61
- const actions = {};
62
- for (const resourceName of this.resources.keys()) {
63
- const resourceActions = this.getResourceActionsParams(resourceName);
64
- for (const actionName of Object.keys(resourceActions)) {
65
- actions[`${resourceName}:${actionName}`] = resourceActions[actionName];
66
- }
67
- }
68
- return {
69
- role: this.name,
70
- strategy: this.strategy,
71
- actions,
72
- };
73
- }
74
- getResourceActionFromPath(path) {
75
- const [resourceName, actionName] = path.split(':');
76
- const resource = this.resources.get(resourceName);
77
- let action = null;
78
- if (resource) {
79
- action = resource.getAction(actionName);
80
- }
81
- return {
82
- resourceName,
83
- actionName,
84
- resource,
85
- action,
86
- };
131
+
132
+ return {
133
+ role: this.name,
134
+ strategy: this.strategy,
135
+ actions
136
+ };
137
+ }
138
+
139
+ getResourceActionFromPath(path) {
140
+ const _path$split = path.split(':'),
141
+ _path$split2 = _slicedToArray(_path$split, 2),
142
+ resourceName = _path$split2[0],
143
+ actionName = _path$split2[1];
144
+
145
+ const resource = this.resources.get(resourceName);
146
+ let action = null;
147
+
148
+ if (resource) {
149
+ action = resource.getAction(actionName);
87
150
  }
151
+
152
+ return {
153
+ resourceName,
154
+ actionName,
155
+ resource,
156
+ action
157
+ };
158
+ }
159
+
88
160
  }
89
- exports.ACLRole = ACLRole;
90
- //# sourceMappingURL=acl-role.js.map
161
+
162
+ exports.ACLRole = ACLRole;