mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Tweak the credential handling
- use node.credentials rather than node._creds for consistency with the runtime - simplify the structure of node.credentials
This commit is contained in:
parent
760dd022dc
commit
b604db83f6
@ -92,13 +92,13 @@ var RED = function() {
|
|||||||
node.dirty = true;
|
node.dirty = true;
|
||||||
node.changed = false;
|
node.changed = false;
|
||||||
}
|
}
|
||||||
if(node._creds) {
|
if(node.credentials) {
|
||||||
delete node._creds;
|
delete node.credentials;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
RED.nodes.eachConfig(function (confNode) {
|
RED.nodes.eachConfig(function (confNode) {
|
||||||
if (confNode._creds) {
|
if (confNode.credentials) {
|
||||||
delete confNode._creds;
|
delete confNode.credentials;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Once deployed, cannot undo back to a clean state
|
// Once deployed, cannot undo back to a clean state
|
||||||
|
@ -183,8 +183,15 @@ RED.nodes = function() {
|
|||||||
for (var d in n._def.defaults) {
|
for (var d in n._def.defaults) {
|
||||||
node[d] = n[d];
|
node[d] = n[d];
|
||||||
}
|
}
|
||||||
if(exportCreds && n._creds) {
|
if(exportCreds && n.credentials) {
|
||||||
node._creds = n._creds.send;
|
node.credentials = {};
|
||||||
|
for (var cred in n._def.credentials) {
|
||||||
|
if (n._def.credentials.hasOwnProperty(cred)) {
|
||||||
|
if (n.credentials[cred] != null) {
|
||||||
|
node.credentials[cred] = n.credentials[cred];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (n._def.category != "config") {
|
if (n._def.category != "config") {
|
||||||
node.x = n.x;
|
node.x = n.x;
|
||||||
|
@ -22,44 +22,6 @@ RED.editor = function() {
|
|||||||
return 'credentials/' + dashedType + "/" + nodeID;
|
return 'credentials/' + dashedType + "/" + nodeID;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assign the credentials to the node
|
|
||||||
* @param node - the node containing the credentials
|
|
||||||
* @param credDefinition - definition of the credentials
|
|
||||||
* @param prefix - prefix of the input fields
|
|
||||||
*/
|
|
||||||
function manageNodeCredentials(node, credDefinition, prefix) {
|
|
||||||
if(!node._creds) {
|
|
||||||
node._creds = {server: {}, send: {}, input: {}};
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var cred in credDefinition) {
|
|
||||||
var input = $("#" + prefix + '-' + cred);
|
|
||||||
var value = input.val();
|
|
||||||
if (credDefinition[cred].type == 'password') {
|
|
||||||
if(value != '') {
|
|
||||||
node._creds.input['has' + cred] = true;
|
|
||||||
if (value == '__PWRD__') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
node._creds.input['has' + cred] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (node._creds.server[cred] != value) {
|
|
||||||
node._creds.send[cred] = value;
|
|
||||||
} else {
|
|
||||||
delete node._creds.send[cred];
|
|
||||||
}
|
|
||||||
if (credDefinition[cred].type != 'password') {
|
|
||||||
node._creds.input[cred] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate a node
|
* Validate a node
|
||||||
* @param node - the node being validated
|
* @param node - the node being validated
|
||||||
@ -237,7 +199,8 @@ RED.editor = function() {
|
|||||||
if (editing_node._def.credentials) {
|
if (editing_node._def.credentials) {
|
||||||
var prefix = 'node-input';
|
var prefix = 'node-input';
|
||||||
var credDefinition = editing_node._def.credentials;
|
var credDefinition = editing_node._def.credentials;
|
||||||
manageNodeCredentials(editing_node,credDefinition,prefix);
|
var credsChanged = updateNodeCredentials(editing_node,credDefinition,prefix);
|
||||||
|
changed = changed || credsChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -381,8 +344,11 @@ RED.editor = function() {
|
|||||||
*/
|
*/
|
||||||
function populateCredentialsInputs(node, credDef, credData, prefix) {
|
function populateCredentialsInputs(node, credDef, credData, prefix) {
|
||||||
for (var cred in credDef) {
|
for (var cred in credDef) {
|
||||||
|
if (credDef.hasOwnProperty(cred)) {
|
||||||
if (credDef[cred].type == 'password') {
|
if (credDef[cred].type == 'password') {
|
||||||
if (credData['has' + cred]) {
|
if (credData[cred]) {
|
||||||
|
$('#' + prefix + '-' + cred).val(credData[cred]);
|
||||||
|
} else if (credData['has' + cred]) {
|
||||||
$('#' + prefix + '-' + cred).val('__PWRD__');
|
$('#' + prefix + '-' + cred).val('__PWRD__');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -392,12 +358,49 @@ RED.editor = function() {
|
|||||||
preparePropertyEditor(credData, cred, prefix);
|
preparePropertyEditor(credData, cred, prefix);
|
||||||
}
|
}
|
||||||
attachPropertyChangeHandler(node, credDef, cred, prefix);
|
attachPropertyChangeHandler(node, credDef, cred, prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
for (var cred in credDef) {
|
for (var cred in credDef) {
|
||||||
|
if (credDef.hasOwnProperty(cred)) {
|
||||||
$("#" + prefix + "-" + cred).change();
|
$("#" + prefix + "-" + cred).change();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the node credentials from the edit form
|
||||||
|
* @param node - the node containing the credentials
|
||||||
|
* @param credDefinition - definition of the credentials
|
||||||
|
* @param prefix - prefix of the input fields
|
||||||
|
* @return {boolean} whether anything has changed
|
||||||
|
*/
|
||||||
|
function updateNodeCredentials(node, credDefinition, prefix) {
|
||||||
|
var changed = false;
|
||||||
|
if(!node.credentials) {
|
||||||
|
node.credentials = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var cred in credDefinition) {
|
||||||
|
if (credDefinition.hasOwnProperty(cred)) {
|
||||||
|
var input = $("#" + prefix + '-' + cred);
|
||||||
|
var value = input.val();
|
||||||
|
if (credDefinition[cred].type == 'password') {
|
||||||
|
node.credentials['has' + cred] = (value != "");
|
||||||
|
if (value == '__PWRD__') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
changed = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
if (value != node.credentials._[cred]) {
|
||||||
|
node.credentials[cred] = value;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare all of the editor dialog fields
|
* Prepare all of the editor dialog fields
|
||||||
* @param node - the node being edited
|
* @param node - the node being edited
|
||||||
@ -413,18 +416,7 @@ RED.editor = function() {
|
|||||||
}
|
}
|
||||||
attachPropertyChangeHandler(node,definition.defaults,d,prefix);
|
attachPropertyChangeHandler(node,definition.defaults,d,prefix);
|
||||||
}
|
}
|
||||||
if (definition.credentials) {
|
var completePrepare = function() {
|
||||||
if (node._creds) {
|
|
||||||
populateCredentialsInputs(node, definition.credentials, node._creds.input, prefix);
|
|
||||||
} else {
|
|
||||||
node._creds = {server: {}, send: {}, input: {}};
|
|
||||||
$.getJSON(getCredentialsURL(node.type, node.id), function (data) {
|
|
||||||
node._creds.server = data;
|
|
||||||
node._creds.input = jQuery.extend({}, data);
|
|
||||||
populateCredentialsInputs(node, definition.credentials, node._creds.input, prefix);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (definition.oneditprepare) {
|
if (definition.oneditprepare) {
|
||||||
definition.oneditprepare.call(node);
|
definition.oneditprepare.call(node);
|
||||||
}
|
}
|
||||||
@ -433,6 +425,23 @@ RED.editor = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (definition.credentials) {
|
||||||
|
if (node.credentials) {
|
||||||
|
populateCredentialsInputs(node, definition.credentials, node.credentials, prefix);
|
||||||
|
completePrepare();
|
||||||
|
} else {
|
||||||
|
$.getJSON(getCredentialsURL(node.type, node.id), function (data) {
|
||||||
|
node.credentials = data;
|
||||||
|
node.credentials._ = $.extend(true,{},data);
|
||||||
|
populateCredentialsInputs(node, definition.credentials, node.credentials, prefix);
|
||||||
|
completePrepare();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
completePrepare();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function showEditDialog(node) {
|
function showEditDialog(node) {
|
||||||
editing_node = node;
|
editing_node = node;
|
||||||
RED.view.state(RED.state.EDITING);
|
RED.view.state(RED.state.EDITING);
|
||||||
@ -570,7 +579,7 @@ RED.editor = function() {
|
|||||||
updateConfigNodeSelect(configProperty,configType,configId);
|
updateConfigNodeSelect(configProperty,configType,configId);
|
||||||
}
|
}
|
||||||
if (configTypeDef.credentials) {
|
if (configTypeDef.credentials) {
|
||||||
manageNodeCredentials(configNode,configTypeDef.credentials,"node-config-input");
|
updateNodeCredentials(configNode,configTypeDef.credentials,"node-config-input");
|
||||||
}
|
}
|
||||||
if (configTypeDef.oneditsave) {
|
if (configTypeDef.oneditsave) {
|
||||||
configTypeDef.oneditsave.call(RED.nodes.node(configId));
|
configTypeDef.oneditsave.call(RED.nodes.node(configId));
|
||||||
|
@ -45,12 +45,11 @@ var parseCredentials = function (config) {
|
|||||||
for (var i in config) {
|
for (var i in config) {
|
||||||
if (config.hasOwnProperty(i)) {
|
if (config.hasOwnProperty(i)) {
|
||||||
var node = config[i];
|
var node = config[i];
|
||||||
if (!node._creds) {
|
if (node.credentials) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var type = node.type;
|
var type = node.type;
|
||||||
credentials.merge(node.id, type, node._creds);
|
credentials.merge(node.id, type, node.credentials);
|
||||||
delete node._creds;
|
delete node.credentials;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
credentials.save().then(function () {
|
credentials.save().then(function () {
|
||||||
|
Loading…
Reference in New Issue
Block a user