update according to PR comments

This commit is contained in:
Hiroyasu Nishiyama 2021-08-30 08:00:58 +09:00
parent 6aecc3915c
commit d78e5932f9
8 changed files with 176 additions and 128 deletions

View File

@ -691,6 +691,21 @@ RED.nodes = (function() {
node[d] = n[d];
}
}
var credentialSet = {};
if (n.credentials) {
for (var tabCred in n.credentials) {
if (n.credentials.hasOwnProperty(tabCred)) {
if (!n.credentials._ ||
n.credentials["has_"+tabCred] != n.credentials._["has_"+tabCred] ||
(n.credentials["has_"+tabCred] && n.credentials[tabCred])) {
credentialSet[tabCred] = n.credentials[tabCred];
}
}
}
if (Object.keys(credentialSet).length > 0) {
node.credentials = credentialSet;
}
}
return node;
}
/**
@ -740,8 +755,10 @@ RED.nodes = (function() {
}
if (exportCreds) {
var credentialSet = {};
if (/^subflow:/.test(node.type) && n.credentials) {
// A subflow instance node can have arbitrary creds
if ((/^subflow:/.test(node.type) ||
(node.type === "group")) &&
n.credentials) {
// A subflow instance/group node can have arbitrary creds
for (var sfCred in n.credentials) {
if (n.credentials.hasOwnProperty(sfCred)) {
if (!n.credentials._ ||

View File

@ -507,7 +507,7 @@ RED.editor = (function() {
done();
}
}
if (definition.credentials || /^subflow:/.test(definition.type)) {
if (definition.credentials || /^subflow:/.test(definition.type) || (node.type === "group")) {
if (node.credentials) {
populateCredentialsInputs(node, definition.credentials, node.credentials, prefix);
completePrepare();
@ -1467,7 +1467,7 @@ var buildingEditDialog = false;
changes.env = editing_node.env;
changed = true;
}
}
}
if (changed) {
var wasChanged = editing_node.changed;

View File

@ -120,6 +120,36 @@ RED.workspaces = (function() {
dialogForm.i18n();
}
function getNodeCredentials(type, id, done) {
var timeoutNotification;
var intialTimeout = setTimeout(function() {
timeoutNotification = RED.notify($('<p data-i18n="[prepend]editor.loadCredentials"> <img src="red/images/spin.svg"/></p>').i18n(),{fixed: true})
},800);
$.ajax({
url: "credentials/tab/" + id,
dataType: 'json',
success: function(data) {
if (timeoutNotification) {
timeoutNotification.close();
timeoutNotification = null;
}
clearTimeout(intialTimeout);
done(data);
},
error: function(jqXHR,status,error) {
if (timeoutNotification) {
timeoutNotification.close();
timeoutNotification = null;
}
clearTimeout(intialTimeout);
RED.notify(RED._("editor.errors.credentialLoadFailed"),"error")
done(null);
},
timeout: 30000,
});
}
function showEditWorkspaceDialog(id) {
var workspace = RED.nodes.workspace(id);
if (!workspace) {
@ -180,7 +210,6 @@ RED.workspaces = (function() {
var old_env = workspace.env;
var new_env = RED.subflow.exportSubflowInstanceEnv(workspace);
console.log("; NE", new_env);
if (new_env && (new_env.length > 0)) {
new_env.forEach(function(prop) {
if (prop.type === "cred") {
@ -285,20 +314,37 @@ RED.workspaces = (function() {
}).appendTo(editorContent).hide(),
iconClass: "fa fa-list",
};
RED.subflow.buildPropertiesForm(workspace);
editorTabs.addTab(tabPropertiesTab);
if (!workspace.hasOwnProperty("disabled")) {
workspace.disabled = false;
function cb() {
RED.subflow.buildPropertiesForm(workspace);
editorTabs.addTab(tabPropertiesTab);
if (!workspace.hasOwnProperty("disabled")) {
workspace.disabled = false;
}
$('<input id="node-input-disabled" type="checkbox">').prop("checked",workspace.disabled).appendTo(trayFooterLeft).toggleButton({
enabledIcon: "fa-circle-thin",
disabledIcon: "fa-ban",
invertState: true
})
finishedBuilding = true;
RED.tray.resize();
}
if (workspace.credentials) {
cb();
}
else {
getNodeCredentials("tab", workspace.id, function(data) {
if (data) {
workspace.credentials = data;
workspace.credentials._ = $.extend(true,{},data);
}
cb();
});
}
$('<input id="node-input-disabled" type="checkbox">').prop("checked",workspace.disabled).appendTo(trayFooterLeft).toggleButton({
enabledIcon: "fa-circle-thin",
disabledIcon: "fa-ban",
invertState: true
})
finishedBuilding = true;
RED.tray.resize();
},
close: function() {
if (RED.view.state() != RED.state.IMPORT_DRAGGING) {

View File

@ -233,7 +233,9 @@ var api = module.exports = {
}
var sendCredentials = {};
var cred;
if (/^subflow(:|$)/.test(opts.type)) {
if (/^subflow(:|$)/.test(opts.type) ||
(opts.type === "tab") ||
(opts.type === "group")) {
for (cred in credentials) {
if (credentials.hasOwnProperty(cred)) {
sendCredentials['has_'+cred] = credentials[cred] != null && credentials[cred] !== '';

View File

@ -20,6 +20,7 @@ const events = require("@node-red/util").events;
var flowUtil = require("./util");
const context = require('../nodes/context');
const hooks = require("@node-red/util").hooks;
const credentials = require("../nodes/credentials");
var Subflow;
var Log;
@ -422,11 +423,22 @@ class Flow {
*/
getFlowSetting(name) {
const flow = this.flow;
if (flow.credentials === undefined) {
flow.credentials = credentials.get(flow.id) || {};
}
if (flow.env) {
if (!name.startsWith("$parent.")) {
if (!flow._env) {
const envs = flow.env;
const entries = envs.map((env) => [env.name, env]);
const entries = envs.map((env) => {
if (env.type === "cred") {
const cred = flow.credentials;
if (cred.hasOwnProperty(env.name)) {
env.value = cred[env.name];
}
}
return [env.name, env]
});
flow._env = Object.fromEntries(entries);
}
const env = flow._env[name];
@ -466,6 +478,82 @@ class Flow {
return null;
}
/*!
* Get value of environment variable defined in group node.
* @param {String} group - group node
* @param {String} name - name of variable
* @return {Object} object containing the value in val property or null if not defined
*/
getGroupEnvSetting(node, group, name) {
if (group) {
if (group.credentials === undefined) {
group.credentials = credentials.get(group.id) || {};
}
if (!name.startsWith("$parent.")) {
if (group.env) {
if (!group._env) {
const envs = group.env;
const entries = envs.map((env) => {
if (env.type === "cred") {
const cred = group.credentials;
if (cred.hasOwnProperty(env.name)) {
env.value = cred[env.name];
}
}
return [env.name, env];
return [env.name, env];
});
group._env = Object.fromEntries(entries);
}
const env = group._env[name];
if (env) {
let value = env.value;
const type = env.type;
if ((type !== "env") ||
(value !== name)) {
if (type === "env") {
value = value.replace(new RegExp("\\${"+name+"}","g"),"${$parent."+name+"}");
}
if (type === "bool") {
const val
= ((value === "true") ||
(value === true));
return {
val: val
};
}
if (type === "cred") {
return {
val: value
};
}
try {
var val = redUtil.evaluateNodeProperty(value, type, node, null, null);
return {
val: val
};
}
catch (e) {
this.error(e);
return null;
}
}
}
}
}
else {
name = name.substring(8);
}
if (group.g) {
const parent = this.getGroupNode(group.g);
return this.getGroupEnvSetting(node, parent, name);
}
}
return null;
}
/**
* Get a flow setting value. This currently automatically defers to the parent
* flow which, as defined in ./index.js returns `process.env[key]`.

View File

@ -43,54 +43,6 @@ function evaluateInputValue(value, type, node) {
return redUtil.evaluateNodeProperty(value, type, node, null, null);
}
/*!
* Get value of environment variable defined in group node.
* @param {String} group - group node
* @param {String} name - name of variable
* @return {Object} object containing the value in val property or null if not defined
*/
function getGroupSetting(node, group, flow, name) {
if (group) {
if (!name.startsWith("$parent.")) {
if (group.env) {
if (!group._env) {
const envs = group.env;
const entries = envs.map((env) => [env.name, env]);
group._env = Object.fromEntries(entries);
}
const env = group._env[name];
if (env) {
let value = env.value;
const type = env.type;
if ((type !== "env") || (value !== name)) {
if (type === "env") {
value = value.replace(new RegExp("\\${"+name+"}","g"),"${$parent."+name+"}");
}
try {
const val = evaluateInputValue(value, type, node);
return {
val: val
};
}
catch (e) {
this.error(e);
return null;
}
}
}
}
}
else {
name = name.substring(8);
}
if (group.g && flow) {
const parent = flow.getGroupNode(group.g);
return getGroupSetting(node, parent, flow, name);
}
}
return null;
}
/**
* This class represents a subflow - which is handled as a special type of Flow
*/
@ -421,7 +373,7 @@ class Subflow extends Flow {
const node = this.subflowInstance;
if (node.g) {
const group = this.getGroupNode(node.g);
const result = getGroupSetting(node, group, this, name);
const result = this.getGroupEnvSetting(node, group, name);
if (result) {
return result.val;
}

View File

@ -703,6 +703,9 @@ async function updateFlow(id,newFlow, user) {
if (newFlow.hasOwnProperty('env')) {
tabNode.env = newFlow.env;
}
if (newFlow.hasOwnProperty('credentials')) {
tabNode.credentials = newFlow.credentials;
}
nodes = [tabNode].concat(newFlow.nodes||[]).concat(newFlow.configs||[]);
nodes.forEach(function(n) {

View File

@ -515,66 +515,6 @@ function setObjectProperty(msg,prop,value,createMissing) {
return true;
}
/*!
* Get value of environment variable defined in group node.
* @param {String} group - group node
* @param {String} name - name of variable
* @return {Object} object containing the value in val property or null if not defined
*/
function getGroupSetting(node, group, flow, name) {
if (group) {
if (!name.startsWith("$parent.")) {
if (group.env) {
if (!group._env) {
const envs = group.env;
const entries = envs.map((env) => [env.name, env]);
group._env = Object.fromEntries(entries);
}
const env = group._env[name];
if (env) {
let value = env.value;
const type = env.type;
if ((type !== "env") || (value !== name)) {
if (type === "env") {
value = value.replace(new RegExp("\\${"+name+"}","g"),"${$parent."+name+"}");
}
try {
if (type === "bool") {
const val = ((value === "true") ||
(value === true));
return {
val: val
};
}
if (type === "cred") {
return {
val: value
};
}
var val = evaluateNodeProperty(value, type, node, null, null);
return {
val: val
};
}
catch (e) {
this.error(e);
return null;
}
}
}
}
}
else {
name = name.substring(8);
}
if (group.g && flow) {
const parent = flow.getGroupNode(group.g);
return getGroupSetting(node, parent, flow, name);
}
}
return null;
}
/*!
* Get value of environment variable.
* @param {Node} node - accessing node
@ -586,7 +526,7 @@ function getSetting(node, name, flow_) {
if (flow) {
if (node && node.g) {
const group = flow.getGroupNode(node.g);
const result = getGroupSetting(node, group, flow, name);
const result = flow.getGroupEnvSetting(node, group, name);
if (result) {
return result.val;
}