@nocobase/acl 0.7.6-alpha.2 → 0.8.0-alpha.10

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.
@@ -9,7 +9,7 @@ export interface AvailableActionOptions {
9
9
  onNewRecord?: boolean;
10
10
  allowConfigureFields?: boolean;
11
11
  }
12
- export declare class AclAvailableAction {
12
+ export declare class ACLAvailableAction {
13
13
  name: string;
14
14
  options: AvailableActionOptions;
15
15
  constructor(name: string, options: AvailableActionOptions);
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.AclAvailableAction = void 0;
6
+ exports.ACLAvailableAction = void 0;
7
7
 
8
- class AclAvailableAction {
8
+ class ACLAvailableAction {
9
9
  constructor(name, options) {
10
10
  this.name = void 0;
11
11
  this.options = void 0;
@@ -15,4 +15,4 @@ class AclAvailableAction {
15
15
 
16
16
  }
17
17
 
18
- exports.AclAvailableAction = AclAvailableAction;
18
+ exports.ACLAvailableAction = ACLAvailableAction;
@@ -1,12 +1,10 @@
1
1
  import { ACL } from './acl';
2
- declare type StrategyValue = false | '*' | string | string[];
3
2
  export interface AvailableStrategyOptions {
4
3
  displayName?: string;
5
4
  actions?: false | string | string[];
6
5
  allowConfigure?: boolean;
7
6
  resource?: '*';
8
7
  }
9
- export declare function strategyValueMatched(strategy: StrategyValue, value: string): boolean;
10
8
  export declare const predicate: {
11
9
  own: {
12
10
  filter: {
@@ -26,4 +24,3 @@ export declare class ACLAvailableStrategy {
26
24
  matchAction(actionName: string): any;
27
25
  allow(resourceName: string, actionName: string): any;
28
26
  }
29
- export {};
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.predicate = exports.ACLAvailableStrategy = void 0;
7
- exports.strategyValueMatched = strategyValueMatched;
8
7
 
9
8
  function _lodash() {
10
9
  const data = _interopRequireDefault(require("lodash"));
@@ -30,22 +29,6 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
30
29
 
31
30
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
32
31
 
33
- function strategyValueMatched(strategy, value) {
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;
47
- }
48
-
49
32
  const predicate = {
50
33
  own: {
51
34
  filter: {
@@ -17,9 +17,7 @@ export declare class ACLResource {
17
17
  getActions(): {};
18
18
  getAction(name: string): RoleActionParams;
19
19
  setAction(name: string, params: RoleActionParams): void;
20
- setActions(actions: {
21
- [key: string]: RoleActionParams;
22
- }): void;
20
+ setActions(actions: ResourceActions): void;
23
21
  removeAction(name: string): void;
24
22
  }
25
23
  export {};
package/lib/acl-role.d.ts CHANGED
@@ -9,7 +9,7 @@ export interface RoleActionParams {
9
9
  blacklist?: string[];
10
10
  [key: string]: any;
11
11
  }
12
- interface ResourceActionsOptions {
12
+ export interface ResourceActionsOptions {
13
13
  [actionName: string]: RoleActionParams;
14
14
  }
15
15
  export declare class ACLRole {
@@ -19,11 +19,9 @@ export declare class ACLRole {
19
19
  resources: Map<string, ACLResource>;
20
20
  constructor(acl: ACL, name: string);
21
21
  getResource(name: string): ACLResource | undefined;
22
- setResource(name: string, resource: ACLResource): void;
23
22
  setStrategy(value: string | AvailableStrategyOptions): void;
24
- grantResource(resourceName: string, options: ResourceActionsOptions): void;
25
23
  getResourceActionsParams(resourceName: string): {};
26
- revokeResource(resourceName: any): void;
24
+ revokeResource(resourceName: string): void;
27
25
  grantAction(path: string, options?: RoleActionParams): void;
28
26
  getActionParams(path: string): RoleActionParams;
29
27
  revokeAction(path: string): void;
@@ -35,4 +33,3 @@ export declare class ACLRole {
35
33
  action: any;
36
34
  };
37
35
  }
38
- export {};
package/lib/acl-role.js CHANGED
@@ -7,20 +7,20 @@ exports.ACLRole = void 0;
7
7
 
8
8
  var _aclResource = require("./acl-resource");
9
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
10
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
13
11
 
14
12
  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
13
 
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
14
  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
15
 
22
16
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
23
17
 
18
+ 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; } } }; }
19
+
20
+ 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
+ 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
24
  class ACLRole {
25
25
  constructor(acl, name) {
26
26
  this.acl = void 0;
@@ -35,39 +35,18 @@ class ACLRole {
35
35
  return this.resources.get(name);
36
36
  }
37
37
 
38
- setResource(name, resource) {
39
- this.resources.set(name, resource);
40
- }
41
-
42
38
  setStrategy(value) {
43
39
  this.strategy = value;
44
40
  }
45
41
 
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);
58
- }
59
-
60
- this.resources.set(resourceName, resource);
61
- }
62
-
63
42
  getResourceActionsParams(resourceName) {
64
43
  const resource = this.getResource(resourceName);
65
44
  return resource.getActions();
66
45
  }
67
46
 
68
47
  revokeResource(resourceName) {
69
- for (var _i2 = 0, _arr2 = [...this.resources.keys()]; _i2 < _arr2.length; _i2++) {
70
- const key = _arr2[_i2];
48
+ for (var _i = 0, _arr = [...this.resources.keys()]; _i < _arr.length; _i++) {
49
+ const key = _arr[_i];
71
50
 
72
51
  if (key === resourceName || key.includes(`${resourceName}.`)) {
73
52
  this.resources.delete(key);
@@ -118,8 +97,8 @@ class ACLRole {
118
97
  const resourceName = _step.value;
119
98
  const resourceActions = this.getResourceActionsParams(resourceName);
120
99
 
121
- for (var _i3 = 0, _Object$keys = Object.keys(resourceActions); _i3 < _Object$keys.length; _i3++) {
122
- const actionName = _Object$keys[_i3];
100
+ for (var _i2 = 0, _Object$keys = Object.keys(resourceActions); _i2 < _Object$keys.length; _i2++) {
101
+ const actionName = _Object$keys[_i2];
123
102
  actions[`${resourceName}:${actionName}`] = resourceActions[actionName];
124
103
  }
125
104
  }
package/lib/acl.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  /// <reference types="node" />
2
+ import { Toposort, ToposortOptions } from '@nocobase/utils';
2
3
  import EventEmitter from 'events';
3
- import { AclAvailableAction, AvailableActionOptions } from './acl-available-action';
4
+ import { ACLAvailableAction, AvailableActionOptions } from './acl-available-action';
4
5
  import { ACLAvailableStrategy, AvailableStrategyOptions } from './acl-available-strategy';
5
- import { ACLRole, RoleActionParams } from './acl-role';
6
- import { AllowManager } from './allow-manager';
6
+ import { ACLRole, ResourceActionsOptions, RoleActionParams } from './acl-role';
7
+ import { AllowManager, ConditionFunc } from './allow-manager';
7
8
  interface CanResult {
8
9
  role: string;
9
10
  resource: string;
@@ -13,10 +14,8 @@ interface CanResult {
13
14
  export interface DefineOptions {
14
15
  role: string;
15
16
  allowConfigure?: boolean;
16
- strategy?: string | Omit<AvailableStrategyOptions, 'acl'>;
17
- actions?: {
18
- [key: string]: RoleActionParams;
19
- };
17
+ strategy?: string | AvailableStrategyOptions;
18
+ actions?: ResourceActionsOptions;
20
19
  routes?: any;
21
20
  }
22
21
  export interface ListenerContext {
@@ -34,9 +33,9 @@ interface CanArgs {
34
33
  action: string;
35
34
  }
36
35
  export declare class ACL extends EventEmitter {
37
- protected availableActions: Map<string, AclAvailableAction>;
36
+ protected availableActions: Map<string, ACLAvailableAction>;
38
37
  protected availableStrategy: Map<string, ACLAvailableStrategy>;
39
- protected middlewares: any[];
38
+ protected middlewares: Toposort<any>;
40
39
  allowManager: AllowManager;
41
40
  roles: Map<string, ACLRole>;
42
41
  actionAlias: Map<string, string>;
@@ -49,15 +48,15 @@ export declare class ACL extends EventEmitter {
49
48
  registerConfigResource(name: string): void;
50
49
  isConfigResource(name: string): boolean;
51
50
  setAvailableAction(name: string, options?: AvailableActionOptions): void;
52
- getAvailableAction(name: string): AclAvailableAction;
53
- getAvailableActions(): Map<string, AclAvailableAction>;
54
- setAvailableStrategy(name: string, options: Omit<AvailableStrategyOptions, 'acl'>): void;
51
+ getAvailableAction(name: string): ACLAvailableAction;
52
+ getAvailableActions(): Map<string, ACLAvailableAction>;
53
+ setAvailableStrategy(name: string, options: AvailableStrategyOptions): void;
55
54
  beforeGrantAction(listener?: Listener): void;
56
- can({ role, resource, action }: CanArgs): CanResult | null;
55
+ can(options: CanArgs): CanResult | null;
57
56
  protected isAvailableAction(actionName: string): boolean;
58
57
  resolveActionAlias(action: string): string;
59
- use(fn: any): void;
60
- allow(resourceName: string, actionNames: string[] | string, condition?: any): void;
58
+ use(fn: any, options?: ToposortOptions): void;
59
+ allow(resourceName: string, actionNames: string[] | string, condition?: string | ConditionFunc): void;
61
60
  parseJsonTemplate(json: any, ctx: any): any;
62
61
  middleware(): (ctx: any, next: any) => Promise<void>;
63
62
  }
package/lib/acl.js CHANGED
@@ -5,6 +5,16 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.ACL = void 0;
7
7
 
8
+ function _utils() {
9
+ const data = require("@nocobase/utils");
10
+
11
+ _utils = function _utils() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
8
18
  function _events() {
9
19
  const data = _interopRequireDefault(require("events"));
10
20
 
@@ -84,11 +94,12 @@ class ACL extends _events().default {
84
94
  super();
85
95
  this.availableActions = new Map();
86
96
  this.availableStrategy = new Map();
87
- this.middlewares = [];
97
+ this.middlewares = void 0;
88
98
  this.allowManager = new _allowManager.AllowManager(this);
89
99
  this.roles = new Map();
90
100
  this.actionAlias = new Map();
91
101
  this.configResources = [];
102
+ this.middlewares = new (_utils().Toposort)();
92
103
  this.beforeGrantAction(ctx => {
93
104
  if (_lodash().default.isPlainObject(ctx.params) && ctx.params.own) {
94
105
  ctx.params = _lodash().default.merge(ctx.params, _aclAvailableStrategy.predicate.own);
@@ -112,7 +123,7 @@ class ACL extends _events().default {
112
123
  }
113
124
  }
114
125
  });
115
- this.middlewares.push(this.allowManager.aclMiddleware());
126
+ this.middlewares.add(this.allowManager.aclMiddleware());
116
127
  }
117
128
 
118
129
  define(options) {
@@ -158,7 +169,7 @@ class ACL extends _events().default {
158
169
  }
159
170
 
160
171
  setAvailableAction(name, options = {}) {
161
- this.availableActions.set(name, new _aclAvailableAction.AclAvailableAction(name, options));
172
+ this.availableActions.set(name, new _aclAvailableAction.ACLAvailableAction(name, options));
162
173
 
163
174
  if (options.aliases) {
164
175
  const aliases = _lodash().default.isArray(options.aliases) ? options.aliases : [options.aliases];
@@ -196,11 +207,10 @@ class ACL extends _events().default {
196
207
  this.addListener('beforeGrantAction', listener);
197
208
  }
198
209
 
199
- can({
200
- role,
201
- resource,
202
- action
203
- }) {
210
+ can(options) {
211
+ const role = options.role,
212
+ resource = options.resource,
213
+ action = options.action;
204
214
  const aclRole = this.roles.get(role);
205
215
 
206
216
  if (!aclRole) {
@@ -262,8 +272,8 @@ class ACL extends _events().default {
262
272
  return this.actionAlias.get(action) ? this.actionAlias.get(action) : action;
263
273
  }
264
274
 
265
- use(fn) {
266
- this.middlewares.push(fn);
275
+ use(fn, options) {
276
+ this.middlewares.add(fn, options);
267
277
  }
268
278
 
269
279
  allow(resourceName, actionNames, condition) {
@@ -331,7 +341,7 @@ class ACL extends _events().default {
331
341
  action: actionName
332
342
  })
333
343
  };
334
- return (0, _koaCompose().default)(acl.middlewares)(ctx, /*#__PURE__*/_asyncToGenerator(function* () {
344
+ return (0, _koaCompose().default)(acl.middlewares.nodes)(ctx, /*#__PURE__*/_asyncToGenerator(function* () {
335
345
  const permission = ctx.permission;
336
346
 
337
347
  if (permission.skip) {
@@ -1,5 +1,5 @@
1
1
  import { ACL } from './acl';
2
- declare type ConditionFunc = (ctx: any) => Promise<boolean>;
2
+ export declare type ConditionFunc = (ctx: any) => Promise<boolean> | boolean;
3
3
  export declare class AllowManager {
4
4
  acl: ACL;
5
5
  protected skipActions: Map<string, Map<string, string | true | ConditionFunc>>;
@@ -10,4 +10,3 @@ export declare class AllowManager {
10
10
  registerAllowCondition(name: string, condition: ConditionFunc): void;
11
11
  aclMiddleware(): (ctx: any, next: any) => Promise<void>;
12
12
  }
13
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/acl",
3
- "version": "0.7.6-alpha.2",
3
+ "version": "0.8.0-alpha.10",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "licenses": [
@@ -12,7 +12,7 @@
12
12
  "main": "./lib/index.js",
13
13
  "types": "./lib/index.d.ts",
14
14
  "dependencies": {
15
- "@nocobase/resourcer": "0.7.6-alpha.2",
15
+ "@nocobase/resourcer": "0.8.0-alpha.10",
16
16
  "json-templates": "^4.2.0"
17
17
  },
18
18
  "repository": {
@@ -20,5 +20,5 @@
20
20
  "url": "git+https://github.com/nocobase/nocobase.git",
21
21
  "directory": "packages/acl"
22
22
  },
23
- "gitHead": "2cfccff9d3ff14c521bf317fa7ee4efa18444c92"
23
+ "gitHead": "a9723cdeadc764eba634b5815685177679025c83"
24
24
  }