More auto device removal fixes. #3089

Ylian Saint-Hilaire committed Sep 1, 2021 at 22:30 UTC b2905bfe2977901b1a31d30dd935a2cb64513fc1
2 files changed +56 -55
db.js
+56 -54
@@ -156,66 +156,68 @@ module.exports.CreateDB = function (parent, func) {
156 var expireDays = -1;
157 if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; }
158 const mesh = parent.webserver.meshes[docs[j].meshid];
159 - if (mesh && (typeof mesh.expireDevs == 'number') && (expireDays > mesh.expireDevs)) { expireDays = mesh.expireDevs; }
159 + if (mesh && (typeof mesh.expireDevs == 'number')) { expireDays = mesh.expireDevs; }
160 var remove = false;
161 - if ((expireDays > 0) && (expireDays < days)) { remove = true; }
162 - if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
163 - if (remove) {
164 - // Check if this device is connected right now
165 - const nodeid = docs[j]._id.substring(2);
166 - const conn = parent.GetConnectivityState(nodeid);
167 - if (conn == null) {
168 - // Remove the device
169 - obj.Get(nodeid, function (err, docs) {
170 - if (err != null) return;
171 - if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
172 - const node = docs[0];
173 -
174 - // Delete this node including network interface information, events and timeline
175 - obj.Remove(node._id); // Remove node with that id
176 - obj.Remove('if' + node._id); // Remove interface information
177 - obj.Remove('nt' + node._id); // Remove notes
178 - obj.Remove('lc' + node._id); // Remove last connect time
179 - obj.Remove('si' + node._id); // Remove system information
180 - obj.Remove('al' + node._id); // Remove error log last time
181 - if (obj.RemoveSMBIOS) { obj.RemoveSMBIOS(node._id); } // Remove SMBios data
182 - obj.RemoveAllNodeEvents(node._id); // Remove all events for this node
183 - obj.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
184 - if (typeof node.pmt == 'string') { obj.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
185 - obj.Get('ra' + node._id, function (err, nodes) {
186 - if ((nodes != null) && (nodes.length == 1)) { obj.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
187 - obj.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
188 - });
161 + if (expireDays > 0) {
162 + if (expireDays < days) { remove = true; }
163 + if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
164 + if (remove) {
165 + // Check if this device is connected right now
166 + const nodeid = docs[j]._id.substring(2);
167 + const conn = parent.GetConnectivityState(nodeid);
168 + if (conn == null) {
169 + // Remove the device
170 + obj.Get(nodeid, function (err, docs) {
171 + if (err != null) return;
172 + if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
173 + const node = docs[0];
174 +
175 + // Delete this node including network interface information, events and timeline
176 + obj.Remove(node._id); // Remove node with that id
177 + obj.Remove('if' + node._id); // Remove interface information
178 + obj.Remove('nt' + node._id); // Remove notes
179 + obj.Remove('lc' + node._id); // Remove last connect time
180 + obj.Remove('si' + node._id); // Remove system information
181 + obj.Remove('al' + node._id); // Remove error log last time
182 + if (obj.RemoveSMBIOS) { obj.RemoveSMBIOS(node._id); } // Remove SMBios data
183 + obj.RemoveAllNodeEvents(node._id); // Remove all events for this node
184 + obj.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
185 + if (typeof node.pmt == 'string') { obj.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
186 + obj.Get('ra' + node._id, function (err, nodes) {
187 + if ((nodes != null) && (nodes.length == 1)) { obj.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
188 + obj.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
189 + });
190
190 - // Remove any user node links
191 - if (node.links != null) {
192 - for (var i in node.links) {
193 - if (i.startsWith('user/')) {
194 - var cuser = parent.webserver.users[i];
195 - if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) {
196 - // Remove the user link & save the user
197 - delete cuser.links[node._id];
198 - if (Object.keys(cuser.links).length == 0) { delete cuser.links; }
199 - obj.SetUser(cuser);
200 -
201 - // Notify user change
202 - var targets = ['*', 'server-users', cuser._id];
203 - var event = { etype: 'user', userid: cuser._id, username: cuser.name, action: 'accountchange', msgid: 86, msgArgs: [cuser.name], msg: 'Removed user device rights for ' + cuser.name, domain: node.domain, account: parent.webserver.CloneSafeUser(cuser) };
204 - if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
205 - parent.DispatchEvent(targets, obj, event);
191 + // Remove any user node links
192 + if (node.links != null) {
193 + for (var i in node.links) {
194 + if (i.startsWith('user/')) {
195 + var cuser = parent.webserver.users[i];
196 + if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) {
197 + // Remove the user link & save the user
198 + delete cuser.links[node._id];
199 + if (Object.keys(cuser.links).length == 0) { delete cuser.links; }
200 + obj.SetUser(cuser);
201 +
202 + // Notify user change
203 + var targets = ['*', 'server-users', cuser._id];
204 + var event = { etype: 'user', userid: cuser._id, username: cuser.name, action: 'accountchange', msgid: 86, msgArgs: [cuser.name], msg: 'Removed user device rights for ' + cuser.name, domain: node.domain, account: parent.webserver.CloneSafeUser(cuser) };
205 + if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
206 + parent.DispatchEvent(targets, obj, event);
207 + }
208 }
209 }
210 }
209 - }
211
211 - // Event node deletion
212 - var meshname = '(unknown)';
213 - if ((parent.webserver.meshes[node.meshid] != null) && (parent.webserver.meshes[node.meshid].name != null)) { meshname = parent.webserver.meshes[node.meshid].name; }
214 - var event = { etype: 'node', action: 'removenode', nodeid: node._id, msgid: 87, msgArgs: [node.name, meshname], msg: 'Removed device ' + node.name + ' from device group ' + meshname, domain: node.domain };
215 - // TODO: We can't use the changeStream for node delete because we will not know the meshid the device was in.
216 - //if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to remove the node. Another event will come.
217 - parent.DispatchEvent(parent.webserver.CreateNodeDispatchTargets(node.meshid, node._id), obj, event);
218 - });
212 + // Event node deletion
213 + var meshname = '(unknown)';
214 + if ((parent.webserver.meshes[node.meshid] != null) && (parent.webserver.meshes[node.meshid].name != null)) { meshname = parent.webserver.meshes[node.meshid].name; }
215 + var event = { etype: 'node', action: 'removenode', nodeid: node._id, msgid: 87, msgArgs: [node.name, meshname], msg: 'Removed device ' + node.name + ' from device group ' + meshname, domain: node.domain };
216 + // TODO: We can't use the changeStream for node delete because we will not know the meshid the device was in.
217 + //if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to remove the node. Another event will come.
218 + parent.DispatchEvent(parent.webserver.CreateNodeDispatchTargets(node.meshid, node._id), obj, event);
219 + });
220 + }
221 }
222 }
223 }
meshuser.js
-1
@@ -6255,7 +6255,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6255 cmdData.result = 'Usage: removeinactivedevices [showremoved|showall]';
6256 } else {
6257 parent.db.removeInactiveDevices((arg == 'showall'), function (msg) { try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { } });
6258 - cmdData.result = 'Ok';
6258 }
6259 }
6260