Added function handler for adddeviceuser

Noah Zalev committed Jan 8, 2022 at 14:46 UTC e0915f97f88e0d7413c3a85e2ed29be1fcd6c328
1 file changed +137 -137
meshuser.js
+137 -137
@@ -2171,143 +2171,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2171 if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'editmesh', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
2172 break;
2173 }
2174 - case 'adddeviceuser': {
2175 - if (typeof command.userid == 'string') { command.userids = [command.userid]; }
2176 - var err = null, nodeIdSplit;
2177 - try {
2178 - if (common.validateString(command.nodeid, 1, 1024) == false) { err = 'Invalid nodeid'; } // Check the nodeid
2179 - else if (common.validateInt(command.rights) == false) { err = 'Invalid rights'; } // Device rights must be an integer
2180 - else if ((command.rights & 7) != 0) { err = 'Invalid rights'; } // EDITMESH, MANAGEUSERS or MANAGECOMPUTERS rights can't be assigned to a user to device link
2181 - else if ((common.validateStrArray(command.usernames, 1, 64) == false) && (common.validateStrArray(command.userids, 1, 128) == false)) { err = 'Invalid usernames'; } // Username is between 1 and 64 characters
2182 - else {
2183 - if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
2184 - else if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = 'Invalid domain'; } // Invalid domain, operation only valid for current domain
2185 - }
2186 - } catch (ex) { err = 'Validation exception: ' + ex; }
2187 -
2188 - // Handle any errors
2189 - if (err != null) {
2190 - if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: err })); } catch (ex) { } }
2191 - break;
2192 - }
2193 -
2194 - // Convert user names to userid's
2195 - if (command.userids == null) {
2196 - command.userids = [];
2197 - for (var i in command.usernames) {
2198 - if (command.usernames[i] != null) {
2199 - if (parent.users['user/' + domain.id + '/' + command.usernames[i].toLowerCase()] != null) { command.userids.push('user/' + domain.id + '/' + command.usernames[i].toLowerCase()); }
2200 - else if (parent.users['user/' + domain.id + '/' + command.usernames[i]] != null) { command.userids.push('user/' + domain.id + '/' + command.usernames[i]); }
2201 - }
2202 - }
2203 - }
2204 -
2205 - // Get the node and the rights for this node
2206 - parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
2207 - // Check if already in the right mesh
2208 - if ((node == null) || (node.meshid == command.meshid)) return;
2209 - var dispatchTargets = ['*', node.meshid, node._id];
2210 -
2211 - // Check that we have rights to manage users on this device
2212 - if ((rights & MESHRIGHT_MANAGEUSERS) == 0) return;
2213 -
2214 - // Add the new link to the users
2215 - var nodeChanged = false;
2216 - for (var i in command.userids) {
2217 - var newuserid = command.userids[i];
2218 -
2219 - // Add a user
2220 - var newuser = null;
2221 - if (newuserid.startsWith('ugrp/')) { newuser = parent.userGroups[newuserid]; }
2222 - if (newuserid.startsWith('user/')) {
2223 - newuser = parent.users[newuserid];
2224 -
2225 - // Search for a user name in that windows domain is the username starts with *\
2226 - if ((newuser == null) && (newuserid.startsWith('user/' + domain.id + '/*\\')) == true) {
2227 - var search = newuserid.split('/')[2].substring(1);
2228 - for (var i in parent.users) { if (i.endsWith(search) && (parent.users[i].domain == domain.id)) { newuser = parent.users[i]; command.userids[i] = newuserid = newuser._id; break; } }
2229 - }
2230 - }
2231 -
2232 - // Check the the user and device are in the same domain
2233 - if (command.nodeid.split('/')[1] != newuserid.split('/')[1]) return; // Domain mismatch
2234 -
2235 - if (newuser != null) {
2236 - // Add this user to the dispatch target list
2237 - dispatchTargets.push(newuser._id);
2238 -
2239 - if (command.remove === true) {
2240 - // Remove link to this user
2241 - if (newuser.links != null) {
2242 - delete newuser.links[command.nodeid];
2243 - if (Object.keys(newuser.links).length == 0) { delete newuser.links; }
2244 - }
2245 -
2246 - // Remove link to this device
2247 - if (node.links != null) {
2248 - delete node.links[newuserid];
2249 - nodeChanged = true;
2250 - if (Object.keys(node.links).length == 0) { delete node.links; }
2251 - }
2252 - } else {
2253 - // Add the new link to this user
2254 - if (newuser.links == null) { newuser.links = {}; }
2255 - newuser.links[command.nodeid] = { rights: command.rights };
2256 -
2257 - // Add the new link to the device
2258 - if (node.links == null) { node.links = {}; }
2259 - node.links[newuserid] = { rights: command.rights }
2260 - nodeChanged = true;
2261 - }
2262 -
2263 - // Save the user to the database
2264 - if (newuserid.startsWith('user/')) {
2265 - db.SetUser(newuser);
2266 - parent.parent.DispatchEvent([newuser], obj, 'resubscribe');
2267 -
2268 - // Notify user change
2269 - var targets = ['*', 'server-users', newuserid];
2270 - var event;
2271 - if (command.rights == 0) {
2272 - event = { etype: 'user', userid: user._id, username: user.name, action: 'accountchange', msgid: 81, msgArgs: [newuser.name], msg: 'Removed user device rights for ' + newuser.name, domain: domain.id, account: parent.CloneSafeUser(newuser), nodeListChange: newuserid };
2273 - } else {
2274 - event = { etype: 'user', userid: user._id, username: user.name, action: 'accountchange', msgid: 82, msgArgs: [newuser.name], msg: 'Changed user device rights for ' + newuser.name, domain: domain.id, account: parent.CloneSafeUser(newuser), nodeListChange: newuserid };
2275 - }
2276 - 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.
2277 - parent.parent.DispatchEvent(targets, obj, event);
2278 - } else if (newuserid.startsWith('ugrp/')) {
2279 - db.Set(newuser);
2280 -
2281 - // Notify user group change
2282 - var targets = ['*', 'server-ugroups', newuser._id];
2283 - var event = { etype: 'ugrp', username: user.name, ugrpid: newuser._id, name: newuser.name, action: 'usergroupchange', links: newuser.links, msgid: 79, msgArgs: [newuser.name], msg: 'User group changed: ' + newuser.name, domain: domain.id };
2284 - 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.
2285 - parent.parent.DispatchEvent(targets, obj, event);
2286 - }
2287 - }
2288 - }
2289 -
2290 - // Save the device
2291 - if (nodeChanged == true) {
2292 - // Save the node to the database
2293 - db.Set(parent.cleanDevice(node));
2294 -
2295 - // Event the node change
2296 - var event;
2297 - if (command.rights == 0) {
2298 - event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msgid: 81, msgArgs: [node.name], msg: 'Removed user device rights for ' + node.name, node: parent.CloneSafeNode(node) }
2299 - } else {
2300 - event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msgid: 82, msgArgs: [node.name], msg: 'Changed user device rights for ' + node.name, node: parent.CloneSafeNode(node) }
2301 - }
2302 - 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.
2303 - parent.parent.DispatchEvent(dispatchTargets, obj, event);
2304 - }
2305 -
2306 - if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
2307 - });
2308 -
2309 - break;
2310 - }
2174 case 'removemeshuser':
2175 {
2176 var xdomain, err = null;
@@ -5175,6 +5038,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5038 }
5039
5040 const serverCommands = {
5041 + 'adddeviceuser': serverCommandAddDeviceUser,
5042 'addmeshuser': serverCommandAddMeshUser,
5043 'adduser': serverCommandAddUser,
5044 'adduserbatch': serverCommandAddUserBatch,
@@ -5266,6 +5130,142 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5130 'webstats': [serverUserCommandWebStats, ""]
5131 };
5132
5133 + function serverCommandAddDeviceUser(command) {
5134 + if (typeof command.userid == 'string') { command.userids = [command.userid]; }
5135 + var err = null;
5136 + try {
5137 + if (common.validateString(command.nodeid, 1, 1024) == false) { err = 'Invalid nodeid'; } // Check the nodeid
5138 + else if (common.validateInt(command.rights) == false) { err = 'Invalid rights'; } // Device rights must be an integer
5139 + else if ((command.rights & 7) != 0) { err = 'Invalid rights'; } // EDITMESH, MANAGEUSERS or MANAGECOMPUTERS rights can't be assigned to a user to device link
5140 + else if ((common.validateStrArray(command.usernames, 1, 64) == false) && (common.validateStrArray(command.userids, 1, 128) == false)) { err = 'Invalid usernames'; } // Username is between 1 and 64 characters
5141 + else {
5142 + if (command.nodeid.indexOf('/') == -1) { command.nodeid = 'node/' + domain.id + '/' + command.nodeid; }
5143 + else if ((command.nodeid.split('/').length != 3) || (command.nodeid.split('/')[1] != domain.id)) { err = 'Invalid domain'; } // Invalid domain, operation only valid for current domain
5144 + }
5145 + } catch (ex) { err = 'Validation exception: ' + ex; }
5146 +
5147 + // Handle any errors
5148 + if (err != null) {
5149 + if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: err })); } catch (ex) { } }
5150 + return;
5151 + }
5152 +
5153 + // Convert user names to userid's
5154 + if (command.userids == null) {
5155 + command.userids = [];
5156 + for (var i in command.usernames) {
5157 + if (command.usernames[i] != null) {
5158 + if (parent.users['user/' + domain.id + '/' + command.usernames[i].toLowerCase()] != null) { command.userids.push('user/' + domain.id + '/' + command.usernames[i].toLowerCase()); }
5159 + else if (parent.users['user/' + domain.id + '/' + command.usernames[i]] != null) { command.userids.push('user/' + domain.id + '/' + command.usernames[i]); }
5160 + }
5161 + }
5162 + }
5163 +
5164 + // Get the node and the rights for this node
5165 + parent.GetNodeWithRights(domain, user, command.nodeid, function (node, rights, visible) {
5166 + // Check if already in the right mesh
5167 + if ((node == null) || (node.meshid == command.meshid)) return;
5168 + var dispatchTargets = ['*', node.meshid, node._id];
5169 +
5170 + // Check that we have rights to manage users on this device
5171 + if ((rights & MESHRIGHT_MANAGEUSERS) == 0) return;
5172 +
5173 + // Add the new link to the users
5174 + var nodeChanged = false;
5175 + for (var i in command.userids) {
5176 + var newuserid = command.userids[i];
5177 +
5178 + // Add a user
5179 + var newuser = null;
5180 + if (newuserid.startsWith('ugrp/')) { newuser = parent.userGroups[newuserid]; }
5181 + if (newuserid.startsWith('user/')) {
5182 + newuser = parent.users[newuserid];
5183 +
5184 + // Search for a user name in that windows domain is the username starts with *\
5185 + if ((newuser == null) && (newuserid.startsWith('user/' + domain.id + '/*\\')) == true) {
5186 + var search = newuserid.split('/')[2].substring(1);
5187 + for (var i in parent.users) { if (i.endsWith(search) && (parent.users[i].domain == domain.id)) { newuser = parent.users[i]; command.userids[i] = newuserid = newuser._id; break; } }
5188 + }
5189 + }
5190 +
5191 + // Check the the user and device are in the same domain
5192 + if (command.nodeid.split('/')[1] != newuserid.split('/')[1]) return; // Domain mismatch
5193 +
5194 + if (newuser != null) {
5195 + // Add this user to the dispatch target list
5196 + dispatchTargets.push(newuser._id);
5197 +
5198 + if (command.remove === true) {
5199 + // Remove link to this user
5200 + if (newuser.links != null) {
5201 + delete newuser.links[command.nodeid];
5202 + if (Object.keys(newuser.links).length == 0) { delete newuser.links; }
5203 + }
5204 +
5205 + // Remove link to this device
5206 + if (node.links != null) {
5207 + delete node.links[newuserid];
5208 + nodeChanged = true;
5209 + if (Object.keys(node.links).length == 0) { delete node.links; }
5210 + }
5211 + } else {
5212 + // Add the new link to this user
5213 + if (newuser.links == null) { newuser.links = {}; }
5214 + newuser.links[command.nodeid] = { rights: command.rights };
5215 +
5216 + // Add the new link to the device
5217 + if (node.links == null) { node.links = {}; }
5218 + node.links[newuserid] = { rights: command.rights };
5219 + nodeChanged = true;
5220 + }
5221 +
5222 + // Save the user to the database
5223 + if (newuserid.startsWith('user/')) {
5224 + db.SetUser(newuser);
5225 + parent.parent.DispatchEvent([newuser], obj, 'resubscribe');
5226 +
5227 + // Notify user change
5228 + var targets = ['*', 'server-users', newuserid];
5229 + var event;
5230 + if (command.rights == 0) {
5231 + event = { etype: 'user', userid: user._id, username: user.name, action: 'accountchange', msgid: 81, msgArgs: [newuser.name], msg: 'Removed user device rights for ' + newuser.name, domain: domain.id, account: parent.CloneSafeUser(newuser), nodeListChange: newuserid };
5232 + } else {
5233 + event = { etype: 'user', userid: user._id, username: user.name, action: 'accountchange', msgid: 82, msgArgs: [newuser.name], msg: 'Changed user device rights for ' + newuser.name, domain: domain.id, account: parent.CloneSafeUser(newuser), nodeListChange: newuserid };
5234 + }
5235 + 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.
5236 + parent.parent.DispatchEvent(targets, obj, event);
5237 + } else if (newuserid.startsWith('ugrp/')) {
5238 + db.Set(newuser);
5239 +
5240 + // Notify user group change
5241 + var targets = ['*', 'server-ugroups', newuser._id];
5242 + var event = { etype: 'ugrp', username: user.name, ugrpid: newuser._id, name: newuser.name, action: 'usergroupchange', links: newuser.links, msgid: 79, msgArgs: [newuser.name], msg: 'User group changed: ' + newuser.name, domain: domain.id };
5243 + 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.
5244 + parent.parent.DispatchEvent(targets, obj, event);
5245 + }
5246 + }
5247 + }
5248 +
5249 + // Save the device
5250 + if (nodeChanged == true) {
5251 + // Save the node to the database
5252 + db.Set(parent.cleanDevice(node));
5253 +
5254 + // Event the node change
5255 + var event;
5256 + if (command.rights == 0) {
5257 + event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msgid: 81, msgArgs: [node.name], msg: 'Removed user device rights for ' + node.name, node: parent.CloneSafeNode(node) };
5258 + } else {
5259 + event = { etype: 'node', userid: user._id, username: user.name, action: 'changenode', nodeid: node._id, domain: domain.id, msgid: 82, msgArgs: [node.name], msg: 'Changed user device rights for ' + node.name, node: parent.CloneSafeNode(node) };
5260 + }
5261 + 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.
5262 + parent.parent.DispatchEvent(dispatchTargets, obj, event);
5263 + }
5264 +
5265 + if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'adddeviceuser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
5266 + });
5267 + }
5268 +
5269 function serverCommandAddMeshUser(command) {
5270 var err = null, mesh, meshIdSplit;
5271 if (typeof command.userid == 'string') { command.userids = [command.userid]; }