Added device group move to MeshCtrl.
Ylian Saint-Hilaire committed
Apr 16, 2020 at 08:46 UTC
8da5c11b42d473f4022c7ed55f2ad6c1c7ef3284
3 files changed
+75
-9
meshctrl.js
+24
-1
@@ -7,7 +7,7 @@ try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm
7
var settings = {};
8
const crypto = require('crypto');
9
const args = require('minimist')(process.argv.slice(2));
10
-const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config'];
10
+const possibleCommands = ['listusers', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup'];
11
if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { console.log('Missing module "https-proxy-agent", type "npm install https-proxy-agent" to install it.'); return; } }
12
13
if (args['_'].length == 0) {
@@ -27,6 +27,7 @@ if (args['_'].length == 0) {
27
console.log(" RemoveUser - Delete a user account.");
28
console.log(" AddDeviceGroup - Create a new device group.");
29
console.log(" RemoveDeviceGroup - Delete a device group.");
30
+ console.log(" MoveToDeviceGroup - Move a device to a different device group.");
31
console.log(" AddUserToDeviceGroup - Add a user to a device group.");
32
console.log(" RemoveUserFromDeviceGroup - Remove a user from a device group.");
33
console.log(" AddUserToDevice - Add a user to a device.");
@@ -97,6 +98,12 @@ if (args['_'].length == 0) {
98
else { ok = true; }
99
break;
100
}
101
+ case 'movetodevicegroup': {
102
+ if ((args.id == null) && (args.group == null)) { console.log("Device group identifier missing, use --id [groupid] or --group [groupname]"); }
103
+ else if (args.devid == null) { console.log("Device identifier missing, use --devid [deviceid]"); }
104
+ else { ok = true; }
105
+ break;
106
+ }
107
case 'broadcast': {
108
if (args.msg == null) { console.log("Message missing, use --msg [message]"); }
109
else { ok = true; }
@@ -266,6 +273,15 @@ if (args['_'].length == 0) {
273
console.log(" --group [groupname] - Device group name (or --id).");
274
break;
275
}
276
+ case 'movetodevicegroup': {
277
+ console.log("Move a device to a new device group, Example usages:\r\n");
278
+ console.log(" MeshCtrl MoveToDeviceGroup --devid deviceid --id groupid");
279
+ console.log("\r\nRequired arguments:\r\n");
280
+ console.log(" --id [groupid] - Device group identifier (or --group).");
281
+ console.log(" --group [groupname] - Device group name (or --id).");
282
+ console.log(" --devid [deviceid] - Device identifier.");
283
+ break;
284
+ }
285
case 'addusertodevicegroup': {
286
console.log("Add a user to a device group, Example usages:\r\n");
287
console.log(" MeshCtrl AddUserToDeviceGroup --id groupid --userid userid --fullrights");
@@ -591,6 +607,12 @@ function serverConnect() {
607
ws.send(JSON.stringify(op));
608
break;
609
}
610
+ case 'movetodevicegroup': {
611
+ var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [ args.devid ] };
612
+ if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
613
+ ws.send(JSON.stringify(op));
614
+ break;
615
+ }
616
case 'addusertodevicegroup': {
617
var meshrights = 0;
618
if (args.fullrights) { meshrights = 0xFFFFFFFF; }
@@ -716,6 +738,7 @@ function serverConnect() {
738
case 'deleteuser': // REMOVEUSER
739
case 'createmesh': // ADDDEVICEGROUP
740
case 'deletemesh': // REMOVEDEVICEGROUP
741
+ case 'changeDeviceMesh':
742
case 'addmeshuser': //
743
case 'removemeshuser': //
744
case 'inviteAgent': //
meshuser.js
+49
-6
@@ -2817,22 +2817,62 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2817
}
2818
case 'changeDeviceMesh':
2819
{
2820
- if (common.validateStrArray(command.nodeids, 1, 256) == false) break; // Check nodeid strings
2821
- if (common.validateString(command.meshid, 1, 256) == false) break; // Check target meshid string
2820
+ var err = null;
2821
+
2822
+ // Resolve the device group name if needed
2823
+ if ((typeof command.meshname == 'string') && (command.meshid == null)) {
2824
+ for (var i in parent.meshes) {
2825
+ var m = parent.meshes[i];
2826
+ if ((m.mtype == 2) && (m.name == command.meshname) && parent.IsMeshViewable(user, m)) {
2827
+ if (command.meshid == null) { command.meshid = m._id; } else { err = 'Duplicate device groups found'; }
2828
+ }
2829
+ }
2830
+ }
2831
+
2832
+ // Perform input validation
2833
+ try {
2834
+ if (common.validateStrArray(command.nodeids, 1, 256) == false) { err = "Invalid nodeids"; } // Check nodeids
2835
+ if (common.validateString(command.meshid, 1, 1024) == false) { err = "Invalid groupid"; } // Check meshid
2836
+ else {
2837
+ if (command.meshid.indexOf('/') == -1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; }
2838
+ mesh = parent.meshes[command.meshid];
2839
+ if (mesh == null) { err = "Unknown device group"; }
2840
+ else if ((parent.GetMeshRights(user, mesh) & MESHRIGHT_MANAGECOMPUTERS) == 0) { err = "Permission denied"; }
2841
+ else if ((command.meshid.split('/').length != 3) || (command.meshid.split('/')[1] != domain.id)) { err = "Invalid domain"; } // Invalid domain, operation only valid for current domain
2842
+ }
2843
+ } catch (ex) { console.log(ex); err = "Validation exception: " + ex; }
2844
+
2845
+ // Handle any errors
2846
+ if (err != null) {
2847
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'addmeshuser', responseid: command.responseid, result: err })); } catch (ex) { } }
2848
+ break;
2849
+ }
2850
2851
// For each nodeid, change the group
2852
for (var i = 0; i < command.nodeids.length; i++) {
2853
+ var xnodeid = command.nodeids[i];
2854
+ if (xnodeid.indexOf('/') == -1) { xnodeid = 'node/' + domain.id + '/' + xnodeid; }
2855
+
2856
// Get the node and the rights for this node
2826
- parent.GetNodeWithRights(domain, user, command.nodeids[i], function (node, rights, visible) {
2857
+ parent.GetNodeWithRights(domain, user, xnodeid, function (node, rights, visible) {
2858
+ // Check if we found this device
2859
+ if (node == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changeDeviceMesh', responseid: command.responseid, result: 'Device not found' })); } catch (ex) { } } return; }
2860
+
2861
// Check if already in the right mesh
2828
- if ((node == null) || (node.meshid == command.meshid)) return;
2862
+ if (node.meshid == command.meshid) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changeDeviceMesh', responseid: command.responseid, result: 'Device already in correct group' })); } catch (ex) { } } return; }
2863
2864
// Make sure both source and target mesh are the same type
2831
- try { if (parent.meshes[node.meshid].mtype != parent.meshes[command.meshid].mtype) return; } catch (e) { return; };
2865
+ try { if (parent.meshes[node.meshid].mtype != parent.meshes[command.meshid].mtype) return; } catch (e) {
2866
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changeDeviceMesh', responseid: command.responseid, result: 'Device groups are of different types' })); } catch (ex) { } }
2867
+ return;
2868
+ };
2869
2870
// Make sure that we have rights on both source and destination mesh
2871
const targetMeshRights = parent.GetMeshRights(user, command.meshid);
2835
- if (((rights & MESHRIGHT_MANAGECOMPUTERS) == 0) || ((targetMeshRights & MESHRIGHT_MANAGECOMPUTERS) == 0)) return;
2872
+ if (((rights & MESHRIGHT_MANAGECOMPUTERS) == 0) || ((targetMeshRights & MESHRIGHT_MANAGECOMPUTERS) == 0)) {
2873
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changeDeviceMesh', responseid: command.responseid, result: 'Permission denied' })); } catch (ex) { } }
2874
+ return;
2875
+ }
2876
2877
// Perform the switch, start by saving the node with the new meshid.
2878
const oldMeshId = node.meshid;
@@ -2867,6 +2907,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2907
var event = { etype: 'node', userid: user._id, username: user.name, action: 'nodemeshchange', nodeid: node._id, node: node, oldMeshId: oldMeshId, newMeshId: command.meshid, msg: 'Moved device ' + node.name + ' to group ' + newMesh.name, domain: domain.id };
2908
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the mesh. Another event will come.
2909
parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(command.meshid, [oldMeshId, node._id]), obj, event);
2910
+
2911
+ // Send response if required
2912
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'changeDeviceMesh', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
2913
});
2914
}
2915
break;
translate/translate.json
+2
-2
@@ -20442,7 +20442,7 @@
20442
},
20443
{
20444
"en": "Remove User Group Permissions",
20445
- "nl": "Gebruikersgroepmachtigingen verwijderen"
20445
+ "nl": "Gebruikersgroepmachtigingen verwijderen",
20446
"xloc": [
20447
"default.handlebars->29->1272",
20448
"default.handlebars->29->1593"
@@ -29541,4 +29541,4 @@
29541
]
29542
}
29543
]
29544
-}
29544
+}
\ No newline at end of file