Update mysql to use credentials system

Fixes #86
This commit is contained in:
Nick O'Leary 2015-01-19 09:46:29 +00:00
parent fb7ccc7736
commit b7a1193d32
2 changed files with 22 additions and 100 deletions

View File

@ -17,27 +17,27 @@
<script type="text/x-red" data-template-name="MySQLdatabase">
<div class="form-row">
<label for="node-config-input-host"><i class="fa fa-globe"></i> Host</label>
<input type="text" id="node-config-input-host" placeholder="localhost">
<input type="text" id="node-config-input-host">
</div>
<div class="form-row">
<label for="node-config-input-port"><i class="fa fa-random"></i> Port</label>
<input type="text" id="node-config-input-port" placeholder="3306">
<input type="text" id="node-config-input-port">
</div>
<div class="form-row">
<label for="node-config-input-user"><i class="fa fa-user"></i> User</label>
<input type="text" id="node-config-input-user" placeholder="JoeDemo">
<input type="text" id="node-config-input-user">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-pass" placeholder="mySecret">
<input type="password" id="node-config-input-password">
</div>
<div class="form-row">
<label for="node-config-input-db"><i class="fa fa-database"></i> Database</label>
<input type="text" id="node-config-input-db" placeholder="myDatabase">
<input type="text" id="node-config-input-db">
</div>
<div class="form-row">
<label for="node-config-input-tz"><i class="fa fa-clock-o"></i> Timezone</label>
<input type="text" id="node-config-input-tz" placeholder="local">
<input type="text" id="node-config-input-tz">
</div>
</script>
@ -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) {}
});
}
});
</script>

View File

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