From 497f08bba93a001872ffa01907fac99d3adbccfb Mon Sep 17 00:00:00 2001 From: Kris Daniels Date: Sat, 21 Dec 2013 11:01:19 +0100 Subject: [PATCH] moved credtials from config to credential store --- storage/postgres/110-postgres.html | 47 ++++++++++++++++++++++++---- storage/postgres/110-postgres.js | 50 ++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/storage/postgres/110-postgres.html b/storage/postgres/110-postgres.html index 033cc990..a4ea5e57 100644 --- a/storage/postgres/110-postgres.html +++ b/storage/postgres/110-postgres.html @@ -27,7 +27,7 @@
- +
@@ -40,12 +40,46 @@ defaults: { hostname: { value:"localhost",required:true}, port: { value: 5432,required:true}, - db: { value:"postgres",required:true}, - username: { value:"postgres", required:true }, - password: { value:"postgres", required:true } + db: { value:"postgres",required:true} }, label: function() { 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) {} + }); } }); @@ -98,12 +132,13 @@ $( "#node-input-output" ).prop( "checked", this.output ); $("#node-input-name").focus(); - + }, oneditsave: function() { + var hasOutput = $( "#node-input-output" ).prop( "checked" ); this.outputs = hasOutput ? 1: 0; - delete this.editor; + } }); diff --git a/storage/postgres/110-postgres.js b/storage/postgres/110-postgres.js index edd42c88..4e2d41ee 100644 --- a/storage/postgres/110-postgres.js +++ b/storage/postgres/110-postgres.js @@ -17,14 +17,57 @@ var RED = require(process.env.NODE_RED_HOME+"/red/red"); var pg=require('pg'); 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) { RED.nodes.createNode(this,n); this.hostname = n.hostname; this.port = n.port; 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); @@ -39,10 +82,11 @@ function PostgresNode(n) { this.output = n.output; var node = this; + 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); named.patch(node.clientdb);