@tachybase/acl 0.23.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.
@@ -0,0 +1,108 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var allow_manager_exports = {};
20
+ __export(allow_manager_exports, {
21
+ AllowManager: () => AllowManager
22
+ });
23
+ module.exports = __toCommonJS(allow_manager_exports);
24
+ const _AllowManager = class _AllowManager {
25
+ constructor(acl) {
26
+ this.acl = acl;
27
+ this.registerAllowCondition("loggedIn", (ctx) => {
28
+ return ctx.state.currentUser;
29
+ });
30
+ this.registerAllowCondition("public", (ctx) => {
31
+ return true;
32
+ });
33
+ this.registerAllowCondition("allowConfigure", async (ctx) => {
34
+ var _a;
35
+ const roleName = ctx.state.currentRole;
36
+ if (!roleName) {
37
+ return false;
38
+ }
39
+ const role = acl.getRole(roleName);
40
+ if (!role) {
41
+ return false;
42
+ }
43
+ return (_a = role.getStrategy()) == null ? void 0 : _a.allowConfigure;
44
+ });
45
+ }
46
+ skipActions = /* @__PURE__ */ new Map();
47
+ registeredCondition = /* @__PURE__ */ new Map();
48
+ allow(resourceName, actionName, condition) {
49
+ const actionMap = this.skipActions.get(resourceName) || /* @__PURE__ */ new Map();
50
+ actionMap.set(actionName, condition || true);
51
+ this.skipActions.set(resourceName, actionMap);
52
+ }
53
+ getAllowedConditions(resourceName, actionName) {
54
+ const fetchActionSteps = ["*", resourceName];
55
+ const results = [];
56
+ for (const fetchActionStep of fetchActionSteps) {
57
+ const resource = this.skipActions.get(fetchActionStep);
58
+ if (resource) {
59
+ for (const fetchActionStep2 of ["*", actionName]) {
60
+ const condition = resource.get(fetchActionStep2);
61
+ if (condition) {
62
+ results.push(typeof condition === "string" ? this.registeredCondition.get(condition) : condition);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ return results;
68
+ }
69
+ registerAllowCondition(name, condition) {
70
+ this.registeredCondition.set(name, condition);
71
+ }
72
+ async isAllowed(resourceName, actionName, ctx) {
73
+ const skippedConditions = this.getAllowedConditions(resourceName, actionName);
74
+ for (const skippedCondition of skippedConditions) {
75
+ if (skippedCondition) {
76
+ let skipResult = false;
77
+ if (typeof skippedCondition === "function") {
78
+ skipResult = await skippedCondition(ctx);
79
+ } else if (skippedCondition) {
80
+ skipResult = true;
81
+ }
82
+ if (skipResult) {
83
+ return true;
84
+ }
85
+ }
86
+ }
87
+ return false;
88
+ }
89
+ aclMiddleware() {
90
+ return async (ctx, next) => {
91
+ const { resourceName, actionName } = ctx.action;
92
+ const skip = await this.acl.allowManager.isAllowed(resourceName, actionName, ctx);
93
+ if (skip) {
94
+ ctx.permission = {
95
+ ...ctx.permission || {},
96
+ skip: true
97
+ };
98
+ }
99
+ await next();
100
+ };
101
+ }
102
+ };
103
+ __name(_AllowManager, "AllowManager");
104
+ let AllowManager = _AllowManager;
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ AllowManager
108
+ });
@@ -0,0 +1,10 @@
1
+ export type Merger = () => object;
2
+ export type ActionPath = string;
3
+ export default class FixedParamsManager {
4
+ merger: Map<string, Merger[]>;
5
+ addParams(resource: string, action: string, merger: Merger): void;
6
+ getParamsMerger(resource: string, action: string): Merger[];
7
+ protected getActionPath(resource: string, action: string): string;
8
+ getParams(resource: string, action: string, extraParams?: any): {};
9
+ static mergeParams(a: any, b: any): void;
10
+ }
@@ -0,0 +1,62 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var fixed_params_manager_exports = {};
20
+ __export(fixed_params_manager_exports, {
21
+ default: () => FixedParamsManager
22
+ });
23
+ module.exports = __toCommonJS(fixed_params_manager_exports);
24
+ var import_utils = require("@tachybase/utils");
25
+ const SPLIT = ":";
26
+ const _FixedParamsManager = class _FixedParamsManager {
27
+ merger = /* @__PURE__ */ new Map();
28
+ addParams(resource, action, merger) {
29
+ const path = this.getActionPath(resource, action);
30
+ this.merger.set(path, [...this.getParamsMerger(resource, action), merger]);
31
+ }
32
+ getParamsMerger(resource, action) {
33
+ const path = this.getActionPath(resource, action);
34
+ return this.merger.get(path) || [];
35
+ }
36
+ getActionPath(resource, action) {
37
+ return `${resource}${SPLIT}${action}`;
38
+ }
39
+ getParams(resource, action, extraParams = {}) {
40
+ const results = {};
41
+ for (const merger of this.getParamsMerger(resource, action)) {
42
+ _FixedParamsManager.mergeParams(results, merger());
43
+ }
44
+ if (extraParams) {
45
+ _FixedParamsManager.mergeParams(results, extraParams);
46
+ }
47
+ return results;
48
+ }
49
+ static mergeParams(a, b) {
50
+ (0, import_utils.assign)(a, b, {
51
+ filter: "andMerge",
52
+ fields: "intersect",
53
+ appends: "union",
54
+ except: "union",
55
+ whitelist: "intersect",
56
+ blacklist: "intersect",
57
+ sort: "overwrite"
58
+ });
59
+ }
60
+ };
61
+ __name(_FixedParamsManager, "FixedParamsManager");
62
+ let FixedParamsManager = _FixedParamsManager;
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './acl';
2
+ export * from './acl-available-action';
3
+ export * from './acl-available-strategy';
4
+ export * from './acl-resource';
5
+ export * from './acl-role';
6
+ export * from './no-permission-error';
7
+ export * from './skip-middleware';
package/lib/index.js ADDED
@@ -0,0 +1,33 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var src_exports = {};
16
+ module.exports = __toCommonJS(src_exports);
17
+ __reExport(src_exports, require("./acl"), module.exports);
18
+ __reExport(src_exports, require("./acl-available-action"), module.exports);
19
+ __reExport(src_exports, require("./acl-available-strategy"), module.exports);
20
+ __reExport(src_exports, require("./acl-resource"), module.exports);
21
+ __reExport(src_exports, require("./acl-role"), module.exports);
22
+ __reExport(src_exports, require("./no-permission-error"), module.exports);
23
+ __reExport(src_exports, require("./skip-middleware"), module.exports);
24
+ // Annotate the CommonJS export names for ESM import in node:
25
+ 0 && (module.exports = {
26
+ ...require("./acl"),
27
+ ...require("./acl-available-action"),
28
+ ...require("./acl-available-strategy"),
29
+ ...require("./acl-resource"),
30
+ ...require("./acl-role"),
31
+ ...require("./no-permission-error"),
32
+ ...require("./skip-middleware")
33
+ });
@@ -0,0 +1,4 @@
1
+ declare class NoPermissionError extends Error {
2
+ constructor(...args: any[]);
3
+ }
4
+ export { NoPermissionError };
@@ -0,0 +1,34 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var no_permission_error_exports = {};
20
+ __export(no_permission_error_exports, {
21
+ NoPermissionError: () => NoPermissionError
22
+ });
23
+ module.exports = __toCommonJS(no_permission_error_exports);
24
+ const _NoPermissionError = class _NoPermissionError extends Error {
25
+ constructor(...args) {
26
+ super(...args);
27
+ }
28
+ };
29
+ __name(_NoPermissionError, "NoPermissionError");
30
+ let NoPermissionError = _NoPermissionError;
31
+ // Annotate the CommonJS export names for ESM import in node:
32
+ 0 && (module.exports = {
33
+ NoPermissionError
34
+ });
@@ -0,0 +1,6 @@
1
+ export declare const skip: (options: ACLSkipOptions) => (ctx: any, next: any) => Promise<void>;
2
+ interface ACLSkipOptions {
3
+ resourceName: string;
4
+ actionName: string;
5
+ }
6
+ export {};
@@ -0,0 +1,38 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var skip_middleware_exports = {};
20
+ __export(skip_middleware_exports, {
21
+ skip: () => skip
22
+ });
23
+ module.exports = __toCommonJS(skip_middleware_exports);
24
+ const skip = /* @__PURE__ */ __name((options) => {
25
+ return /* @__PURE__ */ __name(async function ACLSkipMiddleware(ctx, next) {
26
+ const { resourceName, actionName } = ctx.action;
27
+ if (resourceName === options.resourceName && actionName === options.actionName) {
28
+ ctx.permission = {
29
+ skip: true
30
+ };
31
+ }
32
+ await next();
33
+ }, "ACLSkipMiddleware");
34
+ }, "skip");
35
+ // Annotate the CommonJS export names for ESM import in node:
36
+ 0 && (module.exports = {
37
+ skip
38
+ });
@@ -0,0 +1,19 @@
1
+ export type SnippetOptions = {
2
+ name: string;
3
+ actions: Array<string>;
4
+ };
5
+ declare class Snippet {
6
+ name: string;
7
+ actions: Array<string>;
8
+ constructor(name: string, actions: Array<string>);
9
+ }
10
+ export type SnippetGroup = {
11
+ name: string;
12
+ snippets: SnippetOptions[];
13
+ };
14
+ declare class SnippetManager {
15
+ snippets: Map<string, Snippet>;
16
+ register(snippet: SnippetOptions): void;
17
+ allow(actionPath: string, snippetName: string): boolean;
18
+ }
19
+ export default SnippetManager;
@@ -0,0 +1,64 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var snippet_manager_exports = {};
30
+ __export(snippet_manager_exports, {
31
+ default: () => snippet_manager_default
32
+ });
33
+ module.exports = __toCommonJS(snippet_manager_exports);
34
+ var import_minimatch = __toESM(require("minimatch"));
35
+ const _Snippet = class _Snippet {
36
+ constructor(name, actions) {
37
+ this.name = name;
38
+ this.actions = actions;
39
+ }
40
+ };
41
+ __name(_Snippet, "Snippet");
42
+ let Snippet = _Snippet;
43
+ const _SnippetManager = class _SnippetManager {
44
+ snippets = /* @__PURE__ */ new Map();
45
+ register(snippet) {
46
+ this.snippets.set(snippet.name, snippet);
47
+ }
48
+ allow(actionPath, snippetName) {
49
+ const negated = snippetName.startsWith("!");
50
+ snippetName = negated ? snippetName.slice(1) : snippetName;
51
+ const snippet = this.snippets.get(snippetName);
52
+ if (!snippet) {
53
+ return null;
54
+ }
55
+ const matched = snippet.actions.some((action) => (0, import_minimatch.default)(actionPath, action));
56
+ if (matched) {
57
+ return negated ? false : true;
58
+ }
59
+ return null;
60
+ }
61
+ };
62
+ __name(_SnippetManager, "SnippetManager");
63
+ let SnippetManager = _SnippetManager;
64
+ var snippet_manager_default = SnippetManager;
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@tachybase/acl",
3
+ "version": "0.23.8",
4
+ "description": "",
5
+ "license": "Apache-2.0",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "dependencies": {
9
+ "koa-compose": "^4.1.0",
10
+ "lodash": "4.17.21",
11
+ "minimatch": "^5.1.6",
12
+ "@tachybase/resourcer": "0.23.8",
13
+ "@tachybase/utils": "0.23.8"
14
+ },
15
+ "devDependencies": {
16
+ "@types/lodash": "4.17.13",
17
+ "@types/node": "20.17.10"
18
+ },
19
+ "scripts": {
20
+ "build": "tachybase-build --no-dts @tachybase/acl"
21
+ }
22
+ }