1
0
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:
Nick O'Leary 2014-07-19 00:16:21 +01:00
parent 760dd022dc
commit b604db83f6
4 changed files with 91 additions and 76 deletions

View File

@ -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

View File

@ -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;

View File

@ -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,23 +344,63 @@ 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[cred].type == 'password') { if (credDef.hasOwnProperty(cred)) {
if (credData['has' + cred]) { if (credDef[cred].type == 'password') {
$('#' + prefix + '-' + cred).val('__PWRD__'); if (credData[cred]) {
$('#' + prefix + '-' + cred).val(credData[cred]);
} else if (credData['has' + cred]) {
$('#' + prefix + '-' + cred).val('__PWRD__');
}
else {
$('#' + prefix + '-' + cred).val('');
}
} else {
preparePropertyEditor(credData, cred, prefix);
} }
else { attachPropertyChangeHandler(node, credDef, cred, prefix);
$('#' + prefix + '-' + cred).val('');
}
} else {
preparePropertyEditor(credData, 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,23 +416,29 @@ RED.editor = function() {
} }
attachPropertyChangeHandler(node,definition.defaults,d,prefix); attachPropertyChangeHandler(node,definition.defaults,d,prefix);
} }
if (definition.credentials) { var completePrepare = function() {
if (node._creds) { if (definition.oneditprepare) {
populateCredentialsInputs(node, definition.credentials, node._creds.input, prefix); definition.oneditprepare.call(node);
} else { }
node._creds = {server: {}, send: {}, input: {}}; for (var d in definition.defaults) {
$.getJSON(getCredentialsURL(node.type, node.id), function (data) { $("#"+prefix+"-"+d).change();
node._creds.server = data;
node._creds.input = jQuery.extend({}, data);
populateCredentialsInputs(node, definition.credentials, node._creds.input, prefix);
});
} }
} }
if (definition.oneditprepare) {
definition.oneditprepare.call(node); if (definition.credentials) {
} if (node.credentials) {
for (var d in definition.defaults) { populateCredentialsInputs(node, definition.credentials, node.credentials, prefix);
$("#"+prefix+"-"+d).change(); 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();
} }
} }
@ -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));

View File

@ -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;
credentials.merge(node.id, type, node.credentials);
delete node.credentials;
} }
var type = node.type;
credentials.merge(node.id, type, node._creds);
delete node._creds;
} }
} }
credentials.save().then(function () { credentials.save().then(function () {