Added device group edit in MeshCtrl.js.

This commit is contained in:
Ylian Saint-Hilaire 2020-07-24 12:32:43 -07:00
parent a2a47a1320
commit 39295112ad
2 changed files with 125 additions and 44 deletions

View File

@ -7,7 +7,7 @@ try { require('ws'); } catch (ex) { console.log('Missing module "ws", type "npm
var settings = {};
const crypto = require('crypto');
const args = require('minimist')(process.argv.slice(2));
const possibleCommands = ['listusers', 'listusersessions', 'listdevicegroups', 'listdevices', 'listusersofdevicegroup', 'serverinfo', 'userinfo', 'adduser', 'removeuser', 'adddevicegroup', 'removedevicegroup', 'broadcast', 'showevents', 'addusertodevicegroup', 'removeuserfromdevicegroup', 'addusertodevice', 'removeuserfromdevice', 'sendinviteemail', 'generateinvitelink', 'config', 'movetodevicegroup', 'deviceinfo', 'addusergroup', 'listusergroups', 'removeusergroup', 'runcommand', 'shell', 'upload', 'download'];
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'];
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; } }
if (args['_'].length == 0) {
@ -32,6 +32,7 @@ if (args['_'].length == 0) {
console.log(" RemoveUserGroup - Delete a user group.");
console.log(" AddDeviceGroup - Create a new device group.");
console.log(" RemoveDeviceGroup - Delete a device group.");
console.log(" EditDeviceGroup - Change a device group values.");
console.log(" MoveToDeviceGroup - Move a device to a different device group.");
console.log(" AddUserToDeviceGroup - Add a user to a device group.");
console.log(" RemoveUserFromDeviceGroup - Remove a user from a device group.");
@ -109,6 +110,7 @@ if (args['_'].length == 0) {
else { ok = true; }
break;
}
case 'editdevicegroup':
case 'removedevicegroup': {
if ((args.id == null) && (args.group == null)) { console.log(winRemoveSingleQuotes("Device group identifier missing, use --id '[groupid]' or --group [groupname]")); }
else { ok = true; }
@ -360,6 +362,35 @@ if (args['_'].length == 0) {
console.log(" --group [groupname] - Device group name (or --id).");
break;
}
case 'editdevicegroup': {
console.log("Edit a device group, Example usages:\r\n");
console.log(" MeshCtrl EditDeviceGroup --id 'groupid' --name 'NewName'");
console.log("\r\nRequired arguments:\r\n");
if (process.platform == 'win32') {
console.log(" --id [groupid] - Device group identifier (or --group).");
} else {
console.log(" --id '[groupid]' - Device group identifier (or --group).");
}
console.log(" --group [groupname] - Device group name (or --id).");
console.log("\r\nOptional arguments:\r\n");
console.log(" --name [name] - Set new device group name.");
console.log(" --desc [name] - Set new device group description, blank to clear.");
console.log(" --flags [number] - Set device group flags, sum of the values below, 0 for none.");
console.log(" 1 = Auto remove device on disconnect.");
console.log(" 2 = Sync hostname.");
console.log(" --consent [number] - Set device group consent options, sum of the values below, 0 for none.");
console.log(" 1 = Desktop notify user.");
console.log(" 2 = Terminal notify user.");
console.log(" 4 = Files notify user.");
console.log(" 8 = Desktop prompt for user consent.");
console.log(" 16 = Terminal prompt for user consent.");
console.log(" 32 = Files prompt for user consent.");
console.log(" 64 = Desktop show connection toolbar.");
console.log(" --invitecodes [aa,bb] - Comma seperated list of invite codes, blank to clear.");
console.log(" --backgroundonly - When used with invitecodes, set agent to only install in background.");
console.log(" --interactiveonly - When used with invitecodes, set agent to only run on demand.");
break;
}
case 'movetodevicegroup': {
console.log("Move a device to a new device group, Example usages:\r\n");
console.log(winRemoveSingleQuotes(" MeshCtrl MoveToDeviceGroup --devid 'deviceid' --id 'groupid'"));
@ -814,6 +845,31 @@ function serverConnect() {
ws.send(JSON.stringify(op));
break;
}
case 'editdevicegroup': {
var op = { action: 'editmesh', responseid: 'meshctrl' };
if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshidname = args.group; }
if ((typeof args.name == 'string') && (args.name != '')) { op.meshname = args.name; }
if (args.desc === true) { op.desc = ""; } else if (typeof args.desc == 'string') { op.desc = args.desc; }
if (args.invitecodes === true) { op.invite = "*"; } else if (typeof args.invitecodes == 'string') {
var invitecodes = args.invitecodes.split(','), invitecodes2 = [];
for (var i in invitecodes) { if (invitecodes[i].length > 0) { invitecodes2.push(invitecodes[i]); } }
if (invitecodes2.length > 0) {
op.invite = { codes: invitecodes2, flags: 0 };
if (args.backgroundonly === true) { op.invite.flags = 2; }
else if (args.interactiveonly === true) { op.invite.flags = 1; }
}
}
if (args.flags != null) {
var flags = parseInt(args.flags);
if (typeof flags == 'number') { op.flags = flags; }
}
if (args.consent != null) {
var consent = parseInt(args.consent);
if (typeof consent == 'number') { op.consent = consent; }
}
ws.send(JSON.stringify(op));
break;
}
case 'movetodevicegroup': {
var op = { action: 'changeDeviceMesh', responseid: 'meshctrl', nodeids: [ args.devid ] };
if (args.id) { op.meshid = args.id; } else if (args.group) { op.meshname = args.group; }
@ -1000,6 +1056,7 @@ function serverConnect() {
case 'deleteuser': // REMOVEUSER
case 'createmesh': // ADDDEVICEGROUP
case 'deletemesh': // REMOVEDEVICEGROUP
case 'editmesh': // EDITDEVICEGROUP
case 'changeDeviceMesh':
case 'addmeshuser': //
case 'removemeshuser': //

View File

@ -2590,6 +2590,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
}
case 'deletemesh':
{
// Delete a mesh and all computers within it
var err = null;
// Resolve the device group name if needed
@ -2602,8 +2603,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
}
}
// Validate input
try {
// Delete a mesh and all computers within it
if (common.validateString(command.meshid, 1, 1024) == false) { err = 'Invalid group identifier'; } // Check the meshid
else if (command.meshid.indexOf('/') == -1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; }
} catch (ex) { err = 'Validation exception: ' + ex; }
@ -2682,54 +2683,77 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
case 'editmesh':
{
// Change the name or description of a device group (mesh)
if (common.validateString(command.meshid, 1, 1024) == false) break; // Check the meshid
mesh = parent.meshes[command.meshid];
change = '';
var err = null;
if (mesh) {
// Check if this user has rights to do this
if ((parent.GetMeshRights(user, mesh) & MESHRIGHT_EDITMESH) == 0) return;
if ((command.meshid.split('/').length != 3) || (command.meshid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
if ((common.validateString(command.meshname, 1, 64) == true) && (command.meshname != mesh.name)) { change = 'Group name changed from "' + mesh.name + '" to "' + command.meshname + '"'; mesh.name = command.meshname; }
if ((common.validateString(command.desc, 0, 1024) == true) && (command.desc != mesh.desc)) { if (change != '') change += ' and description changed'; else change += 'Group "' + mesh.name + '" description changed'; mesh.desc = command.desc; }
if ((common.validateInt(command.flags) == true) && (command.flags != mesh.flags)) { if (change != '') change += ' and flags changed'; else change += 'Group "' + mesh.name + '" flags changed'; mesh.flags = command.flags; }
if ((common.validateInt(command.consent) == true) && (command.consent != mesh.consent)) { if (change != '') change += ' and consent changed'; else change += 'Group "' + mesh.name + '" consent changed'; mesh.consent = command.consent; }
// See if we need to change device group invitation codes
if (mesh.mtype == 2) {
if (command.invite === '*') {
// Clear invite codes
if (mesh.invite != null) { delete mesh.invite; }
if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
} else if (typeof command.invite === 'object') {
// Set invite codes
if ((mesh.invite == null) || (mesh.invite.codes != command.invite.codes) || (mesh.invite.flags != command.invite.flags)) {
// Check if an invite code is not already in use.
var dup = null;
for (var i in command.invite.codes) {
for (var j in parent.meshes) {
if ((j != command.meshid) && (parent.meshes[j].domain == domain.id) && (parent.meshes[j].invite != null) && (parent.meshes[j].invite.codes.indexOf(command.invite.codes[i]) >= 0)) { dup = command.invite.codes[i]; break; }
}
}
if (dup != null) {
// A duplicate was found, don't allow this change.
displayNotificationMessage('Error, invite code \"' + dup + '\" already in use.', 'Invite Codes');
return;
}
mesh.invite = { codes: command.invite.codes, flags: command.invite.flags };
if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
}
// Resolve the device group name if needed
if ((typeof command.meshidname == 'string') && (command.meshid == null)) {
for (var i in parent.meshes) {
var m = parent.meshes[i];
if ((m.mtype == 2) && (m.name == command.meshidname) && parent.IsMeshViewable(user, m)) {
if (command.meshid == null) { command.meshid = m._id; } else { err = 'Duplicate device groups found'; }
}
}
}
if (change != '') {
db.Set(mesh);
var event = { etype: 'mesh', userid: user._id, username: user.name, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, flags: mesh.flags, consent: mesh.consent, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id, invite: mesh.invite };
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.
parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(mesh, [user._id]), obj, event);
// Validate input
try {
if (common.validateString(command.meshid, 1, 1024) == false) { err = 'Invalid group identifier'; } // Check the meshid
else if (command.meshid.indexOf('/') == -1) { command.meshid = 'mesh/' + domain.id + '/' + command.meshid; }
if (err == null) {
mesh = parent.meshes[command.meshid];
if (mesh == null) { err = 'Invalid group identifier '; }
}
} catch (ex) { err = 'Validation exception: ' + ex; }
// Handle any errors
if (err != null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'editmesh', responseid: command.responseid, result: err })); } catch (ex) { } } break; }
change = '';
// Check if this user has rights to do this
if ((parent.GetMeshRights(user, mesh) & MESHRIGHT_EDITMESH) == 0) return;
if ((command.meshid.split('/').length != 3) || (command.meshid.split('/')[1] != domain.id)) return; // Invalid domain, operation only valid for current domain
if ((common.validateString(command.meshname, 1, 64) == true) && (command.meshname != mesh.name)) { change = 'Group name changed from "' + mesh.name + '" to "' + command.meshname + '"'; mesh.name = command.meshname; }
if ((common.validateString(command.desc, 0, 1024) == true) && (command.desc != mesh.desc)) { if (change != '') change += ' and description changed'; else change += 'Group "' + mesh.name + '" description changed'; mesh.desc = command.desc; }
if ((common.validateInt(command.flags) == true) && (command.flags != mesh.flags)) { if (change != '') change += ' and flags changed'; else change += 'Group "' + mesh.name + '" flags changed'; mesh.flags = command.flags; }
if ((common.validateInt(command.consent) == true) && (command.consent != mesh.consent)) { if (change != '') change += ' and consent changed'; else change += 'Group "' + mesh.name + '" consent changed'; mesh.consent = command.consent; }
// See if we need to change device group invitation codes
if (mesh.mtype == 2) {
if (command.invite === '*') {
// Clear invite codes
if (mesh.invite != null) { delete mesh.invite; }
if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
} else if ((typeof command.invite == 'object') && (Array.isArray(command.invite.codes)) && (typeof command.invite.flags == 'number')) {
// Set invite codes
if ((mesh.invite == null) || (mesh.invite.codes != command.invite.codes) || (mesh.invite.flags != command.invite.flags)) {
// Check if an invite code is not already in use.
var dup = null;
for (var i in command.invite.codes) {
for (var j in parent.meshes) {
if ((j != command.meshid) && (parent.meshes[j].domain == domain.id) && (parent.meshes[j].invite != null) && (parent.meshes[j].invite.codes.indexOf(command.invite.codes[i]) >= 0)) { dup = command.invite.codes[i]; break; }
}
}
if (dup != null) {
// A duplicate was found, don't allow this change.
displayNotificationMessage('Error, invite code \"' + dup + '\" already in use.', 'Invite Codes');
return;
}
mesh.invite = { codes: command.invite.codes, flags: command.invite.flags };
if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
}
}
}
if (change != '') {
db.Set(mesh);
var event = { etype: 'mesh', userid: user._id, username: user.name, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, flags: mesh.flags, consent: mesh.consent, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id, invite: mesh.invite };
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.
parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(mesh, [user._id]), obj, event);
}
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'editmesh', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
break;
}
case 'addmeshuser':