mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Migrate MQTT nodes to new credentials api
This commit is contained in:
parent
c379f1b197
commit
760dd022dc
@ -115,8 +115,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="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-pass">
|
<input type="password" id="node-config-input-password">
|
||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -127,48 +127,14 @@
|
|||||||
broker: {value:"",required:true},
|
broker: {value:"",required:true},
|
||||||
port: {value:1883,required:true,validate:RED.validators.number()},
|
port: {value:1883,required:true,validate:RED.validators.number()},
|
||||||
clientid: { value:"" }
|
clientid: { value:"" }
|
||||||
//user -> credentials
|
},
|
||||||
//pass -> credentials
|
credentials: {
|
||||||
|
user: {type:"text"},
|
||||||
|
password: {type: "password"}
|
||||||
},
|
},
|
||||||
label: function() {
|
label: function() {
|
||||||
if (this.broker == "") { this.broker = "localhost"; }
|
if (this.broker == "") { this.broker = "localhost"; }
|
||||||
return (this.clientid?this.clientid+"@":"")+this.broker+":"+this.port;
|
return (this.clientid?this.clientid+"@":"")+this.broker+":"+this.port;
|
||||||
},
|
|
||||||
oneditprepare: function() {
|
|
||||||
$.getJSON('mqtt-broker/'+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() {
|
|
||||||
if (this.broker == "") { this.broker = "localhost"; }
|
|
||||||
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: 'mqtt-broker/'+this.id,
|
|
||||||
type: 'POST',
|
|
||||||
data: credentials,
|
|
||||||
success:function(result){}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
ondelete: function() {
|
|
||||||
$.ajax({
|
|
||||||
url: 'mqtt-broker/'+this.id,
|
|
||||||
type: 'DELETE',
|
|
||||||
success: function(result) {}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -24,54 +24,18 @@ module.exports = function(RED) {
|
|||||||
this.broker = n.broker;
|
this.broker = n.broker;
|
||||||
this.port = n.port;
|
this.port = n.port;
|
||||||
this.clientid = n.clientid;
|
this.clientid = n.clientid;
|
||||||
var credentials = RED.nodes.getCredentials(n.id);
|
if (this.credentials) {
|
||||||
if (credentials) {
|
this.username = this.credentials.user;
|
||||||
this.username = credentials.user;
|
this.password = this.credentials.password;
|
||||||
this.password = credentials.password;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode);
|
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode,{
|
||||||
|
credentials: {
|
||||||
var querystring = require('querystring');
|
user: {type:"text"},
|
||||||
|
password: {type: "password"}
|
||||||
RED.httpAdmin.get('/mqtt-broker/: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('/mqtt-broker/:id',function(req,res) {
|
|
||||||
RED.nodes.deleteCredentials(req.params.id);
|
|
||||||
res.send(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
RED.httpAdmin.post('/mqtt-broker/: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 MQTTInNode(n) {
|
function MQTTInNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.topic = n.topic;
|
this.topic = n.topic;
|
||||||
|
Loading…
Reference in New Issue
Block a user