Added ListUsersOfUserGroup to MeshCtrl.js

Ylian Saint-Hilaire committed Oct 2, 2020 at 15:27 UTC 31062ccd57e271034bb1c28219ebe9a2eac24dbf
1 file changed +42 -9
meshctrl.js
+42 -9
@@ -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', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast'];
10 +const possibleCommands = ['listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'listusersofusergroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'editdevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download', 'deviceopenurl', 'devicemessage', 'devicetoast'];
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) {
@@ -24,6 +24,7 @@ if (args['_'].length == 0) {
24 console.log(" ListDevices - List devices.");
25 console.log(" ListDeviceGroups - List device groups.");
26 console.log(" ListUsersOfDeviceGroup - List the users in a device group.");
27 + console.log(" ListUsersOfUserGroup - List the users in a user group.");
28 console.log(" DeviceInfo - Show information about a device.");
29 console.log(" Config - Perform operation on config.json file.");
30 console.log(" AddUser - Create a new user account.");
@@ -80,6 +81,12 @@ if (args['_'].length == 0) {
81 else { ok = true; }
82 break;
83 }
84 + case 'listusersofusergroup': {
85 + if (args.groupid == null) { console.log(winRemoveSingleQuotes("User group id missing, use --groupid '[id]'")); }
86 + else { ok = true; }
87 + break;
88 + }
89 +
90 case 'deviceinfo': {
91 if (args.id == null) { console.log(winRemoveSingleQuotes("Missing device id, use --id '[deviceid]'")); }
92 else { ok = true; }
@@ -333,6 +340,17 @@ if (args['_'].length == 0) {
340 console.log(" --json - Show result as JSON.");
341 break;
342 }
343 + case 'listusersofusergroup': {
344 + console.log("List users that are part of a user group, Example usage:\r\n");
345 + console.log(" MeshCtrl ListUserOfUserGroup ");
346 + console.log("\r\nRequired arguments:\r\n");
347 + if (process.platform == 'win32') {
348 + console.log(" --id [groupid] - Device group identifier.");
349 + } else {
350 + console.log(" --id '[groupid]' - Device group identifier.");
351 + }
352 + break;
353 + }
354 case 'adduser': {
355 console.log("Add a new user account, Example usages:\r\n");
356 console.log(" MeshCtrl AddUser --user newaccountname --pass newpassword");
@@ -853,6 +871,7 @@ function serverConnect() {
871 case 'userinfo': { break; }
872 case 'listusers': { ws.send(JSON.stringify({ action: 'users', responseid: 'meshctrl' })); break; }
873 case 'listusersessions': { ws.send(JSON.stringify({ action: 'wssessioncount', responseid: 'meshctrl' })); }
874 + case 'listusersofusergroup':
875 case 'listusergroups': { ws.send(JSON.stringify({ action: 'usergroups', responseid: 'meshctrl' })); }
876 case 'listdevicegroups': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
877 case 'listusersofdevicegroup': { ws.send(JSON.stringify({ action: 'meshes', responseid: 'meshctrl' })); break; }
@@ -1174,16 +1193,30 @@ function serverConnect() {
1193 break;
1194 }
1195 case 'usergroups': { // LIST USER GROUPS
1177 - if (args.json) {
1178 - console.log(JSON.stringify(data.ugroups, ' ', 2));
1179 - } else {
1180 - for (var i in data.ugroups) {
1181 - var x = i + ', ' + data.ugroups[i].name;
1182 - if (data.ugroups[i].desc && (data.ugroups[i].desc != '')) { x += ', ' + data.ugroups[i].desc; }
1183 - console.log(x);
1196 + if (settings.cmd == 'listusergroups') {
1197 + if (args.json) {
1198 + console.log(JSON.stringify(data.ugroups, ' ', 2));
1199 + } else {
1200 + for (var i in data.ugroups) {
1201 + var x = i + ', ' + data.ugroups[i].name;
1202 + if (data.ugroups[i].desc && (data.ugroups[i].desc != '')) { x += ', ' + data.ugroups[i].desc; }
1203 + console.log(x);
1204 + }
1205 }
1206 + process.exit();
1207 + } else if (settings.cmd == 'listusersofusergroup') {
1208 + var ugrpid = args.groupid;
1209 + if ((args.domain != null) && (userid.indexOf('/') < 0)) { ugrpid = 'ugrp/' + args.domain + '/' + ugrpid; }
1210 + var ugroup = data.ugroups[ugrpid];
1211 + if (ugroup == null) {
1212 + console.log('User group not found.');
1213 + } else {
1214 + var usercount = 0;
1215 + if (ugroup.links) { for (var i in ugroup.links) { if (i.startsWith('user/')) { usercount++; console.log(i); } } }
1216 + if (usercount == 0) { console.log('No users in this user group.'); }
1217 + }
1218 + process.exit();
1219 }
1186 - process.exit();
1220 break;
1221 }
1222 case 'users': { // LISTUSERS