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

Merge pull request #228 from Belphemur/credentials

Credentials Validation
This commit is contained in:
Nick O'Leary 2014-05-27 10:53:10 +01:00
commit d8df592ea6
3 changed files with 34 additions and 19 deletions

View File

@ -15,18 +15,24 @@
**/ **/
RED.editor = function() { RED.editor = function() {
var editing_node = null; var editing_node = null;
// TODO: should IMPORT/EXPORT get their own dialogs? // TODO: should IMPORT/EXPORT get their own dialogs?
function validateNodeProperties(node, definition, properties) {
var isValid = true;
for (var prop in definition) {
if (!validateNodeProperty(node, definition, prop, properties[prop])) {
isValid = false;
}
}
return isValid;
}
function validateNode(node) { function validateNode(node) {
var oldValue = node.valid; var oldValue = node.valid;
node.valid = true; node.valid = validateNodeProperties(node, node._def.defaults, node);
for (var d in node._def.defaults) { if (node._def._creds) {
if (!validateNodeProperty(node,d,node[d])) { node.valid = node.valid && validateNodeProperties(node, node._def.credentials, node._def._creds);
node.valid = false;
} }
} if (oldValue != node.valid) {
if (node.valid != oldValue) {
node.dirty = true; node.dirty = true;
} }
} }
@ -35,6 +41,7 @@ RED.editor = function() {
var dashedType = nodeType.replace(/\s+/g, '-'); var dashedType = nodeType.replace(/\s+/g, '-');
return 'credentials/' + dashedType + "/" + nodeID; return 'credentials/' + dashedType + "/" + nodeID;
} }
function sendCredentials(node,credDefinition,prefix) { function sendCredentials(node,credDefinition,prefix) {
var credentials = {}; var credentials = {};
for (var cred in credDefinition) { for (var cred in credDefinition) {
@ -45,6 +52,7 @@ RED.editor = function() {
} }
credentials[cred] = value; credentials[cred] = value;
} }
node._def._creds = credentials;
$.ajax({ $.ajax({
url: getCredentialsURL(node.type, node.id), url: getCredentialsURL(node.type, node.id),
type: 'POST', type: 'POST',
@ -52,17 +60,18 @@ RED.editor = function() {
success: function (result) { success: function (result) {
} }
}); });
} }
function validateNodeProperty(node,property,value) { function validateNodeProperty(node,definition,property,value) {
var valid = true; var valid = true;
if ("required" in node._def.defaults[property] && node._def.defaults[property].required) { if ("required" in definition[property] && definition[property].required) {
valid = value !== ""; valid = value !== "";
} }
if (valid && "validate" in node._def.defaults[property]) { if (valid && "validate" in definition[property]) {
valid = node._def.defaults[property].validate.call(node,value); valid = definition[property].validate.call(node,value);
} }
if (valid && node._def.defaults[property].type && RED.nodes.getType(node._def.defaults[property].type) && !("validate" in node._def.defaults[property])) { if (valid && definition[property].type && RED.nodes.getType(definition[property].type) && !("validate" in definition[property])) {
if (!value || value == "_ADD_") { if (!value || value == "_ADD_") {
valid = false; valid = false;
} else { } else {
@ -304,12 +313,13 @@ RED.editor = function() {
/** /**
* Add an on-change handler to revalidate a node field * Add an on-change handler to revalidate a node field
* @param node - the node being edited * @param node - the node being edited
* @param definition - the definition of the node
* @param property - the name of the field * @param property - the name of the field
* @param prefix - the prefix to use in the input element ids (node-input|node-config-input) * @param prefix - the prefix to use in the input element ids (node-input|node-config-input)
*/ */
function attachPropertyChangeHandler(node,property,prefix) { function attachPropertyChangeHandler(node,definition,property,prefix) {
$("#"+prefix+"-"+property).change(function() { $("#"+prefix+"-"+property).change(function() {
if (!validateNodeProperty(node, property,this.value)) { if (!validateNodeProperty(node, definition, property,this.value)) {
$(this).addClass("input-error"); $(this).addClass("input-error");
} else { } else {
$(this).removeClass("input-error"); $(this).removeClass("input-error");
@ -330,10 +340,9 @@ RED.editor = function() {
} else { } else {
preparePropertyEditor(node,d,prefix); preparePropertyEditor(node,d,prefix);
} }
attachPropertyChangeHandler(node,d,prefix); attachPropertyChangeHandler(node,definition.defaults,d,prefix);
} }
if (definition.credentials) { if (definition.credentials) {
// TODO: Validate credentials fields
$.getJSON(getCredentialsURL(node.type, node.id), function (data) { $.getJSON(getCredentialsURL(node.type, node.id), function (data) {
for (var cred in definition.credentials) { for (var cred in definition.credentials) {
@ -347,6 +356,10 @@ RED.editor = function() {
} else { } else {
preparePropertyEditor(data, cred, prefix); preparePropertyEditor(data, cred, prefix);
} }
attachPropertyChangeHandler(node, definition.credentials, cred, prefix);
for (var cred in definition.credentials) {
$("#" + prefix + "-" + cred).change();
}
} }
}); });
} }
@ -509,9 +522,10 @@ RED.editor = function() {
configTypeDef.oneditsave.call(RED.nodes.node(configId)); configTypeDef.oneditsave.call(RED.nodes.node(configId));
} }
validateNode(configNode); validateNode(configNode);
RED.view.dirty(true);
RED.view.dirty(true);
$(this).dialog("close"); $(this).dialog("close");
} }
}, },
{ {

View File

@ -36,7 +36,6 @@ module.exports = {
addCredentials: credentials.add, addCredentials: credentials.add,
getCredentials: credentials.get, getCredentials: credentials.get,
deleteCredentials: credentials.delete, deleteCredentials: credentials.delete,
registerCredentials: credentials.register,
createNode: createNode, createNode: createNode,
registerType: registry.registerType, registerType: registry.registerType,
getType: registry.get, getType: registry.get,

View File

@ -22,6 +22,7 @@ var comms = require("./comms");
var log = require("./log"); var log = require("./log");
var fs = require("fs"); var fs = require("fs");
var settings = null; var settings = null;
var credentials = require("./nodes/credentials");
var path = require('path'); var path = require('path');
@ -51,6 +52,7 @@ var RED = {
stop: server.stop, stop: server.stop,
nodes: nodes, nodes: nodes,
library: library, library: library,
credentials: credentials,
events: events, events: events,
log: log, log: log,
comms:comms comms:comms