Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 9194e26

Browse files
committed
Add update node.
1 parent d0a4f10 commit 9194e26

File tree

3 files changed

+160
-3
lines changed

3 files changed

+160
-3
lines changed

transports/ldap/index.html

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<script type="text/x-red" data-template-name="ldap-update in">
2+
<div class="form-row">
3+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
4+
<input type="text" id="node-input-name" placeholder="Name">
5+
</div>
6+
<div class="form-row">
7+
<label for="node-input-ldap"><i class="fa fa-user"></i> Add new LDAP Server</label>
8+
<input type="text" id="node-input-ldap">
9+
</div>
10+
<div class="form-row">
11+
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
12+
<select type="text" id="node-input-operation">
13+
<option value="replace">Replace</option>
14+
<option value="add">Add</option>
15+
<option value="delete">Delete</option>
16+
</select>
17+
</div>
18+
<div class="form-row">
19+
<label for="node-input-dn"> DN</label>
20+
<input type="text" id="node-input-dn" placeholder="CN=jim bob,OU=users,DC=corp,DC=com">
21+
</div>
22+
<div class="form-row">
23+
<label for="node-input-attribute"> Attribute</label>
24+
<input type="text" id="node-input-attribute" placeholder="givenName">
25+
</div>
26+
<div class="form-row">
27+
<label for="node-input-value"> Value</label>
28+
<input type="text" id="node-input-value" placeholder="tom">
29+
</div>
30+
</script>
31+
32+
<script type="text/javascript">
33+
RED.nodes.registerType('ldap-update in', {
34+
category: 'storage-input',
35+
color:"#AAAA66",
36+
defaults: {
37+
name: { value: '' },
38+
ldap: { type: 'ldap', required: true },
39+
operation: { value: 'list', required: true },
40+
dn: { value: '' },
41+
attribute: { value: '' },
42+
value: { value: '' }
43+
},
44+
inputs: 1,
45+
outputs: 1,
46+
icon: "db.png",
47+
label: function () {
48+
return this.name || 'ldap-update';
49+
},
50+
labelStyle: function () {
51+
return this.name ? 'node_label_italic' : '';
52+
},
53+
});
54+
</script>
55+
56+
57+
<script type="text/x-red" data-template-name="ldap">
58+
<div class="form-row">
59+
<label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
60+
<input type="text" id="node-config-input-host" placeholder="localhost" style="width: 40%;" />
61+
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
62+
<input type="text" id="node-config-input-port" placeholder="22" style="width:45px">
63+
</div>
64+
<div class="form-row">
65+
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
66+
<input type="text" id="node-config-input-username">
67+
</div>
68+
<div class="form-row">
69+
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
70+
<input type="password" id="node-config-input-password">
71+
</div>
72+
</script>
73+
74+
<script type="text/javascript">
75+
(function() {
76+
RED.nodes.registerType('ldap', {
77+
category: 'config',
78+
defaults: {
79+
host: { value: 'ldap://localhost', required: true },
80+
port: { value: '389', required: true, validate: RED.validators.number() },
81+
},
82+
credentials: {
83+
username: { type: "text" },
84+
password: { type: "password" }
85+
},
86+
exportable: false,
87+
label: function() {
88+
return this.host || ''
89+
},
90+
oneditsave: function() {
91+
let trimFields = [
92+
"username",
93+
"password"
94+
];
95+
// Just in case any whitespace has crept in with the copy-paste of the fields
96+
trimFields.forEach(function(field) {
97+
let v = $("#node-config-input-"+field).val();
98+
v = v.trim();
99+
$("#node-config-input-"+field).val(v);
100+
});
101+
}
102+
});
103+
})();
104+
</script>

transports/ldap/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,44 @@
11
module.exports = function (RED) {
22
'use strict';
33

4-
function ldapNode(n) {
4+
function ldapNode (n) {
55
RED.nodes.createNode(this, n);
6+
7+
this.options = {
8+
host: n.host || 'ldap://localhost',
9+
port: n.port || 389
10+
};
11+
}
12+
13+
function ldapUpdateNode (n) {
14+
RED.nodes.createNode(this, n);
15+
this.operation = n.operation;
16+
this.dn = n.dn;
17+
this.attribute = n.attribute;
18+
this.value = n.value;
19+
this.ldapConfig = RED.nodes.getNode(n.ldap);
20+
21+
let node = this;
22+
node.on('input', async function (msg) {
23+
node.operation = msg.operation || node.operation;
24+
node.dn = msg.dn || node.dn;
25+
node.attribute = msg.attribute || node.attribute;
26+
node.value = msg.payload || node.value;
27+
28+
let ldap = require('./ldap');
29+
node.status({ fill:"blue",shape:"dot",text: 'connecting' });
30+
try {
31+
await ldap.connect(node.ldapConfig.options.host, node.ldapConfig.credentials.username, node.ldapConfig.credentials.password);
32+
node.status({ fill: 'green', shape: 'dot', text: 'connected' });
33+
34+
await ldap.update(node.dn, node.operation, node.attribute, node.value);
35+
36+
node.status({});
37+
} catch (err) {
38+
node.status({ fill: 'red', shape: 'ring', text: 'failed' });
39+
node.error(err ? err.toString() : 'Unknown error' );
40+
}
41+
});
642
}
743

844
RED.nodes.registerType('ldap', ldapNode, {
@@ -11,4 +47,5 @@ module.exports = function (RED) {
1147
password: { type: "password" }
1248
}
1349
});
50+
RED.nodes.registerType('ldap-update in', ldapUpdateNode);
1451
};

transports/ldap/ldap.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
const ldap = require('ldapjs');
22
module.exports = {
33
client: undefined,
4-
connect: async function(url, username, password, options) {
4+
/**
5+
* Connect to LDAP server
6+
* @param {string} url URL of LDAP server
7+
* @param {string} username UPN or dn of user to bind to instance with
8+
* @param {string} password Password of user
9+
* @param {object} options additional options for ldapjs connection
10+
* @returns {Promise<*>}
11+
*/
12+
connect: async function(url, username, password, options = {}) {
513
let that = this;
614
this.client = ldap.createClient({
715
url,
@@ -14,7 +22,15 @@ module.exports = {
1422
});
1523
});
1624
},
17-
update: async function (dn, operation, attribute, value) {
25+
/**
26+
* Perform an update action on a specific LDAP object
27+
* @param {string} dn DN of the object
28+
* @param {string} operation Operation type to perform
29+
* @param {string} attribute Attribute to change
30+
* @param {null|string} value Value of the change
31+
* @returns {Promise<*>}
32+
*/
33+
update: async function (dn, operation, attribute, value = null) {
1834
let that = this;
1935
let changeObj = {
2036
operation,

0 commit comments

Comments
 (0)