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

moved credtials from config to credential store

This commit is contained in:
Kris Daniels 2013-12-21 11:01:19 +01:00
parent 43c6bdb95a
commit 497f08bba9
2 changed files with 88 additions and 9 deletions

View File

@ -27,7 +27,7 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-name"><i class="icon-user"></i> Username</label> <label for="node-config-input-name"><i class="icon-user"></i> Username</label>
<input type="text" id="node-config-input-username" placeholder="postgres"> <input type="text" id="node-config-input-user" placeholder="postgres">
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label> <label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
<input type="password" id="node-config-input-password" placeholder="postgres"> <input type="password" id="node-config-input-password" placeholder="postgres">
</div> </div>
@ -40,12 +40,46 @@
defaults: { defaults: {
hostname: { value:"localhost",required:true}, hostname: { value:"localhost",required:true},
port: { value: 5432,required:true}, port: { value: 5432,required:true},
db: { value:"postgres",required:true}, db: { value:"postgres",required:true}
username: { value:"postgres", required:true },
password: { value:"postgres", required:true }
}, },
label: function() { label: function() {
return this.name||this.hostname+":"+this.port+"/"+this.db; return this.name||this.hostname+":"+this.port+"/"+this.db;
},
oneditprepare: function() {
$.getJSON('postgresdb/'+this.id,function(data) {
if (data.user) {
$('#node-config-input-user').val(data.user);
}
if (data.hasPassword) {
$('#node-config-input-password').val('__PWRD__');
} else {
$('#node-config-input-password').val('');
}
});
},
oneditsave: function() {
var newUser = $('#node-config-input-user').val();
var newPass = $('#node-config-input-password').val();
var credentials = {};
credentials.user = newUser;
if (newPass != '__PWRD__') {
credentials.password = newPass;
}
$.ajax({
url: 'postgresdb/'+this.id,
type: 'POST',
data: credentials,
success:function(result){}
});
},
ondelete: function() {
$.ajax({
url: 'postgresdb/'+this.id,
type: 'DELETE',
success: function(result) {}
});
} }
}); });
</script> </script>
@ -98,12 +132,13 @@
$( "#node-input-output" ).prop( "checked", this.output ); $( "#node-input-output" ).prop( "checked", this.output );
$("#node-input-name").focus(); $("#node-input-name").focus();
}, },
oneditsave: function() { oneditsave: function() {
var hasOutput = $( "#node-input-output" ).prop( "checked" ); var hasOutput = $( "#node-input-output" ).prop( "checked" );
this.outputs = hasOutput ? 1: 0; this.outputs = hasOutput ? 1: 0;
delete this.editor;
} }
}); });
</script> </script>

View File

@ -17,14 +17,57 @@
var RED = require(process.env.NODE_RED_HOME+"/red/red"); var RED = require(process.env.NODE_RED_HOME+"/red/red");
var pg=require('pg'); var pg=require('pg');
var named=require('node-postgres-named'); var named=require('node-postgres-named');
var querystring = require('querystring');
RED.app.get('/postgresdb/: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.app.delete('/postgresdb/:id',function(req,res) {
RED.nodes.deleteCredentials(req.params.id);
res.send(200);
});
RED.app.post('/postgresdb/: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 PostgresDatabaseNode(n) { function PostgresDatabaseNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.hostname = n.hostname; this.hostname = n.hostname;
this.port = n.port; this.port = n.port;
this.db = n.db; this.db = n.db;
this.username = n.username;
this.password = n.password; var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.user = credentials.user;
this.password = credentials.password;
}
} }
RED.nodes.registerType("postgresdb",PostgresDatabaseNode); RED.nodes.registerType("postgresdb",PostgresDatabaseNode);
@ -39,10 +82,11 @@ function PostgresNode(n) {
this.output = n.output; this.output = n.output;
var node = this; var node = this;
if(this.postgresConfig) if(this.postgresConfig)
{ {
var conString = 'postgres://'+this.postgresConfig.username +':' + this.postgresConfig.password + '@' + this.postgresConfig.hostname + ':' + this.postgresConfig.port + '/' + this.postgresConfig.db; var conString = 'postgres://'+this.postgresConfig.user +':' + this.postgresConfig.password + '@' + this.postgresConfig.hostname + ':' + this.postgresConfig.port + '/' + this.postgresConfig.db;
node.clientdb = new pg.Client(conString); node.clientdb = new pg.Client(conString);
named.patch(node.clientdb); named.patch(node.clientdb);