This commit is contained in:
Josiah Baldwin 2025-10-23 16:54:17 +02:00 committed by GitHub
commit d56dc0d1c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 7 deletions

View File

@ -5779,6 +5779,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)
}
}
@ -5797,7 +5798,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' }); }
});
}
@ -5917,6 +5917,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(mesh, [user._id, newuserid]), obj, event);
if (command.remove === true) { msgs.push("Removed user " + newuserid.split('/')[2]); } else { msgs.push("Added user " + newuserid.split('/')[2]); }
successCount++;
parent.InvalidateNodeCache(newuser, mesh)
} else {
msgs.push("Unknown user " + newuserid.split('/')[2]);
unknownUsers.push(newuserid.split('/')[2]);

View File

@ -9187,15 +9187,17 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
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;
const cache = GetNodeRightsCache[cacheid];
const cache = ((GetNodeRightsCache[user._id] || {})[meshid] || {})[nodeid];
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) {
const out = removeUserRights(r, user);
GetNodeRightsCache[cacheid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCache[user._id] = GetNodeRightsCache[user._id] || {}
GetNodeRightsCache[user._id][meshid] = GetNodeRightsCache[user._id][meshid] || {}
GetNodeRightsCache[user._id][meshid][nodeid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCacheCount++;
return out;
}
@ -9204,7 +9206,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
if ((user.links != null) && (user.links[nodeid] != null)) { r |= user.links[nodeid].rights; } // TODO: Deal with reverse permissions
if (r == 0xFFFFFFFF) {
const out = removeUserRights(r, user);
GetNodeRightsCache[cacheid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCache[user._id] = GetNodeRightsCache[user._id] || {}
GetNodeRightsCache[user._id][meshid] = GetNodeRightsCache[user._id][meshid] || {}
GetNodeRightsCache[user._id][meshid][nodeid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCacheCount++;
return out;
}
@ -9218,11 +9222,45 @@ module.exports.CreateWebServer = function (parent, db, args, certificates, doneF
}
const out = removeUserRights(r, user);
GetNodeRightsCache[cacheid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCache[user._id] = GetNodeRightsCache[user._id] || {}
GetNodeRightsCache[user._id][meshid] = GetNodeRightsCache[user._id][meshid] || {}
GetNodeRightsCache[user._id][meshid][nodeid] = { t: Date.now() + 10000, o: out };
GetNodeRightsCacheCount++;
return out;
}
obj.InvalidateNodeCache = function (user, mesh, nodeid) {
if (user == null) { return; }
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; };
if (mesh == null) {
for (let [key, val] of Object.entries(GetNodeRightsCache[user._id] || {})) {
GetNodeRightsCacheCount -= Object.keys(val).length
}
delete GetNodeRightsCache[user._id];
return;
}
if (nodeid == null) {
let cache_reduction = Object.keys((GetNodeRightsCache[user._id] || {})[meshid] || {}).length
delete (GetNodeRightsCache[user._id] || {})[meshid]
GetNodeRightsCacheCount -= cache_reduction;
return;
}
if (((GetNodeRightsCache[user._id] || {})[meshid] || {})[nodeid]) {
delete ((GetNodeRightsCache[user._id] || {})[meshid] || {})[nodeid]
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) {