From 46e7464b49a62921e6fab76597aa55fac44bc20c Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 31 Aug 2022 11:54:21 -0700 Subject: [PATCH] More work on server device paging. --- meshcentral-config-schema.json | 1 + meshcentral.js | 1 + meshuser.js | 25 ++++++++++++++++++++++--- sample-config-advanced.json | 1 + 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/meshcentral-config-schema.json b/meshcentral-config-schema.json index 49e969bb..c3dcb6f7 100644 --- a/meshcentral-config-schema.json +++ b/meshcentral-config-schema.json @@ -341,6 +341,7 @@ "loginPicture": { "type": "string", "default": null, "description": "Web site .png logo file placed in meshcentral-data that used on the login page when sitestyle is 2." }, "rootRedirect": { "type": "string", "default": null, "description": "Redirects HTTP root requests to this URL. When in use, direct users to /login to see the normal login page." }, "mobileSite": { "type": "boolean", "default": true, "description": "When set to false, this setting will disable the mobile site." }, + "maxDeviceView": { "type": "integer", "default": null, "description": "The maximum number of devices a user can see on the devices page at the same time. By default all devices will show, but this may need to be limited on servers with large number of devices." }, "unknownUserRootRedirect": { "type": "string", "default": null, "description": "Redirects HTTP root requests to this URL only where user is not already logged in. When in use, direct users to /login to see the normal login page." }, "nightMode": { "type": "integer", "default": 0, "description": "0 = User selects day/night mode, 1 = Always night mode, 2 = Always day mode" }, "userQuota": { "type": "integer" }, diff --git a/meshcentral.js b/meshcentral.js index aa43646b..6f76f14f 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -1278,6 +1278,7 @@ function CreateMeshCentralServer(config, args) { if (obj.config.domains[i].limits == null) { obj.config.domains[i].limits = {}; } if (obj.config.domains[i].dns == null) { obj.config.domains[i].url = (i == '') ? '/' : ('/' + i + '/'); } else { obj.config.domains[i].url = '/'; } obj.config.domains[i].id = i; + if ((typeof obj.config.domains[i].maxdeviceview != 'number') || (obj.config.domains[i].maxdeviceview < 1)) { delete obj.config.domains[i].maxdeviceview; } if (typeof obj.config.domains[i].loginkey == 'string') { obj.config.domains[i].loginkey = [obj.config.domains[i].loginkey]; } if ((obj.config.domains[i].loginkey != null) && (obj.common.validateAlphaNumericArray(obj.config.domains[i].loginkey, 1, 128) == false)) { console.log("ERROR: Invalid login key, must be alpha-numeric string with no spaces."); process.exit(); return; } if (typeof obj.config.domains[i].agentkey == 'string') { obj.config.domains[i].agentkey = [obj.config.domains[i].agentkey]; } diff --git a/meshuser.js b/meshuser.js index 18515363..a60b2932 100644 --- a/meshuser.js +++ b/meshuser.js @@ -109,6 +109,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use obj.deviceSkip = 0; // How many devices to skip obj.deviceLimit = 0; // How many devices to view obj.visibleDevices = null; // An object of visible nodeid's if the user is in paging mode + if (domain.maxdeviceview != null) { obj.deviceLimit = domain.maxdeviceview; obj.visibleDevices = {}; } // Check if we are a cross-domain administrator if (parent.parent.config.settings.managecrossdomain && (parent.parent.config.settings.managecrossdomain.indexOf(user._id) >= 0)) { obj.crossDomain = true; } @@ -613,6 +614,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use } serverinfo.preConfiguredScripts = r; } + if (domain.maxdeviceview != null) { serverinfo.maxdeviceview = domain.maxdeviceview; } // Maximum number of devices a user can view at any given time // Send server information try { ws.send(JSON.stringify({ action: 'serverinfo', serverinfo: serverinfo })); } catch (ex) { } @@ -675,6 +677,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use switch (command.action) { case 'nodes': { + // If in paging mode, look to set the skip and limit values + if (obj.visibleDevices != null) { + if ((typeof command.skip == 'number') && (command.skip >= 0)) { obj.deviceSkip = command.skip; } + if ((typeof command.limit == 'number') && (command.limit > 0)) { obj.deviceLimit = command.limit; } + if ((domain.maxdeviceview != null) && (obj.deviceLimit > domain.maxdeviceview)) { obj.deviceLimit = domain.maxdeviceview; } + } + var links = [], extraids = null, err = null; // Resolve the device group name if needed @@ -721,7 +730,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (docs == null) { docs = []; } parent.common.unEscapeAllLinksFieldName(docs); - var r = {}; + var r = {}, nodeCount = docs.length; if (obj.visibleDevices != null) { obj.visibleDevices = {}; } for (i in docs) { // Check device links, if a link points to an unknown user, remove it. @@ -805,9 +814,19 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // If in paging mode, report back the skip and limit values response.skip = obj.deviceSkip; response.limit = obj.deviceLimit; - // TODO: Add total device count + + // Add total device count + if (nodeCount < response.limit) { + response.totalcount = obj.deviceSkip + nodeCount; + try { ws.send(JSON.stringify(response)); } catch (ex) { } + } else { + // TODO + //console.log('response.totalcount', '???'); + try { ws.send(JSON.stringify(response)); } catch (ex) { } + } + } else { + try { ws.send(JSON.stringify(response)); } catch (ex) { } } - try { ws.send(JSON.stringify(response)); } catch (ex) { } }); break; } diff --git a/sample-config-advanced.json b/sample-config-advanced.json index a75bc3ab..ea23bfdc 100644 --- a/sample-config-advanced.json +++ b/sample-config-advanced.json @@ -180,6 +180,7 @@ "_loginPicture": "title-sample.png", "_rootRedirect": "https://www.youtube.com/watch?v=Gs069dndIYk", "_mobileSite": false, + "_maxDeviceView": 1000, "_unknownUserRootRedirect": "https://www.youtube.com/watch?v=2Q_ZzBGPdqE", "_nightMode": 1, "_userQuota": 1048576,