First version with user groups enabled.

Ylian Saint-Hilaire committed Jan 4, 2020 at 13:46 UTC 1134dd2f7c583e57fb0a3577f55ab52465246334
3 files changed +34 -21
meshagent.js
+18 -18
@@ -72,7 +72,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
72 db.removeAllPowerEventsForNode(obj.dbNodeKey); // Remove all power events for this node
73
74 // Event node deletion
75 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'removenode', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 });
75 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'removenode', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 });
76
77 // Disconnect all connections if needed
78 const state = parent.parent.GetConnectivityState(obj.dbNodeKey);
@@ -541,12 +541,12 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
541 if (adminUser.links == null) adminUser.links = {};
542 adminUser.links[obj.dbMeshKey] = { rights: 0xFFFFFFFF };
543 db.SetUser(adminUser);
544 - parent.parent.DispatchEvent(['*', obj.dbMeshKey, adminUser._id], obj, { etype: 'mesh', username: adminUser.name, meshid: obj.dbMeshKey, name: meshname, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id });
544 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey, [adminUser._id]), obj, { etype: 'mesh', username: adminUser.name, meshid: obj.dbMeshKey, name: meshname, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id });
545 }
546 } else {
547 if ((mesh != null) && (mesh.deleted != null) && (mesh.links)) {
548 // Must un-delete this mesh
549 - var ids = ['*', mesh._id];
549 + var ids = parent.CreateMeshDispatchTargets(mesh._id);
550
551 // See if users still exists, if so, add links to the mesh
552 for (var userid in mesh.links) {
@@ -626,13 +626,13 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
626 mesh = { type: 'mesh', _id: obj.dbMeshKey, name: obj.meshid, mtype: 2, desc: '', domain: domain.id, links: links };
627 db.Set(common.escapeLinksFieldName(mesh));
628 parent.meshes[obj.meshid] = mesh;
629 - parent.parent.AddEventDispatch([obj.meshid], ws);
629 + parent.parent.AddEventDispatch(parent.CreateMeshDispatchTargets(obj.meshid), ws);
630
631 if (adminUser.links == null) user.links = {};
632 adminUser.links[obj.meshid] = { rights: 0xFFFFFFFF };
633 //adminUser.subscriptions = parent.subscribe(adminUser._id, ws);
634 db.SetUser(user);
635 - parent.parent.DispatchEvent(['*', meshid, user._id], obj, { etype: 'mesh', username: user.name, meshid: obj.meshid, name: obj.meshid, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id });
635 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(meshid, [user._id]), obj, { etype: 'mesh', username: user.name, meshid: obj.meshid, name: obj.meshid, mtype: 2, desc: '', action: 'createmesh', links: links, msg: 'Mesh created: ' + obj.meshid, domain: domain.id });
636 }
637 }
638
@@ -727,7 +727,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
727 var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device) };
728 if (log == 0) { event.nolog = 1; } else { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); }
729 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
730 - parent.parent.DispatchEvent(['*', device.meshid], obj, event);
730 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event);
731 }
732 }
733
@@ -766,9 +766,9 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
766 // Event the new node
767 if (obj.agentInfo.capabilities & 0x20) {
768 // This is a temporary agent, don't log.
769 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'addnode', node: device, domain: domain.id, nolog: 1 });
769 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'addnode', node: device, domain: domain.id, nolog: 1 });
770 } else {
771 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, { etype: 'node', action: 'addnode', node: device, msg: ('Added device ' + obj.agentInfo.computerName + ' to mesh ' + mesh.name), domain: domain.id });
771 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, { etype: 'node', action: 'addnode', node: device, msg: ('Added device ' + obj.agentInfo.computerName + ' to mesh ' + mesh.name), domain: domain.id });
772 }
773
774 completeAgentConnection3(device, mesh);
@@ -1099,7 +1099,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1099 } catch (ex) { }
1100
1101 // Event the node interface information change (This is a lot of traffic, probably don't need this).
1102 - //parent.parent.DispatchEvent(['*', obj.meshid], obj, { action: 'smBiosChange', nodeid: obj.dbNodeKey, domain: domain.id, smbios: command.value, nolog: 1 });
1102 + //parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.meshid), obj, { action: 'smBiosChange', nodeid: obj.dbNodeKey, domain: domain.id, smbios: command.value, nolog: 1 });
1103
1104 break;
1105 }
@@ -1113,7 +1113,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1113 db.Set(command);
1114
1115 // Event the node interface information change
1116 - parent.parent.DispatchEvent(['*', obj.meshid], obj, { action: 'ifchange', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 });
1116 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.meshid), obj, { action: 'ifchange', nodeid: obj.dbNodeKey, domain: domain.id, nolog: 1 });
1117
1118 break;
1119 }
@@ -1152,7 +1152,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1152
1153 // Event node deletion
1154 const change = 'Migrated device ' + node.name;
1155 - parent.parent.DispatchEvent(['*', node.meshid], obj, { etype: 'node', action: 'removenode', nodeid: node._id, msg: change, domain: node.domain });
1155 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(node.meshid), obj, { etype: 'node', action: 'removenode', nodeid: node._id, msg: change, domain: node.domain });
1156 }
1157 });
1158 break;
@@ -1168,7 +1168,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1168 // Log a value in the event log
1169 if ((typeof command.msg == 'string') && (command.msg.length < 4096)) {
1170 var event = { etype: 'node', action: 'agentlog', nodeid: obj.dbNodeKey, domain: domain.id, msg: command.msg };
1171 - var targets = ['*', obj.dbMeshKey];
1171 + var targets = parent.CreateMeshDispatchTargets(obj.dbMeshKey);
1172 if (typeof command.userid == 'string') {
1173 var loguser = parent.users[command.userid];
1174 if (loguser) { event.userid = command.userid; event.username = loguser.name; targets.push(command.userid); }
@@ -1212,7 +1212,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1212 // Log this activation event
1213 var event = { etype: 'node', action: 'amtactivate', nodeid: obj.dbNodeKey, domain: domain.id, msg: 'Device requested Intel AMT ACM activation, FQDN: ' + command.fqdn, ip: obj.remoteaddrport };
1214 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1215 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event);
1215 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event);
1216
1217 // Update the device Intel AMT information
1218 ChangeAgentCoreInfo({ "intelamt": { user: 'admin', pass: amtpassword, uuid: command.uuid, realm: command.realm } });
@@ -1262,7 +1262,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1262 if (((obj.agentInfo.capabilities & 0x40) != 0) && (typeof command.value.value == 'string') && (command.value.value.length < 256)) {
1263 // If this is a diagnostic agent, log the event in the log of the main agent
1264 var event = { etype: 'node', action: 'diagnostic', nodeid: obj.realNodeKey, domain: domain.id, msg: command.value.value };
1265 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event);
1265 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event);
1266 }
1267 break;
1268 }
@@ -1280,7 +1280,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1280
1281 // Event the new sysinfo hash, this will notify everyone that the sysinfo document was changed
1282 var event = { etype: 'node', action: 'sysinfohash', nodeid: obj.dbNodeKey, domain: domain.id, hash: command.data.hash, nolog: 1 };
1283 - parent.parent.DispatchEvent(['*', obj.dbMeshKey], obj, event);
1283 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(obj.dbMeshKey), obj, event);
1284 }
1285 break;
1286 }
@@ -1379,7 +1379,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1379 if (changes.length > 0) { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); }
1380 if ((log == 0) || ((obj.agentInfo) && (obj.agentInfo.capabilities) && (obj.agentInfo.capabilities & 0x20)) || (changes.length == 0)) { event.nolog = 1; } // If this is a temporary device, don't log changes
1381 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1382 - parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1382 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event);
1383 }
1384 }
1385 });
@@ -1420,7 +1420,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1420 var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), msg: 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', ') };
1421 if (obj.agentInfo.capabilities & 0x20) { event.nolog = 1; } // If this is a temporary device, don't log changes
1422 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1423 - parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1423 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event);
1424 }
1425 }
1426 });
@@ -1449,7 +1449,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1449 // Event the node change
1450 var event = { etype: 'node', action: 'changenode', nodeid: obj.dbNodeKey, domain: domain.id, node: parent.CloneSafeNode(device), nolog: 1 };
1451 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
1452 - parent.parent.DispatchEvent(['*', device.meshid], obj, event);
1452 + parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(device.meshid), obj, event);
1453 }
1454 }
1455 });
meshcentral.js
+3 -3
@@ -1338,7 +1338,7 @@ function CreateMeshCentralServer(config, args) {
1338
1339 // Event any changes on this server only
1340 if ((newConnectivity != oldPowerState) || (newPowerState != oldPowerState)) {
1341 - obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: newConnectivity, pwr: newPowerState, nolog: 1, nopeers: 1 });
1341 + obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: newConnectivity, pwr: newPowerState, nolog: 1, nopeers: 1 });
1342 }
1343 }
1344 };
@@ -1386,7 +1386,7 @@ function CreateMeshCentralServer(config, args) {
1386 }
1387
1388 // Event the node connection change
1389 - if (eventConnectChange == 1) { obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, ct: connectTime, nolog: 1, nopeers: 1 }); }
1389 + if (eventConnectChange == 1) { obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, ct: connectTime, nolog: 1, nopeers: 1 }); }
1390 } else {
1391 // Multi server mode
1392
@@ -1459,7 +1459,7 @@ function CreateMeshCentralServer(config, args) {
1459 }
1460
1461 // Event the node connection change
1462 - if (eventConnectChange == 1) { obj.DispatchEvent(['*', meshid], obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, nolog: 1, nopeers: 1 }); }
1462 + if (eventConnectChange == 1) { obj.DispatchEvent(obj.webserver.CreateMeshDispatchTargets(meshid), obj, { action: 'nodeconnect', meshid: meshid, nodeid: nodeid, conn: state.connectivity, pwr: state.powerState, nolog: 1, nopeers: 1 }); }
1463 } else {
1464 // Multi server mode
1465
webserver.js
+13
@@ -4079,6 +4079,19 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
4079 return false;
4080 }
4081
4082 + // Returns a list of displatch targets for a given mesh
4083 + // We have to target the meshid and all user groups for this mesh, plus any added targets
4084 + obj.CreateMeshDispatchTargets = function (mesh, addedTargets) {
4085 + var targets = (addedTargets != null) ? addedTargets : [];
4086 + if (targets.indexOf('*') == -1) { targets.push('*'); }
4087 + if (typeof mesh == 'string') { mesh = obj.meshes[mesh]; }
4088 + if (mesh != null) {
4089 + targets.push(mesh._id);
4090 + for (var i in mesh.links) { if (i.startsWith('ugrp/')) { targets.push(i); } }
4091 + }
4092 + return targets;
4093 + }
4094 +
4095 // Clone a safe version of a user object, remove everything that is secret.
4096 obj.CloneSafeUser = function (user) {
4097 if (typeof user != 'object') { return user; }