@nocobase/acl 0.9.1-alpha.2 → 0.9.2-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ACLAvailableAction = void 0;
7
-
8
7
  class ACLAvailableAction {
9
8
  constructor(name, options) {
10
9
  this.name = void 0;
@@ -12,7 +11,5 @@ class ACLAvailableAction {
12
11
  this.name = name;
13
12
  this.options = options;
14
13
  }
15
-
16
14
  }
17
-
18
15
  exports.ACLAvailableAction = ACLAvailableAction;
@@ -4,31 +4,20 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.predicate = exports.ACLAvailableStrategy = void 0;
7
-
8
7
  function _lodash() {
9
8
  const data = _interopRequireDefault(require("lodash"));
10
-
11
9
  _lodash = function _lodash() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
15
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
21
-
22
16
  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."); }
23
-
24
17
  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); }
25
-
26
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; }
27
-
28
- 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; }
29
-
19
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
30
20
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
31
-
32
21
  const predicate = {
33
22
  own: {
34
23
  filter: {
@@ -38,7 +27,6 @@ const predicate = {
38
27
  all: {}
39
28
  };
40
29
  exports.predicate = predicate;
41
-
42
30
  class ACLAvailableStrategy {
43
31
  constructor(acl, options) {
44
32
  this.acl = void 0;
@@ -49,48 +37,36 @@ class ACLAvailableStrategy {
49
37
  this.options = options;
50
38
  this.allowConfigure = options.allowConfigure;
51
39
  let actions = this.options.actions;
52
-
53
40
  if (_lodash().default.isString(actions) && actions != '*') {
54
41
  actions = [actions];
55
42
  }
56
-
57
43
  if (_lodash().default.isArray(actions)) {
58
44
  this.actionsAsObject = actions.reduce((carry, action) => {
59
45
  const _action$split = action.split(':'),
60
- _action$split2 = _slicedToArray(_action$split, 2),
61
- actionName = _action$split2[0],
62
- predicate = _action$split2[1];
63
-
46
+ _action$split2 = _slicedToArray(_action$split, 2),
47
+ actionName = _action$split2[0],
48
+ predicate = _action$split2[1];
64
49
  carry[actionName] = predicate;
65
50
  return carry;
66
51
  }, {});
67
52
  }
68
53
  }
69
-
70
54
  matchAction(actionName) {
71
55
  var _this$actionsAsObject;
72
-
73
56
  if (this.options.actions == '*') {
74
57
  return true;
75
58
  }
76
-
77
59
  if ((_this$actionsAsObject = this.actionsAsObject) === null || _this$actionsAsObject === void 0 ? void 0 : _this$actionsAsObject.hasOwnProperty(actionName)) {
78
60
  const predicateName = this.actionsAsObject[actionName];
79
-
80
61
  if (predicateName) {
81
62
  return _lodash().default.cloneDeep(predicate[predicateName]);
82
63
  }
83
-
84
64
  return true;
85
65
  }
86
-
87
66
  return false;
88
67
  }
89
-
90
68
  allow(resourceName, actionName) {
91
69
  return this.matchAction(this.acl.resolveActionAlias(actionName));
92
70
  }
93
-
94
71
  }
95
-
96
72
  exports.ACLAvailableStrategy = ACLAvailableStrategy;
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ACLResource = void 0;
7
-
8
7
  class ACLResource {
9
8
  constructor(options) {
10
9
  this.actions = new Map();
@@ -15,24 +14,20 @@ class ACLResource {
15
14
  this.role = options.role;
16
15
  this.name = options.name;
17
16
  const actionsOption = options.actions || {};
18
-
19
17
  for (var _i = 0, _Object$keys = Object.keys(actionsOption); _i < _Object$keys.length; _i++) {
20
18
  const actionName = _Object$keys[_i];
21
19
  this.actions.set(actionName, actionsOption[actionName]);
22
20
  }
23
21
  }
24
-
25
22
  getActions() {
26
23
  return Array.from(this.actions.keys()).reduce((carry, key) => {
27
24
  carry[key] = this.actions.get(key);
28
25
  return carry;
29
26
  }, {});
30
27
  }
31
-
32
28
  getAction(name) {
33
29
  return this.actions.get(name) || this.actions.get(this.acl.resolveActionAlias(name));
34
30
  }
35
-
36
31
  setAction(name, params) {
37
32
  const context = {
38
33
  role: this.role,
@@ -45,18 +40,14 @@ class ACLResource {
45
40
  this.acl.emit('beforeGrantAction', context);
46
41
  this.actions.set(name, context.params);
47
42
  }
48
-
49
43
  setActions(actions) {
50
44
  for (var _i2 = 0, _Object$keys2 = Object.keys(actions); _i2 < _Object$keys2.length; _i2++) {
51
45
  const actionName = _Object$keys2[_i2];
52
46
  this.setAction(actionName, actions[actionName]);
53
47
  }
54
48
  }
55
-
56
49
  removeAction(name) {
57
50
  this.actions.delete(name);
58
51
  }
59
-
60
52
  }
61
-
62
53
  exports.ACLResource = ACLResource;
package/lib/acl-role.js CHANGED
@@ -4,47 +4,30 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ACLRole = void 0;
7
-
8
7
  var _aclAvailableStrategy = require("./acl-available-strategy");
9
-
10
8
  var _aclResource = require("./acl-resource");
11
-
12
9
  function _lodash() {
13
10
  const data = _interopRequireDefault(require("lodash"));
14
-
15
11
  _lodash = function _lodash() {
16
12
  return data;
17
13
  };
18
-
19
14
  return data;
20
15
  }
21
-
22
16
  function _minimatch() {
23
17
  const data = _interopRequireDefault(require("minimatch"));
24
-
25
18
  _minimatch = function _minimatch() {
26
19
  return data;
27
20
  };
28
-
29
21
  return data;
30
22
  }
31
-
32
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
-
34
24
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
35
-
36
25
  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."); }
37
-
38
- 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; }
39
-
26
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
40
27
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41
-
42
28
  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; } } }; }
43
-
44
29
  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); }
45
-
46
30
  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; }
47
-
48
31
  class ACLRole {
49
32
  constructor(acl, name) {
50
33
  this.acl = void 0;
@@ -55,44 +38,35 @@ class ACLRole {
55
38
  this.acl = acl;
56
39
  this.name = name;
57
40
  }
58
-
59
41
  getResource(name) {
60
42
  return this.resources.get(name);
61
43
  }
62
-
63
44
  setStrategy(value) {
64
45
  this.strategy = value;
65
46
  }
66
-
67
47
  getStrategy() {
68
48
  if (!this.strategy) {
69
49
  return null;
70
50
  }
71
-
72
51
  return _lodash().default.isString(this.strategy) ? this.acl.availableStrategy.get(this.strategy) : new _aclAvailableStrategy.ACLAvailableStrategy(this.acl, this.strategy);
73
52
  }
74
-
75
53
  getResourceActionsParams(resourceName) {
76
54
  const resource = this.getResource(resourceName);
77
55
  return resource.getActions();
78
56
  }
79
-
80
57
  revokeResource(resourceName) {
81
58
  for (var _i = 0, _arr = [...this.resources.keys()]; _i < _arr.length; _i++) {
82
59
  const key = _arr[_i];
83
-
84
60
  if (key === resourceName || key.includes(`${resourceName}.`)) {
85
61
  this.resources.delete(key);
86
62
  }
87
63
  }
88
64
  }
89
-
90
65
  grantAction(path, options) {
91
66
  let _this$getResourceActi = this.getResourceActionFromPath(path),
92
- resource = _this$getResourceActi.resource,
93
- resourceName = _this$getResourceActi.resourceName,
94
- actionName = _this$getResourceActi.actionName;
95
-
67
+ resource = _this$getResourceActi.resource,
68
+ resourceName = _this$getResourceActi.resourceName,
69
+ actionName = _this$getResourceActi.actionName;
96
70
  if (!resource) {
97
71
  resource = new _aclResource.ACLResource({
98
72
  role: this,
@@ -100,48 +74,37 @@ class ACLRole {
100
74
  });
101
75
  this.resources.set(resourceName, resource);
102
76
  }
103
-
104
77
  resource.setAction(actionName, options);
105
78
  }
106
-
107
79
  getActionParams(path) {
108
80
  const _this$getResourceActi2 = this.getResourceActionFromPath(path),
109
- action = _this$getResourceActi2.action;
110
-
81
+ action = _this$getResourceActi2.action;
111
82
  return action;
112
83
  }
113
-
114
84
  revokeAction(path) {
115
85
  const _this$getResourceActi3 = this.getResourceActionFromPath(path),
116
- resource = _this$getResourceActi3.resource,
117
- actionName = _this$getResourceActi3.actionName;
118
-
86
+ resource = _this$getResourceActi3.resource,
87
+ actionName = _this$getResourceActi3.actionName;
119
88
  resource.removeAction(actionName);
120
89
  }
121
-
122
90
  effectiveSnippets() {
123
91
  const allowedSnippets = new Set();
124
92
  const rejectedSnippets = new Set();
125
93
  const availableSnippets = this.acl.snippetManager.snippets;
126
-
127
94
  var _iterator = _createForOfIteratorHelper(this.snippets),
128
- _step;
129
-
95
+ _step;
130
96
  try {
131
97
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
132
98
  let snippetRule = _step.value;
133
99
  const negated = snippetRule.startsWith('!');
134
100
  snippetRule = negated ? snippetRule.slice(1) : snippetRule;
135
-
136
101
  var _iterator2 = _createForOfIteratorHelper(availableSnippets),
137
- _step2;
138
-
102
+ _step2;
139
103
  try {
140
104
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
141
105
  const _step2$value = _slicedToArray(_step2.value, 2),
142
- _ = _step2$value[0],
143
- availableSnippet = _step2$value[1];
144
-
106
+ _ = _step2$value[0],
107
+ availableSnippet = _step2$value[1];
145
108
  if ((0, _minimatch().default)(availableSnippet.name, snippetRule)) {
146
109
  if (negated) {
147
110
  rejectedSnippets.add(availableSnippet.name);
@@ -155,42 +118,34 @@ class ACLRole {
155
118
  } finally {
156
119
  _iterator2.f();
157
120
  }
158
- } // get difference of allowed and rejected snippets
159
-
121
+ }
122
+ // get difference of allowed and rejected snippets
160
123
  } catch (err) {
161
124
  _iterator.e(err);
162
125
  } finally {
163
126
  _iterator.f();
164
127
  }
165
-
166
128
  const effectiveSnippets = new Set([...allowedSnippets].filter(x => !rejectedSnippets.has(x)));
167
129
  return {
168
130
  allowed: [...effectiveSnippets],
169
131
  rejected: [...rejectedSnippets]
170
132
  };
171
133
  }
172
-
173
134
  snippetAllowed(actionPath) {
174
135
  const effectiveSnippets = this.effectiveSnippets();
175
-
176
136
  const getActions = snippets => {
177
137
  return snippets.map(snippetName => this.acl.snippetManager.snippets.get(snippetName).actions).flat();
178
138
  };
179
-
180
139
  const allowedActions = getActions(effectiveSnippets.allowed);
181
140
  const rejectedActions = getActions(effectiveSnippets.rejected);
182
-
183
141
  const actionMatched = (actionPath, actionRule) => {
184
142
  return (0, _minimatch().default)(actionPath, actionRule);
185
143
  };
186
-
187
144
  var _iterator3 = _createForOfIteratorHelper(allowedActions),
188
- _step3;
189
-
145
+ _step3;
190
146
  try {
191
147
  for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
192
148
  const action = _step3.value;
193
-
194
149
  if (actionMatched(actionPath, action)) {
195
150
  return true;
196
151
  }
@@ -200,14 +155,11 @@ class ACLRole {
200
155
  } finally {
201
156
  _iterator3.f();
202
157
  }
203
-
204
158
  var _iterator4 = _createForOfIteratorHelper(rejectedActions),
205
- _step4;
206
-
159
+ _step4;
207
160
  try {
208
161
  for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
209
162
  const action = _step4.value;
210
-
211
163
  if (actionMatched(actionPath, action)) {
212
164
  return false;
213
165
  }
@@ -217,21 +169,16 @@ class ACLRole {
217
169
  } finally {
218
170
  _iterator4.f();
219
171
  }
220
-
221
172
  return null;
222
173
  }
223
-
224
174
  toJSON() {
225
175
  const actions = {};
226
-
227
176
  var _iterator5 = _createForOfIteratorHelper(this.resources.keys()),
228
- _step5;
229
-
177
+ _step5;
230
178
  try {
231
179
  for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
232
180
  const resourceName = _step5.value;
233
181
  const resourceActions = this.getResourceActionsParams(resourceName);
234
-
235
182
  for (var _i2 = 0, _Object$keys = Object.keys(resourceActions); _i2 < _Object$keys.length; _i2++) {
236
183
  const actionName = _Object$keys[_i2];
237
184
  actions[`${resourceName}:${actionName}`] = resourceActions[actionName];
@@ -242,7 +189,6 @@ class ACLRole {
242
189
  } finally {
243
190
  _iterator5.f();
244
191
  }
245
-
246
192
  return {
247
193
  role: this.name,
248
194
  strategy: this.strategy,
@@ -250,20 +196,16 @@ class ACLRole {
250
196
  snippets: Array.from(this.snippets)
251
197
  };
252
198
  }
253
-
254
199
  getResourceActionFromPath(path) {
255
200
  const _path$split = path.split(':'),
256
- _path$split2 = _slicedToArray(_path$split, 2),
257
- resourceName = _path$split2[0],
258
- actionName = _path$split2[1];
259
-
201
+ _path$split2 = _slicedToArray(_path$split, 2),
202
+ resourceName = _path$split2[0],
203
+ actionName = _path$split2[1];
260
204
  const resource = this.resources.get(resourceName);
261
205
  let action = null;
262
-
263
206
  if (resource) {
264
207
  action = resource.getAction(actionName);
265
208
  }
266
-
267
209
  return {
268
210
  resourceName,
269
211
  actionName,
@@ -271,7 +213,5 @@ class ACLRole {
271
213
  action
272
214
  };
273
215
  }
274
-
275
216
  }
276
-
277
217
  exports.ACLRole = ACLRole;
package/lib/acl.js CHANGED
@@ -4,95 +4,62 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ACL = void 0;
7
-
8
7
  function _utils() {
9
8
  const data = require("@nocobase/utils");
10
-
11
9
  _utils = function _utils() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _events() {
19
15
  const data = _interopRequireDefault(require("events"));
20
-
21
16
  _events = function _events() {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _jsonTemplates() {
29
22
  const data = _interopRequireDefault(require("json-templates"));
30
-
31
23
  _jsonTemplates = function _jsonTemplates() {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _koaCompose() {
39
29
  const data = _interopRequireDefault(require("koa-compose"));
40
-
41
30
  _koaCompose = function _koaCompose() {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  function _lodash() {
49
36
  const data = _interopRequireDefault(require("lodash"));
50
-
51
37
  _lodash = function _lodash() {
52
38
  return data;
53
39
  };
54
-
55
40
  return data;
56
41
  }
57
-
58
42
  var _aclAvailableAction = require("./acl-available-action");
59
-
60
43
  var _aclAvailableStrategy = require("./acl-available-strategy");
61
-
62
44
  var _aclRole = require("./acl-role");
63
-
64
45
  var _allowManager = require("./allow-manager");
65
-
66
46
  var _fixedParamsManager = _interopRequireDefault(require("./fixed-params-manager"));
67
-
68
47
  var _snippetManager = _interopRequireDefault(require("./snippet-manager"));
69
-
70
48
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
71
-
72
49
  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; } } }; }
73
-
74
50
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
75
-
76
51
  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."); }
77
-
78
52
  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); }
79
-
80
53
  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; }
81
-
82
- 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; }
83
-
54
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
84
55
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
85
-
86
56
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
87
-
88
57
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
89
-
90
58
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
91
-
92
59
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
93
-
94
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
95
-
60
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
61
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
62
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
96
63
  class ACL extends _events().default {
97
64
  constructor() {
98
65
  super();
@@ -113,14 +80,12 @@ class ACL extends _events().default {
113
80
  });
114
81
  this.beforeGrantAction(ctx => {
115
82
  const actionName = this.resolveActionAlias(ctx.actionName);
116
-
117
83
  if (_lodash().default.isPlainObject(ctx.params)) {
118
84
  if ((actionName === 'create' || actionName === 'update') && ctx.params.fields) {
119
85
  ctx.params = _objectSpread(_objectSpread({}, _lodash().default.omit(ctx.params, 'fields')), {}, {
120
86
  whitelist: ctx.params.fields
121
87
  });
122
88
  }
123
-
124
89
  if (actionName === 'view' && ctx.params.fields) {
125
90
  const appendFields = ['id', 'createdAt', 'updatedAt'];
126
91
  ctx.params = _objectSpread(_objectSpread({}, _lodash().default.omit(ctx.params, 'fields')), {}, {
@@ -135,46 +100,35 @@ class ACL extends _events().default {
135
100
  });
136
101
  this.addCoreMiddleware();
137
102
  }
138
-
139
103
  addCoreMiddleware() {
140
104
  const acl = this;
141
-
142
105
  const filterParams = (ctx, resourceName, params) => {
143
106
  var _params$filter;
144
-
145
107
  if (params === null || params === void 0 ? void 0 : (_params$filter = params.filter) === null || _params$filter === void 0 ? void 0 : _params$filter.createdById) {
146
108
  const collection = ctx.db.getCollection(resourceName);
147
-
148
109
  if (!collection || !collection.getField('createdById')) {
149
110
  return _lodash().default.omit(params, 'filter.createdById');
150
111
  }
151
112
  }
152
-
153
113
  return params;
154
114
  };
155
-
156
115
  this.middlewares.add( /*#__PURE__*/function () {
157
116
  var _ref = _asyncToGenerator(function* (ctx, next) {
158
117
  var _ctx$log, _permission$can, _ctx$log2;
159
-
160
118
  const resourcerAction = ctx.action;
161
119
  const _ctx$action = ctx.action,
162
- resourceName = _ctx$action.resourceName,
163
- actionName = _ctx$action.actionName;
120
+ resourceName = _ctx$action.resourceName,
121
+ actionName = _ctx$action.actionName;
164
122
  const permission = ctx.permission;
165
123
  ((_ctx$log = ctx.log) === null || _ctx$log === void 0 ? void 0 : _ctx$log.info) && ctx.log.info('ctx permission', permission);
166
-
167
124
  if ((!permission.can || typeof permission.can !== 'object') && !permission.skip) {
168
125
  ctx.throw(403, 'No permissions');
169
126
  return;
170
127
  }
171
-
172
128
  const params = ((_permission$can = permission.can) === null || _permission$can === void 0 ? void 0 : _permission$can.params) || acl.fixedParamsManager.getParams(resourceName, actionName);
173
129
  ((_ctx$log2 = ctx.log) === null || _ctx$log2 === void 0 ? void 0 : _ctx$log2.info) && ctx.log.info('acl params', params);
174
-
175
130
  if (params && resourcerAction.mergeParams) {
176
131
  var _ctx$log3;
177
-
178
132
  const filteredParams = filterParams(ctx, resourceName, params);
179
133
  const parsedParams = acl.parseJsonTemplate(filteredParams, ctx);
180
134
  ctx.permission.parsedParams = parsedParams;
@@ -185,20 +139,16 @@ class ACL extends _events().default {
185
139
  if (!x) {
186
140
  return [];
187
141
  }
188
-
189
142
  if (!y) {
190
143
  return x;
191
144
  }
192
-
193
145
  return x.filter(i => y.includes(i.split('.').shift()));
194
146
  }
195
147
  });
196
148
  ctx.permission.mergedParams = _lodash().default.cloneDeep(resourcerAction.params);
197
149
  }
198
-
199
150
  yield next();
200
151
  });
201
-
202
152
  return function (_x, _x2) {
203
153
  return _ref.apply(this, arguments);
204
154
  };
@@ -207,58 +157,43 @@ class ACL extends _events().default {
207
157
  group: 'core'
208
158
  });
209
159
  }
210
-
211
160
  define(options) {
212
161
  const roleName = options.role;
213
162
  const role = new _aclRole.ACLRole(this, roleName);
214
-
215
163
  if (options.strategy) {
216
164
  role.strategy = options.strategy;
217
165
  }
218
-
219
166
  const actions = options.actions || {};
220
-
221
167
  for (var _i = 0, _Object$entries = Object.entries(actions); _i < _Object$entries.length; _i++) {
222
168
  const _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
223
- actionName = _Object$entries$_i[0],
224
- actionParams = _Object$entries$_i[1];
225
-
169
+ actionName = _Object$entries$_i[0],
170
+ actionParams = _Object$entries$_i[1];
226
171
  role.grantAction(actionName, actionParams);
227
172
  }
228
-
229
173
  this.roles.set(roleName, role);
230
174
  return role;
231
175
  }
232
-
233
176
  getRole(name) {
234
177
  return this.roles.get(name);
235
178
  }
236
-
237
179
  removeRole(name) {
238
180
  return this.roles.delete(name);
239
181
  }
240
-
241
182
  registerConfigResources(names) {
242
183
  names.forEach(name => this.registerConfigResource(name));
243
184
  }
244
-
245
185
  registerConfigResource(name) {
246
186
  this.configResources.push(name);
247
187
  }
248
-
249
188
  isConfigResource(name) {
250
189
  return this.configResources.includes(name);
251
190
  }
252
-
253
191
  setAvailableAction(name, options = {}) {
254
192
  this.availableActions.set(name, new _aclAvailableAction.ACLAvailableAction(name, options));
255
-
256
193
  if (options.aliases) {
257
194
  const aliases = _lodash().default.isArray(options.aliases) ? options.aliases : [options.aliases];
258
-
259
195
  var _iterator = _createForOfIteratorHelper(aliases),
260
- _step;
261
-
196
+ _step;
262
197
  try {
263
198
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
264
199
  const alias = _step.value;
@@ -271,60 +206,45 @@ class ACL extends _events().default {
271
206
  }
272
207
  }
273
208
  }
274
-
275
209
  getAvailableAction(name) {
276
210
  const actionName = this.actionAlias.get(name) || name;
277
211
  return this.availableActions.get(actionName);
278
212
  }
279
-
280
213
  getAvailableActions() {
281
214
  return this.availableActions;
282
215
  }
283
-
284
216
  setAvailableStrategy(name, options) {
285
217
  this.availableStrategy.set(name, new _aclAvailableStrategy.ACLAvailableStrategy(this, options));
286
218
  }
287
-
288
219
  beforeGrantAction(listener) {
289
220
  this.addListener('beforeGrantAction', listener);
290
221
  }
291
-
292
222
  can(options) {
293
223
  const role = options.role,
294
- resource = options.resource,
295
- action = options.action;
224
+ resource = options.resource,
225
+ action = options.action;
296
226
  const aclRole = this.roles.get(role);
297
-
298
227
  if (!aclRole) {
299
228
  return null;
300
229
  }
301
-
302
230
  const snippetAllowed = aclRole.snippetAllowed(`${resource}:${action}`);
303
-
304
231
  if (snippetAllowed === false) {
305
232
  return null;
306
233
  }
307
-
308
234
  const fixedParams = this.fixedParamsManager.getParams(resource, action);
309
-
310
235
  const mergeParams = result => {
311
236
  const params = result['params'] || {};
312
237
  const mergedParams = (0, _utils().assign)(params, fixedParams);
313
-
314
238
  if (Object.keys(mergedParams).length) {
315
239
  result['params'] = mergedParams;
316
240
  } else {
317
241
  delete result['params'];
318
242
  }
319
-
320
243
  return result;
321
244
  };
322
-
323
245
  const aclResource = aclRole.getResource(resource);
324
-
325
246
  if (aclResource) {
326
247
  const actionParams = aclResource.getAction(action);
327
-
328
248
  if (actionParams) {
329
249
  // handle single action config
330
250
  return mergeParams({
@@ -337,19 +257,14 @@ class ACL extends _events().default {
337
257
  return null;
338
258
  }
339
259
  }
340
-
341
260
  const roleStrategy = aclRole.getStrategy();
342
-
343
261
  if (!roleStrategy && !snippetAllowed) {
344
262
  return null;
345
263
  }
346
-
347
264
  let roleStrategyParams = roleStrategy === null || roleStrategy === void 0 ? void 0 : roleStrategy.allow(resource, this.resolveActionAlias(action));
348
-
349
265
  if (!roleStrategyParams && snippetAllowed) {
350
266
  roleStrategyParams = {};
351
267
  }
352
-
353
268
  if (roleStrategyParams) {
354
269
  const result = {
355
270
  role,
@@ -357,43 +272,33 @@ class ACL extends _events().default {
357
272
  action,
358
273
  params: {}
359
274
  };
360
-
361
275
  if (_lodash().default.isPlainObject(roleStrategyParams)) {
362
276
  result['params'] = roleStrategyParams;
363
277
  }
364
-
365
278
  return mergeParams(result);
366
279
  }
367
-
368
280
  return null;
369
281
  }
370
-
371
282
  isAvailableAction(actionName) {
372
283
  return this.availableActions.has(this.resolveActionAlias(actionName));
373
284
  }
374
-
375
285
  resolveActionAlias(action) {
376
286
  return this.actionAlias.get(action) ? this.actionAlias.get(action) : action;
377
287
  }
378
-
379
288
  use(fn, options) {
380
289
  this.middlewares.add(fn, _objectSpread({
381
290
  group: 'prep'
382
291
  }, options));
383
292
  }
384
-
385
293
  allow(resourceName, actionNames, condition) {
386
294
  return this.skip(resourceName, actionNames, condition);
387
295
  }
388
-
389
296
  skip(resourceName, actionNames, condition) {
390
297
  if (!Array.isArray(actionNames)) {
391
298
  actionNames = [actionNames];
392
299
  }
393
-
394
300
  var _iterator2 = _createForOfIteratorHelper(actionNames),
395
- _step2;
396
-
301
+ _step2;
397
302
  try {
398
303
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
399
304
  const actionName = _step2.value;
@@ -405,7 +310,6 @@ class ACL extends _events().default {
405
310
  _iterator2.f();
406
311
  }
407
312
  }
408
-
409
313
  parseJsonTemplate(json, ctx) {
410
314
  return (0, _jsonTemplates().default)(json)({
411
315
  ctx: {
@@ -413,22 +317,19 @@ class ACL extends _events().default {
413
317
  }
414
318
  });
415
319
  }
416
-
417
320
  middleware() {
418
321
  const acl = this;
419
322
  return /*#__PURE__*/function () {
420
323
  var _ACLMiddleware = _asyncToGenerator(function* (ctx, next) {
421
324
  const roleName = ctx.state.currentRole || 'anonymous';
422
325
  const _ctx$action2 = ctx.action,
423
- resourceName = _ctx$action2.resourceName,
424
- actionName = _ctx$action2.actionName;
425
-
326
+ resourceName = _ctx$action2.resourceName,
327
+ actionName = _ctx$action2.actionName;
426
328
  ctx.can = options => {
427
329
  return acl.can(_objectSpread({
428
330
  role: roleName
429
331
  }, options));
430
332
  };
431
-
432
333
  ctx.permission = {
433
334
  can: ctx.can({
434
335
  resource: resourceName,
@@ -437,30 +338,24 @@ class ACL extends _events().default {
437
338
  };
438
339
  return (0, _koaCompose().default)(acl.middlewares.nodes)(ctx, next);
439
340
  });
440
-
441
341
  function ACLMiddleware(_x3, _x4) {
442
342
  return _ACLMiddleware.apply(this, arguments);
443
343
  }
444
-
445
344
  return ACLMiddleware;
446
345
  }();
447
346
  }
448
-
449
347
  getActionParams(ctx) {
450
348
  var _this = this;
451
-
452
349
  return _asyncToGenerator(function* () {
453
350
  const roleName = ctx.state.currentRole || 'anonymous';
454
351
  const _ctx$action3 = ctx.action,
455
- resourceName = _ctx$action3.resourceName,
456
- actionName = _ctx$action3.actionName;
457
-
352
+ resourceName = _ctx$action3.resourceName,
353
+ actionName = _ctx$action3.actionName;
458
354
  ctx.can = options => {
459
355
  return _this.can(_objectSpread({
460
356
  role: roleName
461
357
  }, options));
462
358
  };
463
-
464
359
  ctx.permission = {
465
360
  can: ctx.can({
466
361
  resource: resourceName,
@@ -470,15 +365,11 @@ class ACL extends _events().default {
470
365
  yield (0, _koaCompose().default)(_this.middlewares.nodes)(ctx, /*#__PURE__*/_asyncToGenerator(function* () {}));
471
366
  })();
472
367
  }
473
-
474
368
  addFixedParams(resource, action, merger) {
475
369
  this.fixedParamsManager.addParams(resource, action, merger);
476
370
  }
477
-
478
371
  registerSnippet(snippet) {
479
372
  this.snippetManager.register(snippet);
480
373
  }
481
-
482
374
  }
483
-
484
375
  exports.ACL = ACL;
@@ -4,23 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.AllowManager = void 0;
7
-
8
7
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
-
10
8
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
-
12
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
-
9
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
11
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
14
12
  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(_e) { throw _e; }, 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(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
15
-
16
13
  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
14
  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
15
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
21
-
22
16
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
23
-
24
17
  class AllowManager {
25
18
  constructor(acl) {
26
19
  this.acl = void 0;
@@ -36,83 +29,63 @@ class AllowManager {
36
29
  this.registerAllowCondition('allowConfigure', /*#__PURE__*/function () {
37
30
  var _ref = _asyncToGenerator(function* (ctx) {
38
31
  var _role$getStrategy;
39
-
40
32
  const roleName = ctx.state.currentRole;
41
-
42
33
  if (!roleName) {
43
34
  return false;
44
35
  }
45
-
46
36
  const role = acl.getRole(roleName);
47
-
48
37
  if (!role) {
49
38
  return false;
50
39
  }
51
-
52
40
  return (_role$getStrategy = role.getStrategy()) === null || _role$getStrategy === void 0 ? void 0 : _role$getStrategy.allowConfigure;
53
41
  });
54
-
55
42
  return function (_x) {
56
43
  return _ref.apply(this, arguments);
57
44
  };
58
45
  }());
59
46
  }
60
-
61
47
  allow(resourceName, actionName, condition) {
62
48
  const actionMap = this.skipActions.get(resourceName) || new Map();
63
49
  actionMap.set(actionName, condition || true);
64
50
  this.skipActions.set(resourceName, actionMap);
65
51
  }
66
-
67
52
  getAllowedConditions(resourceName, actionName) {
68
53
  const fetchActionSteps = ['*', resourceName];
69
54
  const results = [];
70
-
71
55
  for (var _i = 0, _fetchActionSteps = fetchActionSteps; _i < _fetchActionSteps.length; _i++) {
72
56
  const fetchActionStep = _fetchActionSteps[_i];
73
57
  const resource = this.skipActions.get(fetchActionStep);
74
-
75
58
  if (resource) {
76
59
  for (var _i2 = 0, _arr = ['*', actionName]; _i2 < _arr.length; _i2++) {
77
60
  const fetchActionStep = _arr[_i2];
78
61
  const condition = resource.get(fetchActionStep);
79
-
80
62
  if (condition) {
81
63
  results.push(typeof condition === 'string' ? this.registeredCondition.get(condition) : condition);
82
64
  }
83
65
  }
84
66
  }
85
67
  }
86
-
87
68
  return results;
88
69
  }
89
-
90
70
  registerAllowCondition(name, condition) {
91
71
  this.registeredCondition.set(name, condition);
92
72
  }
93
-
94
73
  isAllowed(resourceName, actionName, ctx) {
95
74
  var _this = this;
96
-
97
75
  return _asyncToGenerator(function* () {
98
76
  const skippedConditions = _this.getAllowedConditions(resourceName, actionName);
99
-
100
77
  var _iterator = _createForOfIteratorHelper(skippedConditions),
101
- _step;
102
-
78
+ _step;
103
79
  try {
104
80
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
105
81
  const skippedCondition = _step.value;
106
-
107
82
  if (skippedCondition) {
108
83
  let skipResult = false;
109
-
110
84
  if (typeof skippedCondition === 'function') {
111
85
  skipResult = yield skippedCondition(ctx);
112
86
  } else if (skippedCondition) {
113
87
  skipResult = true;
114
88
  }
115
-
116
89
  if (skipResult) {
117
90
  return true;
118
91
  }
@@ -123,36 +96,28 @@ class AllowManager {
123
96
  } finally {
124
97
  _iterator.f();
125
98
  }
126
-
127
99
  return false;
128
100
  })();
129
101
  }
130
-
131
102
  aclMiddleware() {
132
103
  var _this2 = this;
133
-
134
104
  return /*#__PURE__*/function () {
135
105
  var _ref2 = _asyncToGenerator(function* (ctx, next) {
136
106
  const _ctx$action = ctx.action,
137
- resourceName = _ctx$action.resourceName,
138
- actionName = _ctx$action.actionName;
107
+ resourceName = _ctx$action.resourceName,
108
+ actionName = _ctx$action.actionName;
139
109
  let skip = yield _this2.acl.allowManager.isAllowed(resourceName, actionName, ctx);
140
-
141
110
  if (skip) {
142
111
  ctx.permission = _objectSpread(_objectSpread({}, ctx.permission || {}), {}, {
143
112
  skip: true
144
113
  });
145
114
  }
146
-
147
115
  yield next();
148
116
  });
149
-
150
117
  return function (_x2, _x3) {
151
118
  return _ref2.apply(this, arguments);
152
119
  };
153
120
  }();
154
121
  }
155
-
156
122
  }
157
-
158
123
  exports.AllowManager = AllowManager;
@@ -4,50 +4,36 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _utils() {
9
8
  const data = require("@nocobase/utils");
10
-
11
9
  _utils = function _utils() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  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(_e) { throw _e; }, 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(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
19
-
20
15
  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); }
21
-
22
16
  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; }
23
-
24
17
  const SPLIT = ':';
25
-
26
18
  class FixedParamsManager {
27
19
  constructor() {
28
20
  this.merger = new Map();
29
21
  }
30
-
31
22
  addParams(resource, action, merger) {
32
23
  const path = this.getActionPath(resource, action);
33
24
  this.merger.set(path, [...this.getParamsMerger(resource, action), merger]);
34
25
  }
35
-
36
26
  getParamsMerger(resource, action) {
37
27
  const path = this.getActionPath(resource, action);
38
28
  return this.merger.get(path) || [];
39
29
  }
40
-
41
30
  getActionPath(resource, action) {
42
31
  return `${resource}${SPLIT}${action}`;
43
32
  }
44
-
45
33
  getParams(resource, action, extraParams = {}) {
46
34
  const results = {};
47
-
48
35
  var _iterator = _createForOfIteratorHelper(this.getParamsMerger(resource, action)),
49
- _step;
50
-
36
+ _step;
51
37
  try {
52
38
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
53
39
  const merger = _step.value;
@@ -58,14 +44,11 @@ class FixedParamsManager {
58
44
  } finally {
59
45
  _iterator.f();
60
46
  }
61
-
62
47
  if (extraParams) {
63
48
  FixedParamsManager.mergeParams(results, extraParams);
64
49
  }
65
-
66
50
  return results;
67
51
  }
68
-
69
52
  static mergeParams(a, b) {
70
53
  (0, _utils().assign)(a, b, {
71
54
  filter: 'andMerge',
@@ -77,7 +60,5 @@ class FixedParamsManager {
77
60
  sort: 'overwrite'
78
61
  });
79
62
  }
80
-
81
63
  }
82
-
83
64
  exports.default = FixedParamsManager;
package/lib/index.js CHANGED
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
-
7
6
  var _acl = require("./acl");
8
-
9
7
  Object.keys(_acl).forEach(function (key) {
10
8
  if (key === "default" || key === "__esModule") return;
11
9
  if (key in exports && exports[key] === _acl[key]) return;
@@ -16,9 +14,7 @@ Object.keys(_acl).forEach(function (key) {
16
14
  }
17
15
  });
18
16
  });
19
-
20
17
  var _aclAvailableAction = require("./acl-available-action");
21
-
22
18
  Object.keys(_aclAvailableAction).forEach(function (key) {
23
19
  if (key === "default" || key === "__esModule") return;
24
20
  if (key in exports && exports[key] === _aclAvailableAction[key]) return;
@@ -29,9 +25,7 @@ Object.keys(_aclAvailableAction).forEach(function (key) {
29
25
  }
30
26
  });
31
27
  });
32
-
33
28
  var _aclAvailableStrategy = require("./acl-available-strategy");
34
-
35
29
  Object.keys(_aclAvailableStrategy).forEach(function (key) {
36
30
  if (key === "default" || key === "__esModule") return;
37
31
  if (key in exports && exports[key] === _aclAvailableStrategy[key]) return;
@@ -42,9 +36,7 @@ Object.keys(_aclAvailableStrategy).forEach(function (key) {
42
36
  }
43
37
  });
44
38
  });
45
-
46
39
  var _aclResource = require("./acl-resource");
47
-
48
40
  Object.keys(_aclResource).forEach(function (key) {
49
41
  if (key === "default" || key === "__esModule") return;
50
42
  if (key in exports && exports[key] === _aclResource[key]) return;
@@ -55,9 +47,7 @@ Object.keys(_aclResource).forEach(function (key) {
55
47
  }
56
48
  });
57
49
  });
58
-
59
50
  var _aclRole = require("./acl-role");
60
-
61
51
  Object.keys(_aclRole).forEach(function (key) {
62
52
  if (key === "default" || key === "__esModule") return;
63
53
  if (key in exports && exports[key] === _aclRole[key]) return;
@@ -68,9 +58,7 @@ Object.keys(_aclRole).forEach(function (key) {
68
58
  }
69
59
  });
70
60
  });
71
-
72
61
  var _noPermissionError = require("./no-permission-error");
73
-
74
62
  Object.keys(_noPermissionError).forEach(function (key) {
75
63
  if (key === "default" || key === "__esModule") return;
76
64
  if (key in exports && exports[key] === _noPermissionError[key]) return;
@@ -81,9 +69,7 @@ Object.keys(_noPermissionError).forEach(function (key) {
81
69
  }
82
70
  });
83
71
  });
84
-
85
72
  var _skipMiddleware = require("./skip-middleware");
86
-
87
73
  Object.keys(_skipMiddleware).forEach(function (key) {
88
74
  if (key === "default" || key === "__esModule") return;
89
75
  if (key in exports && exports[key] === _skipMiddleware[key]) return;
@@ -4,12 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.NoPermissionError = void 0;
7
-
8
7
  class NoPermissionError extends Error {
9
8
  constructor(...args) {
10
9
  super(...args);
11
10
  }
12
-
13
11
  }
14
-
15
12
  exports.NoPermissionError = NoPermissionError;
@@ -4,33 +4,25 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.skip = void 0;
7
-
8
7
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
-
10
8
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
11
-
12
9
  const skip = options => {
13
10
  return /*#__PURE__*/function () {
14
11
  var _ACLSkipMiddleware = _asyncToGenerator(function* (ctx, next) {
15
12
  const _ctx$action = ctx.action,
16
- resourceName = _ctx$action.resourceName,
17
- actionName = _ctx$action.actionName;
18
-
13
+ resourceName = _ctx$action.resourceName,
14
+ actionName = _ctx$action.actionName;
19
15
  if (resourceName === options.resourceName && actionName === options.actionName) {
20
16
  ctx.permission = {
21
17
  skip: true
22
18
  };
23
19
  }
24
-
25
20
  yield next();
26
21
  });
27
-
28
22
  function ACLSkipMiddleware(_x, _x2) {
29
23
  return _ACLSkipMiddleware.apply(this, arguments);
30
24
  }
31
-
32
25
  return ACLSkipMiddleware;
33
26
  }();
34
27
  };
35
-
36
28
  exports.skip = skip;
@@ -4,19 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _minimatch() {
9
8
  const data = _interopRequireDefault(require("minimatch"));
10
-
11
9
  _minimatch = function _minimatch() {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
15
  class Snippet {
21
16
  constructor(name, actions) {
22
17
  this.name = void 0;
@@ -24,37 +19,27 @@ class Snippet {
24
19
  this.name = name;
25
20
  this.actions = actions;
26
21
  }
27
-
28
22
  }
29
-
30
23
  class SnippetManager {
31
24
  constructor() {
32
25
  this.snippets = new Map();
33
26
  }
34
-
35
27
  register(snippet) {
36
28
  this.snippets.set(snippet.name, snippet);
37
29
  }
38
-
39
30
  allow(actionPath, snippetName) {
40
31
  const negated = snippetName.startsWith('!');
41
32
  snippetName = negated ? snippetName.slice(1) : snippetName;
42
33
  const snippet = this.snippets.get(snippetName);
43
-
44
34
  if (!snippet) {
45
35
  throw new Error(`Snippet ${snippetName} not found`);
46
36
  }
47
-
48
37
  const matched = snippet.actions.some(action => (0, _minimatch().default)(actionPath, action));
49
-
50
38
  if (matched) {
51
39
  return negated ? false : true;
52
40
  }
53
-
54
41
  return null;
55
42
  }
56
-
57
43
  }
58
-
59
44
  var _default = SnippetManager;
60
45
  exports.default = _default;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/acl",
3
- "version": "0.9.1-alpha.2",
3
+ "version": "0.9.2-alpha.2",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/resourcer": "0.9.1-alpha.2",
10
- "@nocobase/utils": "0.9.1-alpha.2",
9
+ "@nocobase/resourcer": "0.9.2-alpha.2",
10
+ "@nocobase/utils": "0.9.2-alpha.2",
11
11
  "json-templates": "^4.2.0",
12
12
  "minimatch": "^5.1.1"
13
13
  },
@@ -16,5 +16,5 @@
16
16
  "url": "git+https://github.com/nocobase/nocobase.git",
17
17
  "directory": "packages/acl"
18
18
  },
19
- "gitHead": "d588a68eca4feed4642a4cb317301011266fe5c9"
19
+ "gitHead": "1a0de6908b2b1854bdf434fcafa8909cb65bb8a3"
20
20
  }