From b7a1193d325a180464e6eeabe151940310877df0 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 19 Jan 2015 09:46:29 +0000 Subject: [PATCH] Update mysql to use credentials system Fixes #86 --- storage/mysql/68-mysql.html | 52 ++++++--------------------- storage/mysql/68-mysql.js | 70 +++++++------------------------------ 2 files changed, 22 insertions(+), 100 deletions(-) diff --git a/storage/mysql/68-mysql.html b/storage/mysql/68-mysql.html index aa66dc57..7e50d409 100644 --- a/storage/mysql/68-mysql.html +++ b/storage/mysql/68-mysql.html @@ -17,27 +17,27 @@ @@ -52,45 +52,13 @@ db: {value:"",required:true}, tz: {value:""} }, + credentials: { + user: {type: "text"}, + password: {type: "password"} + }, label: function() { return this.db; - }, - oneditprepare: function() { - $.getJSON('MySQLdatabase/'+this.id,function(data) { - if (data.user) { - $('#node-config-input-user').val(data.user); - } - if (data.hasPassword) { - $('#node-config-input-pass').val('__PWRD__'); - } else { - $('#node-config-input-pass').val(''); - } - - }); - }, - oneditsave: function() { - var newUser = $('#node-config-input-user').val(); - var newPass = $('#node-config-input-pass').val(); - var credentials = {}; - credentials.user = newUser; - if (newPass != '__PWRD__') { - credentials.password = newPass; - } - $.ajax({ - url: 'MySQLdatabase/'+this.id, - type: 'POST', - data: credentials, - success:function(result){} - }); - }, - ondelete: function() { - $.ajax({ - url: 'MySQLdatabase/'+this.id, - type: 'DELETE', - success: function(result) {} - }); } - }); diff --git a/storage/mysql/68-mysql.js b/storage/mysql/68-mysql.js index c4f889ed..1c090a1f 100644 --- a/storage/mysql/68-mysql.js +++ b/storage/mysql/68-mysql.js @@ -18,45 +18,6 @@ module.exports = function(RED) { "use strict"; var reconnect = RED.settings.mysqlReconnectTime || 30000; var mysqldb = require('mysql'); - var querystring = require('querystring'); - - RED.httpAdmin.get('/MySQLdatabase/:id',function(req,res) { - var credentials = RED.nodes.getCredentials(req.params.id); - if (credentials) { - res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!=="")})); - } else { - res.send(JSON.stringify({})); - } - }); - - RED.httpAdmin.delete('/MySQLdatabase/:id',function(req,res) { - RED.nodes.deleteCredentials(req.params.id); - res.send(200); - }); - - RED.httpAdmin.post('/MySQLdatabase/:id',function(req,res) { - var body = ""; - req.on('data', function(chunk) { - body+=chunk; - }); - req.on('end', function(){ - var newCreds = querystring.parse(body); - var credentials = RED.nodes.getCredentials(req.params.id)||{}; - if (newCreds.user == null || newCreds.user === "") { - delete credentials.user; - } else { - credentials.user = newCreds.user; - } - if (newCreds.password === "") { - delete credentials.password; - } else { - credentials.password = newCreds.password||credentials.password; - } - RED.nodes.addCredentials(req.params.id,credentials); - res.send(200); - }); - }); - function MySQLNode(n) { RED.nodes.createNode(this,n); @@ -67,21 +28,6 @@ module.exports = function(RED) { this.connected = false; this.connecting = false; - var credentials = {}; - if (n.user) { - credentials.user = n.user; - credentials.password = n.pass; - RED.nodes.addCredentials(n.id,credentials); - this.user = n.user; - this.password = n.pass; - } else { - credentials = RED.nodes.getCredentials(n.id); - if (credentials) { - this.user = credentials.user; - this.password = credentials.password; - } - } - this.dbname = n.db; var node = this; @@ -90,8 +36,8 @@ module.exports = function(RED) { node.connection = mysqldb.createConnection({ host : node.host, port : node.port, - user : node.user, - password : node.password, + user : node.credentials.user, + password : node.credentials.password, database : node.dbname, timezone : node.tz, insecureAuth: true @@ -124,16 +70,24 @@ module.exports = function(RED) { } } - this.on('close', function () { + this.on('close', function (done) { if (this.tick) { clearTimeout(this.tick); } if (this.connection) { node.connection.end(function(err) { if (err) { node.error(err); } + done(); }); + } else { + done(); } }); } - RED.nodes.registerType("MySQLdatabase",MySQLNode); + RED.nodes.registerType("MySQLdatabase",MySQLNode, { + credentials: { + user: {type: "text"}, + password: {type: "password"} + } + }); function MysqlDBNodeIn(n) {