@nocobase/flow-engine
Version:
A standalone flow engine for NocoBase, managing workflows, models, and actions.
116 lines (114 loc) • 4.5 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Acl_exports = {};
__export(Acl_exports, {
ACL: () => ACL
});
module.exports = __toCommonJS(Acl_exports);
const _ACL = class _ACL {
constructor(flowEngine) {
this.flowEngine = flowEngine;
}
dataSources = {};
loaded = false;
loadingPromise = null;
async load() {
if (this.loaded) return;
if (!this.loadingPromise) {
this.loadingPromise = (async () => {
const { data } = await this.flowEngine.context.api.request({
url: "roles:check"
});
this.dataSources = data || {};
this.loaded = true;
})();
}
await this.loadingPromise;
}
getActionAlias(actionName) {
var _a, _b;
return ((_b = (_a = this.dataSources.data) == null ? void 0 : _a.actionAlias) == null ? void 0 : _b[actionName]) || actionName;
}
inResources(resourceName) {
var _a, _b, _c;
return (_c = (_b = (_a = this.dataSources.data) == null ? void 0 : _a.resources) == null ? void 0 : _b.includes) == null ? void 0 : _c.call(_b, resourceName);
}
getResourceActionParams(resourceName, actionName) {
var _a, _b, _c, _d;
const actionAlias = this.getActionAlias(actionName);
return ((_b = (_a = this.dataSources.data) == null ? void 0 : _a.actions) == null ? void 0 : _b[`${resourceName}:${actionAlias}`]) || ((_d = (_c = this.dataSources.data) == null ? void 0 : _c.actions) == null ? void 0 : _d[actionName]);
}
getStrategyActionParams(actionName) {
var _a, _b, _c;
const actionAlias = this.getActionAlias(actionName);
const strategyAction = (_c = (_b = (_a = this.dataSources.data) == null ? void 0 : _a.strategy) == null ? void 0 : _b.actions) == null ? void 0 : _c.find((action) => {
const [value] = action.split(":");
return value === actionAlias;
});
return strategyAction ? {} : null;
}
parseAction(options) {
var _a;
const { resourceName, actionName, dataSourceKey = "main" } = options;
const targetResource = (resourceName == null ? void 0 : resourceName.includes(".")) && ((_a = this.flowEngine.context.dataSourceManager.getDataSource(dataSourceKey).collectionManager.getAssociation(resourceName)) == null ? void 0 : _a.target);
if (this.inResources(targetResource)) {
return this.getResourceActionParams(targetResource, actionName);
}
if (this.inResources(resourceName)) {
return this.getResourceActionParams(resourceName, actionName);
}
return this.getStrategyActionParams(actionName);
}
parseField(options) {
const { fields } = options;
const params = this.parseAction(options);
const whitelist = [].concat((params == null ? void 0 : params.whitelist) || []).concat((params == null ? void 0 : params.fields) || []).concat((params == null ? void 0 : params.appends) || []);
if (params && !Object.keys(params).length) {
return true;
}
const allowed = whitelist.includes(fields[0]);
return allowed;
}
async aclCheck(options) {
await this.load();
const { data } = this.dataSources;
const { allowAll } = data;
if (allowAll) {
return true;
}
if (options.fields) {
return this.parseField(options);
}
return this.parseAction(options);
}
};
__name(_ACL, "ACL");
let ACL = _ACL;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ACL
});