From c379f1b1975d9f128db48650d29435738edbf707 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 18 Jul 2014 14:31:28 +0100 Subject: [PATCH] Migrate mongo node to new credential api --- nodes/core/storage/66-mongodb.html | 45 ++++--------------------- nodes/core/storage/66-mongodb.js | 54 ++++-------------------------- 2 files changed, 13 insertions(+), 86 deletions(-) diff --git a/nodes/core/storage/66-mongodb.html b/nodes/core/storage/66-mongodb.html index d0ee96a5a..3dbc4925c 100644 --- a/nodes/core/storage/66-mongodb.html +++ b/nodes/core/storage/66-mongodb.html @@ -30,8 +30,8 @@
- - + +
@@ -48,46 +48,13 @@ port: { value: 27017,required:true}, db: { value:"",required:true}, name: { value:"" } - //user -> credentials - //pass -> credentials + }, + credentials: { + user: {type:"text"}, + password: {type: "password"} }, label: function() { return this.name||this.hostname+":"+this.port+"/"+this.db; - }, - oneditprepare: function() { - $.getJSON('mongodb/'+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: 'mongodb/'+this.id, - type: 'POST', - data: credentials, - success:function(result){} - }); - }, - ondelete: function() { - $.ajax({ - url: 'mongodb/'+this.id, - type: 'DELETE', - success: function(result) {} - }); } }); diff --git a/nodes/core/storage/66-mongodb.js b/nodes/core/storage/66-mongodb.js index 6ae14b6e8..ee14b90e7 100644 --- a/nodes/core/storage/66-mongodb.js +++ b/nodes/core/storage/66-mongodb.js @@ -25,63 +25,23 @@ module.exports = function(RED) { this.port = n.port; this.db = n.db; this.name = n.name; - var credentials = RED.nodes.getCredentials(n.id); - if (credentials) { - this.username = credentials.user; - this.password = credentials.password; - } var url = "mongodb://"; - if (this.username && this.password) { - url += this.username+":"+this.password+"@"; + if (this.credentials && this.credentials.username && this.credentials.password) { + url += this.credentials.username+":"+this.credentials.password+"@"; } url += this.hostname+":"+this.port+"/"+this.db; this.url = url; } - RED.nodes.registerType("mongodb",MongoNode); - - var querystring = require('querystring'); - - RED.httpAdmin.get('/mongodb/: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.nodes.registerType("mongodb",MongoNode,{ + credentials: { + user: {type:"text"}, + password: {type: "password"} } }); - - RED.httpAdmin.delete('/mongodb/:id',function(req,res) { - RED.nodes.deleteCredentials(req.params.id); - res.send(200); - }); - - RED.httpAdmin.post('/mongodb/: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 MongoOutNode(n) { RED.nodes.createNode(this,n); this.collection = n.collection;