@nocobase/acl 2.1.0-alpha.6 → 2.1.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/acl.d.ts +22 -8
- package/lib/acl.js +99 -72
- package/package.json +7 -4
package/lib/acl.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { ACLRole, ResourceActionsOptions, RoleActionParams } from './acl-role';
|
|
|
15
15
|
import { AllowManager, ConditionFunc } from './allow-manager';
|
|
16
16
|
import FixedParamsManager, { Merger, GeneralMerger } from './fixed-params-manager';
|
|
17
17
|
import SnippetManager, { SnippetOptions } from './snippet-manager';
|
|
18
|
+
import Database from '@nocobase/database';
|
|
18
19
|
export interface CanResult {
|
|
19
20
|
role: string;
|
|
20
21
|
resource: string;
|
|
@@ -44,6 +45,14 @@ export interface ListenerContext {
|
|
|
44
45
|
params: RoleActionParams;
|
|
45
46
|
}
|
|
46
47
|
type Listener = (ctx: ListenerContext) => void;
|
|
48
|
+
export type UserProvider = (args: {
|
|
49
|
+
fields: string[];
|
|
50
|
+
}) => Promise<any>;
|
|
51
|
+
export interface ParseJsonTemplateOptions {
|
|
52
|
+
timezone?: string;
|
|
53
|
+
state?: any;
|
|
54
|
+
userProvider?: UserProvider;
|
|
55
|
+
}
|
|
47
56
|
interface CanArgs {
|
|
48
57
|
role?: string;
|
|
49
58
|
resource: string;
|
|
@@ -104,10 +113,6 @@ export declare class ACL extends EventEmitter {
|
|
|
104
113
|
* @deprecated
|
|
105
114
|
*/
|
|
106
115
|
skip(resourceName: string, actionNames: string[] | string, condition?: string | ConditionFunc): void;
|
|
107
|
-
/**
|
|
108
|
-
* @internal
|
|
109
|
-
*/
|
|
110
|
-
parseJsonTemplate(json: any, ctx: any): Promise<any>;
|
|
111
116
|
middleware(): (ctx: any, next: any) => Promise<void>;
|
|
112
117
|
/**
|
|
113
118
|
* @internal
|
|
@@ -116,11 +121,20 @@ export declare class ACL extends EventEmitter {
|
|
|
116
121
|
addGeneralFixedParams(merger: GeneralMerger): void;
|
|
117
122
|
addFixedParams(resource: string, action: string, merger: Merger): void;
|
|
118
123
|
registerSnippet(snippet: SnippetOptions): void;
|
|
119
|
-
/**
|
|
120
|
-
* @internal
|
|
121
|
-
*/
|
|
122
|
-
filterParams(ctx: any, resourceName: any, params: any): any;
|
|
123
124
|
protected addCoreMiddleware(): void;
|
|
124
125
|
protected isAvailableAction(actionName: string): boolean;
|
|
125
126
|
}
|
|
127
|
+
export declare function createUserProvider(options: {
|
|
128
|
+
db?: Database;
|
|
129
|
+
dataSourceManager?: any;
|
|
130
|
+
currentUser?: any;
|
|
131
|
+
}): UserProvider;
|
|
132
|
+
/**
|
|
133
|
+
* @internal
|
|
134
|
+
*/
|
|
135
|
+
export declare function parseJsonTemplate(filter: any, options: ParseJsonTemplateOptions): Promise<any>;
|
|
136
|
+
/**
|
|
137
|
+
* @internal
|
|
138
|
+
*/
|
|
139
|
+
export declare function checkFilterParams(collection: any, filter: any): void;
|
|
126
140
|
export {};
|
package/lib/acl.js
CHANGED
|
@@ -37,7 +37,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
37
37
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
38
|
var acl_exports = {};
|
|
39
39
|
__export(acl_exports, {
|
|
40
|
-
ACL: () => ACL
|
|
40
|
+
ACL: () => ACL,
|
|
41
|
+
checkFilterParams: () => checkFilterParams,
|
|
42
|
+
createUserProvider: () => createUserProvider,
|
|
43
|
+
parseJsonTemplate: () => parseJsonTemplate
|
|
41
44
|
});
|
|
42
45
|
module.exports = __toCommonJS(acl_exports);
|
|
43
46
|
var import_utils = require("@nocobase/utils");
|
|
@@ -279,31 +282,6 @@ const _ACL = class _ACL extends import_events.default {
|
|
|
279
282
|
this.allowManager.allow(resourceName, actionName, condition);
|
|
280
283
|
}
|
|
281
284
|
}
|
|
282
|
-
/**
|
|
283
|
-
* @internal
|
|
284
|
-
*/
|
|
285
|
-
async parseJsonTemplate(json, ctx) {
|
|
286
|
-
var _a, _b, _c, _d, _e;
|
|
287
|
-
if (json.filter) {
|
|
288
|
-
(_b = (_a = ctx.logger) == null ? void 0 : _a.info) == null ? void 0 : _b.call(_a, "parseJsonTemplate.raw", JSON.parse(JSON.stringify(json.filter)));
|
|
289
|
-
const timezone = (_c = ctx == null ? void 0 : ctx.get) == null ? void 0 : _c.call(ctx, "x-timezone");
|
|
290
|
-
const state = JSON.parse(JSON.stringify(ctx.state));
|
|
291
|
-
const filter = await (0, import_utils.parseFilter)(json.filter, {
|
|
292
|
-
timezone,
|
|
293
|
-
now: (/* @__PURE__ */ new Date()).toISOString(),
|
|
294
|
-
vars: {
|
|
295
|
-
ctx: {
|
|
296
|
-
state
|
|
297
|
-
},
|
|
298
|
-
$user: getUser(ctx),
|
|
299
|
-
$nRole: /* @__PURE__ */ __name(() => state.currentRole, "$nRole")
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
json.filter = filter;
|
|
303
|
-
(_e = (_d = ctx.logger) == null ? void 0 : _d.info) == null ? void 0 : _e.call(_d, "parseJsonTemplate.parsed", filter);
|
|
304
|
-
}
|
|
305
|
-
return json;
|
|
306
|
-
}
|
|
307
285
|
middleware() {
|
|
308
286
|
const acl = this;
|
|
309
287
|
return /* @__PURE__ */ __name(async function ACLMiddleware(ctx, next) {
|
|
@@ -377,40 +355,11 @@ const _ACL = class _ACL extends import_events.default {
|
|
|
377
355
|
registerSnippet(snippet) {
|
|
378
356
|
this.snippetManager.register(snippet);
|
|
379
357
|
}
|
|
380
|
-
/**
|
|
381
|
-
* @internal
|
|
382
|
-
*/
|
|
383
|
-
filterParams(ctx, resourceName, params) {
|
|
384
|
-
var _a, _b, _c;
|
|
385
|
-
if ((_a = params == null ? void 0 : params.filter) == null ? void 0 : _a.createdById) {
|
|
386
|
-
const collection = ctx.db.getCollection(resourceName);
|
|
387
|
-
if (!collection || !collection.getField("createdById")) {
|
|
388
|
-
throw new import_no_permission_error.NoPermissionError("createdById field not found");
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if ((_c = (_b = params == null ? void 0 : params.filter) == null ? void 0 : _b.$or) == null ? void 0 : _c.length) {
|
|
392
|
-
const checkCreatedById = /* @__PURE__ */ __name((items) => {
|
|
393
|
-
return items.some(
|
|
394
|
-
(x) => {
|
|
395
|
-
var _a2, _b2;
|
|
396
|
-
return "createdById" in x || ((_a2 = x.$or) == null ? void 0 : _a2.some((y) => "createdById" in y)) || ((_b2 = x.$and) == null ? void 0 : _b2.some((y) => "createdById" in y));
|
|
397
|
-
}
|
|
398
|
-
);
|
|
399
|
-
}, "checkCreatedById");
|
|
400
|
-
if (checkCreatedById(params.filter.$or)) {
|
|
401
|
-
const collection = ctx.db.getCollection(resourceName);
|
|
402
|
-
if (!collection || !collection.getField("createdById")) {
|
|
403
|
-
throw new import_no_permission_error.NoPermissionError("createdById field not found");
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return params;
|
|
408
|
-
}
|
|
409
358
|
addCoreMiddleware() {
|
|
410
359
|
const acl = this;
|
|
411
360
|
this.middlewares.add(
|
|
412
361
|
async (ctx, next) => {
|
|
413
|
-
var _a, _b, _c, _d;
|
|
362
|
+
var _a, _b, _c, _d, _e, _f;
|
|
414
363
|
const resourcerAction = ctx.action;
|
|
415
364
|
const { resourceName, actionName } = ctx.permission;
|
|
416
365
|
const permission = ctx.permission;
|
|
@@ -423,10 +372,20 @@ const _ACL = class _ACL extends import_events.default {
|
|
|
423
372
|
((_c = ctx.log) == null ? void 0 : _c.debug) && ctx.log.debug("acl params", params);
|
|
424
373
|
try {
|
|
425
374
|
if (params && resourcerAction.mergeParams) {
|
|
426
|
-
const
|
|
427
|
-
const
|
|
375
|
+
const db = ctx.database ?? ctx.db;
|
|
376
|
+
const collection = (_d = db == null ? void 0 : db.getCollection) == null ? void 0 : _d.call(db, resourceName);
|
|
377
|
+
checkFilterParams(collection, params == null ? void 0 : params.filter);
|
|
378
|
+
const parsedFilter = await parseJsonTemplate(params.filter, {
|
|
379
|
+
state: ctx.state,
|
|
380
|
+
timezone: getTimezone(ctx),
|
|
381
|
+
userProvider: createUserProvider({
|
|
382
|
+
db: ctx.db,
|
|
383
|
+
currentUser: (_e = ctx.state) == null ? void 0 : _e.currentUser
|
|
384
|
+
})
|
|
385
|
+
});
|
|
386
|
+
const parsedParams = params.filter ? { ...params, filter: parsedFilter ?? params.filter } : params;
|
|
428
387
|
ctx.permission.parsedParams = parsedParams;
|
|
429
|
-
((
|
|
388
|
+
((_f = ctx.log) == null ? void 0 : _f.debug) && ctx.log.debug("acl parsedParams", parsedParams);
|
|
430
389
|
ctx.permission.rawParams = import_lodash.default.cloneDeep(resourcerAction.params);
|
|
431
390
|
if (parsedParams.appends && resourcerAction.params.fields) {
|
|
432
391
|
for (const queryField of resourcerAction.params.fields) {
|
|
@@ -477,31 +436,99 @@ const _ACL = class _ACL extends import_events.default {
|
|
|
477
436
|
};
|
|
478
437
|
__name(_ACL, "ACL");
|
|
479
438
|
let ACL = _ACL;
|
|
480
|
-
function
|
|
481
|
-
|
|
482
|
-
|
|
439
|
+
function getTimezone(ctx) {
|
|
440
|
+
var _a, _b, _c, _d, _e, _f;
|
|
441
|
+
return ((_b = (_a = ctx == null ? void 0 : ctx.request) == null ? void 0 : _a.get) == null ? void 0 : _b.call(_a, "x-timezone")) ?? ((_d = (_c = ctx == null ? void 0 : ctx.request) == null ? void 0 : _c.header) == null ? void 0 : _d["x-timezone"]) ?? ((_f = (_e = ctx == null ? void 0 : ctx.req) == null ? void 0 : _e.headers) == null ? void 0 : _f["x-timezone"]);
|
|
442
|
+
}
|
|
443
|
+
__name(getTimezone, "getTimezone");
|
|
444
|
+
function createUserProvider(options) {
|
|
445
|
+
var _a, _b, _c, _d, _e;
|
|
446
|
+
const db = options.db ?? ((_e = (_d = (_c = (_b = (_a = options.dataSourceManager) == null ? void 0 : _a.dataSources) == null ? void 0 : _b.get) == null ? void 0 : _c.call(_b, "main")) == null ? void 0 : _d.collectionManager) == null ? void 0 : _e.db);
|
|
447
|
+
const currentUser = options.currentUser;
|
|
483
448
|
return async ({ fields }) => {
|
|
484
|
-
|
|
485
|
-
const userFields = fields.filter((f) => f && db.getFieldByPath("users." + f));
|
|
486
|
-
(_a = ctx.logger) == null ? void 0 : _a.info("filter-parse: ", { userFields });
|
|
487
|
-
if (!ctx.state.currentUser) {
|
|
449
|
+
if (!db) {
|
|
488
450
|
return;
|
|
489
451
|
}
|
|
452
|
+
if (!currentUser) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const userFields = fields.filter((f) => f && db.getFieldByPath("users." + f));
|
|
490
456
|
if (!userFields.length) {
|
|
491
457
|
return;
|
|
492
458
|
}
|
|
493
459
|
const user = await db.getRepository("users").findOne({
|
|
494
|
-
filterByTk:
|
|
460
|
+
filterByTk: currentUser.id,
|
|
495
461
|
fields: userFields
|
|
496
462
|
});
|
|
497
|
-
(_b = ctx.logger) == null ? void 0 : _b.info("filter-parse: ", {
|
|
498
|
-
$user: user == null ? void 0 : user.toJSON()
|
|
499
|
-
});
|
|
500
463
|
return user;
|
|
501
464
|
};
|
|
502
465
|
}
|
|
503
|
-
__name(
|
|
466
|
+
__name(createUserProvider, "createUserProvider");
|
|
467
|
+
function containsCreatedByIdFilter(input, seen = /* @__PURE__ */ new Set()) {
|
|
468
|
+
if (!input) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
if (Array.isArray(input)) {
|
|
472
|
+
return input.some((item) => containsCreatedByIdFilter(item, seen));
|
|
473
|
+
}
|
|
474
|
+
if (!import_lodash.default.isPlainObject(input)) {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
if (seen.has(input)) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
seen.add(input);
|
|
481
|
+
for (const [key, value] of Object.entries(input)) {
|
|
482
|
+
if (isCreatedByIdKey(key)) {
|
|
483
|
+
return true;
|
|
484
|
+
}
|
|
485
|
+
if (containsCreatedByIdFilter(value, seen)) {
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
__name(containsCreatedByIdFilter, "containsCreatedByIdFilter");
|
|
492
|
+
function isCreatedByIdKey(key) {
|
|
493
|
+
return key === "createdById" || key.startsWith("createdById.") || key.startsWith("createdById$");
|
|
494
|
+
}
|
|
495
|
+
__name(isCreatedByIdKey, "isCreatedByIdKey");
|
|
496
|
+
async function parseJsonTemplate(filter, options) {
|
|
497
|
+
if (!filter) {
|
|
498
|
+
return filter;
|
|
499
|
+
}
|
|
500
|
+
const timezone = options == null ? void 0 : options.timezone;
|
|
501
|
+
const state = JSON.parse(JSON.stringify((options == null ? void 0 : options.state) || {}));
|
|
502
|
+
const parsedFilter = await (0, import_utils.parseFilter)(filter, {
|
|
503
|
+
timezone,
|
|
504
|
+
now: (/* @__PURE__ */ new Date()).toISOString(),
|
|
505
|
+
vars: {
|
|
506
|
+
ctx: {
|
|
507
|
+
state
|
|
508
|
+
},
|
|
509
|
+
$user: (options == null ? void 0 : options.userProvider) || (async () => void 0),
|
|
510
|
+
$nRole: /* @__PURE__ */ __name(() => state.currentRole, "$nRole")
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
return parsedFilter;
|
|
514
|
+
}
|
|
515
|
+
__name(parseJsonTemplate, "parseJsonTemplate");
|
|
516
|
+
function checkFilterParams(collection, filter) {
|
|
517
|
+
if (!filter) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (!containsCreatedByIdFilter(filter)) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
if (!collection || !collection.getField("createdById")) {
|
|
524
|
+
throw new import_no_permission_error.NoPermissionError("createdById field not found");
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
__name(checkFilterParams, "checkFilterParams");
|
|
504
528
|
// Annotate the CommonJS export names for ESM import in node:
|
|
505
529
|
0 && (module.exports = {
|
|
506
|
-
ACL
|
|
530
|
+
ACL,
|
|
531
|
+
checkFilterParams,
|
|
532
|
+
createUserProvider,
|
|
533
|
+
parseJsonTemplate
|
|
507
534
|
});
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/acl",
|
|
3
|
-
"version": "2.1.0-alpha.
|
|
3
|
+
"version": "2.1.0-alpha.8",
|
|
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": "2.1.0-alpha.6",
|
|
10
|
-
"@nocobase/utils": "2.1.0-alpha.6",
|
|
11
9
|
"minimatch": "^5.1.1"
|
|
12
10
|
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@nocobase/database": "2.x",
|
|
13
|
+
"@nocobase/resourcer": "2.x",
|
|
14
|
+
"@nocobase/utils": "2.x"
|
|
15
|
+
},
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
15
18
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
16
19
|
"directory": "packages/acl"
|
|
17
20
|
},
|
|
18
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "eda3bfb9df40d4394905e178f1c5331adbec4e76"
|
|
19
22
|
}
|