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

Migrate mongo node to new credential api

This commit is contained in:
Nick O'Leary 2014-07-18 14:31:28 +01:00
parent 4770a06679
commit c379f1b197
2 changed files with 13 additions and 86 deletions

View File

@ -30,8 +30,8 @@
<input type="text" id="node-config-input-user"> <input type="text" id="node-config-input-user">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label> <label for="node-config-input-password"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-pass"> <input type="password" id="node-config-input-password">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
@ -48,46 +48,13 @@
port: { value: 27017,required:true}, port: { value: 27017,required:true},
db: { value:"",required:true}, db: { value:"",required:true},
name: { value:"" } name: { value:"" }
//user -> credentials },
//pass -> credentials credentials: {
user: {type:"text"},
password: {type: "password"}
}, },
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('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) {}
});
} }
}); });
</script> </script>

View File

@ -25,63 +25,23 @@ module.exports = function(RED) {
this.port = n.port; this.port = n.port;
this.db = n.db; this.db = n.db;
this.name = n.name; this.name = n.name;
var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
}
var url = "mongodb://"; var url = "mongodb://";
if (this.username && this.password) { if (this.credentials && this.credentials.username && this.credentials.password) {
url += this.username+":"+this.password+"@"; url += this.credentials.username+":"+this.credentials.password+"@";
} }
url += this.hostname+":"+this.port+"/"+this.db; url += this.hostname+":"+this.port+"/"+this.db;
this.url = url; this.url = url;
} }
RED.nodes.registerType("mongodb",MongoNode); RED.nodes.registerType("mongodb",MongoNode,{
credentials: {
var querystring = require('querystring'); user: {type:"text"},
password: {type: "password"}
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.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) { function MongoOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.collection = n.collection; this.collection = n.collection;