@nocobase/acl 0.21.0-alpha.1 → 0.21.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.
@@ -3,6 +3,9 @@ export interface AvailableStrategyOptions {
3
3
  displayName?: string;
4
4
  actions?: false | string | string[];
5
5
  allowConfigure?: boolean;
6
+ /**
7
+ * @internal
8
+ */
6
9
  resource?: '*';
7
10
  }
8
11
  export declare const predicate: {
package/lib/acl-role.d.ts CHANGED
@@ -12,6 +12,9 @@ export interface RoleActionParams {
12
12
  export interface ResourceActionsOptions {
13
13
  [actionName: string]: RoleActionParams;
14
14
  }
15
+ /**
16
+ * @internal
17
+ */
15
18
  export declare class ACLRole {
16
19
  acl: ACL;
17
20
  name: string;
package/lib/acl.d.ts CHANGED
@@ -15,9 +15,15 @@ interface CanResult {
15
15
  }
16
16
  export interface DefineOptions {
17
17
  role: string;
18
+ /**
19
+ * @internal
20
+ */
18
21
  allowConfigure?: boolean;
19
22
  strategy?: string | AvailableStrategyOptions;
20
23
  actions?: ResourceActionsOptions;
24
+ /**
25
+ * @internal
26
+ */
21
27
  routes?: any;
22
28
  snippets?: string[];
23
29
  }
@@ -34,6 +40,7 @@ interface CanArgs {
34
40
  role: string;
35
41
  resource: string;
36
42
  action: string;
43
+ rawResourceName?: string;
37
44
  ctx?: any;
38
45
  }
39
46
  export declare class ACL extends EventEmitter {
package/lib/acl.js CHANGED
@@ -154,13 +154,14 @@ const _ACL = class _ACL extends import_events.default {
154
154
  this.addListener("beforeGrantAction", listener);
155
155
  }
156
156
  can(options) {
157
- const { role, resource, action } = options;
157
+ const { role, resource, action, rawResourceName } = options;
158
158
  const aclRole = this.roles.get(role);
159
159
  if (!aclRole) {
160
160
  return null;
161
161
  }
162
- const snippetAllowed = aclRole.snippetAllowed(`${resource}:${action}`);
163
- const fixedParams = this.fixedParamsManager.getParams(resource, action);
162
+ const actionPath = `${rawResourceName ? rawResourceName : resource}:${action}`;
163
+ const snippetAllowed = aclRole.snippetAllowed(actionPath);
164
+ const fixedParams = this.fixedParamsManager.getParams(rawResourceName ? rawResourceName : resource, action);
164
165
  const mergeParams = /* @__PURE__ */ __name((result) => {
165
166
  const params = result["params"] || {};
166
167
  const mergedParams = (0, import_utils.assign)(params, fixedParams);
@@ -257,15 +258,27 @@ const _ACL = class _ACL extends import_events.default {
257
258
  const acl = this;
258
259
  return /* @__PURE__ */ __name(async function ACLMiddleware(ctx, next) {
259
260
  const roleName = ctx.state.currentRole || "anonymous";
260
- const { resourceName, actionName } = ctx.action;
261
+ const { resourceName: rawResourceName, actionName } = ctx.action;
262
+ let resourceName = rawResourceName;
263
+ if (rawResourceName.includes(".")) {
264
+ resourceName = rawResourceName.split(".").pop();
265
+ }
266
+ if (ctx.getCurrentRepository) {
267
+ const currentRepository = ctx.getCurrentRepository();
268
+ if (currentRepository && currentRepository.targetCollection) {
269
+ resourceName = ctx.getCurrentRepository().targetCollection.name;
270
+ }
271
+ }
261
272
  ctx.can = (options) => {
262
273
  const canResult = acl.can({ role: roleName, ...options });
263
274
  return canResult;
264
275
  };
265
276
  ctx.permission = {
266
- can: ctx.can({ resource: resourceName, action: actionName })
277
+ can: ctx.can({ resource: resourceName, action: actionName, rawResourceName }),
278
+ resourceName,
279
+ actionName
267
280
  };
268
- return (0, import_koa_compose.default)(acl.middlewares.nodes)(ctx, next);
281
+ return await (0, import_koa_compose.default)(acl.middlewares.nodes)(ctx, next);
269
282
  }, "ACLMiddleware");
270
283
  }
271
284
  /**
@@ -273,7 +286,17 @@ const _ACL = class _ACL extends import_events.default {
273
286
  */
274
287
  async getActionParams(ctx) {
275
288
  const roleName = ctx.state.currentRole || "anonymous";
276
- const { resourceName, actionName } = ctx.action;
289
+ const { resourceName: rawResourceName, actionName } = ctx.action;
290
+ let resourceName = rawResourceName;
291
+ if (rawResourceName.includes(".")) {
292
+ resourceName = rawResourceName.split(".").pop();
293
+ }
294
+ if (ctx.getCurrentRepository) {
295
+ const currentRepository = ctx.getCurrentRepository();
296
+ if (currentRepository && currentRepository.targetCollection) {
297
+ resourceName = ctx.getCurrentRepository().targetCollection.name;
298
+ }
299
+ }
277
300
  ctx.can = (options) => {
278
301
  const can = this.can({ role: roleName, ...options });
279
302
  if (!can) {
@@ -282,7 +305,9 @@ const _ACL = class _ACL extends import_events.default {
282
305
  return import_lodash.default.cloneDeep(can);
283
306
  };
284
307
  ctx.permission = {
285
- can: ctx.can({ resource: resourceName, action: actionName })
308
+ can: ctx.can({ resource: resourceName, action: actionName, rawResourceName }),
309
+ resourceName,
310
+ actionName
286
311
  };
287
312
  await (0, import_koa_compose.default)(this.middlewares.nodes)(ctx, async () => {
288
313
  });
@@ -312,7 +337,7 @@ const _ACL = class _ACL extends import_events.default {
312
337
  async (ctx, next) => {
313
338
  var _a, _b, _c, _d;
314
339
  const resourcerAction = ctx.action;
315
- const { resourceName, actionName } = ctx.action;
340
+ const { resourceName, actionName } = ctx.permission;
316
341
  const permission = ctx.permission;
317
342
  ((_a = ctx.log) == null ? void 0 : _a.info) && ctx.log.info("ctx permission", permission);
318
343
  if ((!permission.can || typeof permission.can !== "object") && !permission.skip) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/acl",
3
- "version": "0.21.0-alpha.1",
3
+ "version": "0.21.0-alpha.10",
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.21.0-alpha.1",
10
- "@nocobase/utils": "0.21.0-alpha.1",
9
+ "@nocobase/resourcer": "0.21.0-alpha.10",
10
+ "@nocobase/utils": "0.21.0-alpha.10",
11
11
  "minimatch": "^5.1.1"
12
12
  },
13
13
  "repository": {
@@ -15,5 +15,5 @@
15
15
  "url": "git+https://github.com/nocobase/nocobase.git",
16
16
  "directory": "packages/acl"
17
17
  },
18
- "gitHead": "afd2f3d1341b85ea9daa7b2667dd4ace1fafb7ff"
18
+ "gitHead": "98adf5ec996a4f359c6ca1c4a6ac837c43b6e268"
19
19
  }