1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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]; 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; return node;
} }
/** /**
@ -740,8 +755,10 @@ RED.nodes = (function() {
} }
if (exportCreds) { if (exportCreds) {
var credentialSet = {}; var credentialSet = {};
if (/^subflow:/.test(node.type) && n.credentials) { if ((/^subflow:/.test(node.type) ||
// A subflow instance node can have arbitrary creds (node.type === "group")) &&
n.credentials) {
// A subflow instance/group node can have arbitrary creds
for (var sfCred in n.credentials) { for (var sfCred in n.credentials) {
if (n.credentials.hasOwnProperty(sfCred)) { if (n.credentials.hasOwnProperty(sfCred)) {
if (!n.credentials._ || if (!n.credentials._ ||

View File

@ -507,7 +507,7 @@ RED.editor = (function() {
done(); done();
} }
} }
if (definition.credentials || /^subflow:/.test(definition.type)) { if (definition.credentials || /^subflow:/.test(definition.type) || (node.type === "group")) {
if (node.credentials) { if (node.credentials) {
populateCredentialsInputs(node, definition.credentials, node.credentials, prefix); populateCredentialsInputs(node, definition.credentials, node.credentials, prefix);
completePrepare(); completePrepare();

View File

@ -120,6 +120,36 @@ RED.workspaces = (function() {
dialogForm.i18n(); 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) { function showEditWorkspaceDialog(id) {
var workspace = RED.nodes.workspace(id); var workspace = RED.nodes.workspace(id);
if (!workspace) { if (!workspace) {
@ -180,7 +210,6 @@ RED.workspaces = (function() {
var old_env = workspace.env; var old_env = workspace.env;
var new_env = RED.subflow.exportSubflowInstanceEnv(workspace); var new_env = RED.subflow.exportSubflowInstanceEnv(workspace);
console.log("; NE", new_env);
if (new_env && (new_env.length > 0)) { if (new_env && (new_env.length > 0)) {
new_env.forEach(function(prop) { new_env.forEach(function(prop) {
if (prop.type === "cred") { if (prop.type === "cred") {
@ -285,6 +314,8 @@ RED.workspaces = (function() {
}).appendTo(editorContent).hide(), }).appendTo(editorContent).hide(),
iconClass: "fa fa-list", iconClass: "fa fa-list",
}; };
function cb() {
RED.subflow.buildPropertiesForm(workspace); RED.subflow.buildPropertiesForm(workspace);
editorTabs.addTab(tabPropertiesTab); editorTabs.addTab(tabPropertiesTab);
@ -299,6 +330,21 @@ RED.workspaces = (function() {
}) })
finishedBuilding = true; finishedBuilding = true;
RED.tray.resize(); 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();
});
}
}, },
close: function() { close: function() {
if (RED.view.state() != RED.state.IMPORT_DRAGGING) { if (RED.view.state() != RED.state.IMPORT_DRAGGING) {

View File

@ -233,7 +233,9 @@ var api = module.exports = {
} }
var sendCredentials = {}; var sendCredentials = {};
var cred; var cred;
if (/^subflow(:|$)/.test(opts.type)) { if (/^subflow(:|$)/.test(opts.type) ||
(opts.type === "tab") ||
(opts.type === "group")) {
for (cred in credentials) { for (cred in credentials) {
if (credentials.hasOwnProperty(cred)) { if (credentials.hasOwnProperty(cred)) {
sendCredentials['has_'+cred] = credentials[cred] != null && credentials[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"); var flowUtil = require("./util");
const context = require('../nodes/context'); const context = require('../nodes/context');
const hooks = require("@node-red/util").hooks; const hooks = require("@node-red/util").hooks;
const credentials = require("../nodes/credentials");
var Subflow; var Subflow;
var Log; var Log;
@ -422,11 +423,22 @@ class Flow {
*/ */
getFlowSetting(name) { getFlowSetting(name) {
const flow = this.flow; const flow = this.flow;
if (flow.credentials === undefined) {
flow.credentials = credentials.get(flow.id) || {};
}
if (flow.env) { if (flow.env) {
if (!name.startsWith("$parent.")) { if (!name.startsWith("$parent.")) {
if (!flow._env) { if (!flow._env) {
const envs = 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); flow._env = Object.fromEntries(entries);
} }
const env = flow._env[name]; const env = flow._env[name];
@ -466,6 +478,82 @@ class Flow {
return null; 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 * Get a flow setting value. This currently automatically defers to the parent
* flow which, as defined in ./index.js returns `process.env[key]`. * 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); 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 * 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; const node = this.subflowInstance;
if (node.g) { if (node.g) {
const group = this.getGroupNode(node.g); const group = this.getGroupNode(node.g);
const result = getGroupSetting(node, group, this, name); const result = this.getGroupEnvSetting(node, group, name);
if (result) { if (result) {
return result.val; return result.val;
} }

View File

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

View File

@ -515,66 +515,6 @@ function setObjectProperty(msg,prop,value,createMissing) {
return true; 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. * Get value of environment variable.
* @param {Node} node - accessing node * @param {Node} node - accessing node
@ -586,7 +526,7 @@ function getSetting(node, name, flow_) {
if (flow) { if (flow) {
if (node && node.g) { if (node && node.g) {
const group = flow.getGroupNode(node.g); const group = flow.getGroupNode(node.g);
const result = getGroupSetting(node, group, flow, name); const result = flow.getGroupEnvSetting(node, group, name);
if (result) { if (result) {
return result.val; return result.val;
} }