From f8228274548c5097cdd256d41cced4d93052ee9a Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Fri, 16 May 2014 09:15:24 +0300 Subject: [PATCH 1/2] Credentials into RED --- red/nodes/index.js | 1 - red/red.js | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/red/nodes/index.js b/red/nodes/index.js index 8eb31a984..1960f5b70 100644 --- a/red/nodes/index.js +++ b/red/nodes/index.js @@ -36,7 +36,6 @@ module.exports = { addCredentials: credentials.add, getCredentials: credentials.get, deleteCredentials: credentials.delete, - registerCredentials: credentials.register, createNode: createNode, registerType: registry.registerType, getType: registry.get, diff --git a/red/red.js b/red/red.js index 6f3f3dd62..266965ed0 100644 --- a/red/red.js +++ b/red/red.js @@ -22,6 +22,7 @@ var comms = require("./comms"); var log = require("./log"); var fs = require("fs"); var settings = null; +var credentials = require("./nodes/credentials"); var path = require('path'); @@ -51,6 +52,7 @@ var RED = { stop: server.stop, nodes: nodes, library: library, + credentials: credentials, events: events, log: log, comms:comms From 364f44451f9ab65f6a9bc182c9b7fb837d3e38e1 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Fri, 16 May 2014 10:34:23 +0300 Subject: [PATCH 2/2] Validation of credentials inputs. --- public/red/ui/editor.js | 50 ++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/public/red/ui/editor.js b/public/red/ui/editor.js index c4ea36dc2..28ca00c79 100644 --- a/public/red/ui/editor.js +++ b/public/red/ui/editor.js @@ -15,18 +15,24 @@ **/ RED.editor = function() { var editing_node = null; - // 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) { var oldValue = node.valid; - node.valid = true; - for (var d in node._def.defaults) { - if (!validateNodeProperty(node,d,node[d])) { - node.valid = false; - } + node.valid = validateNodeProperties(node, node._def.defaults, node); + if (node._def._creds) { + node.valid = node.valid && validateNodeProperties(node, node._def.credentials, node._def._creds); } - if (node.valid != oldValue) { + if (oldValue != node.valid) { node.dirty = true; } } @@ -35,6 +41,7 @@ RED.editor = function() { var dashedType = nodeType.replace(/\s+/g, '-'); return 'credentials/' + dashedType + "/" + nodeID; } + function sendCredentials(node,credDefinition,prefix) { var credentials = {}; for (var cred in credDefinition) { @@ -45,6 +52,7 @@ RED.editor = function() { } credentials[cred] = value; } + node._def._creds = credentials; $.ajax({ url: getCredentialsURL(node.type, node.id), type: 'POST', @@ -52,17 +60,18 @@ RED.editor = function() { success: function (result) { } }); + } - function validateNodeProperty(node,property,value) { + function validateNodeProperty(node,definition,property,value) { 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 !== ""; } - if (valid && "validate" in node._def.defaults[property]) { - valid = node._def.defaults[property].validate.call(node,value); + if (valid && "validate" in definition[property]) { + 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_") { valid = false; } else { @@ -304,12 +313,13 @@ RED.editor = function() { /** * Add an on-change handler to revalidate a node field * @param node - the node being edited + * @param definition - the definition of the node * @param property - the name of the field * @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() { - if (!validateNodeProperty(node, property,this.value)) { + if (!validateNodeProperty(node, definition, property,this.value)) { $(this).addClass("input-error"); } else { $(this).removeClass("input-error"); @@ -330,10 +340,9 @@ RED.editor = function() { } else { preparePropertyEditor(node,d,prefix); } - attachPropertyChangeHandler(node,d,prefix); + attachPropertyChangeHandler(node,definition.defaults,d,prefix); } if (definition.credentials) { - // TODO: Validate credentials fields $.getJSON(getCredentialsURL(node.type, node.id), function (data) { for (var cred in definition.credentials) { @@ -347,6 +356,10 @@ RED.editor = function() { } else { 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)); } validateNode(configNode); - RED.view.dirty(true); - $( this ).dialog( "close" ); + RED.view.dirty(true); + $(this).dialog("close"); + } }, {