diff --git a/meshuser.js b/meshuser.js index 10cc4ec0ed..c8d2c44be8 100644 --- a/meshuser.js +++ b/meshuser.js @@ -5776,6 +5776,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 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. parent.parent.DispatchEvent(targets, obj, event); } + parent.InvalidateNodeCache(newuser, node.meshid, node._id) } } @@ -5794,7 +5795,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use 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(dispatchTargets, obj, event); } - if (command.responseid != null) { obj.send({ action: 'adddeviceuser', responseid: command.responseid, result: 'ok' }); } }); } @@ -5920,6 +5920,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use failCount++; } } + parent.FlushGetNodeRightsCache() if ((successCount == 0) && (failCount == 0)) { msgs.push("Nothing done"); } diff --git a/webserver.js b/webserver.js index b470dec126..ccb0da0459 100644 --- a/webserver.js +++ b/webserver.js @@ -9174,7 +9174,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF const cacheid = user._id + '/' + meshid + '/' + nodeid; const cache = GetNodeRightsCache[cacheid]; if (cache != null) { if (cache.t > Date.now()) { return cache.o; } else { GetNodeRightsCacheCount--; } } // Cache hit, or we need to update the cache - if (GetNodeRightsCacheCount > 2000) { GetNodeRightsCache = {}; GetNodeRightsCacheCount = 0; } // From time to time, flush the cache + if (GetNodeRightsCacheCount > 2000) { obj.FlushGetNodeRightsCache() } // From time to time, flush the cache var r = obj.GetMeshRights(user, mesh); if (r == 0xFFFFFFFF) { @@ -9207,6 +9207,24 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF return out; } + obj.InvalidateNodeCache = function (user, mesh, nodeid) { + if ((user == null) || (mesh == null) || (nodeid == null)) { return 0; } + if (typeof user == 'string') { user = obj.users[user]; } + if (user == null) { return 0; } + var meshid; + if (typeof mesh == 'string') { meshid = mesh; } else if ((typeof mesh == 'object') && (typeof mesh._id == 'string')) { meshid = mesh._id; } else return 0; + + // Check if we have this in the cache + const cacheid = user._id + '/' + meshid + '/' + nodeid; + delete GetNodeRightsCache[cacheid]; + GetNodeRightsCacheCount--; + } + + obj.FlushGetNodeRightsCache = function() { + GetNodeRightsCache = {}; + GetNodeRightsCacheCount = 0; + } + // Returns a list of displatch targets for a given mesh // We have to target the meshid and all user groups for this mesh, plus any added targets obj.CreateMeshDispatchTargets = function (mesh, addedTargets) {