More work on agent invite by codes.

Ylian Saint-Hilaire committed Mar 16, 2020 at 18:03 UTC edb6011efec2b63466ea79f7c4249f29c3231f8c
5 files changed +96 -7
MeshCentralServer.njsproj
+1
@@ -282,6 +282,7 @@
282 <Content Include="views\download.handlebars" />
283 <Content Include="views\error404-mobile.handlebars" />
284 <Content Include="views\error404.handlebars" />
285 + <Content Include="views\invite.handlebars" />
286 <Content Include="views\login-mobile.handlebars" />
287 <Content Include="views\login.handlebars" />
288 <Content Include="views\message.handlebars" />
meshuser.js
+14 -1
@@ -2272,9 +2272,22 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2272 if ((common.validateString(command.desc, 0, 1024) == true) && (command.desc != mesh.desc)) { if (change != '') change += ' and description changed'; else change += 'Group "' + mesh.name + '" description changed'; mesh.desc = command.desc; }
2273 if ((common.validateInt(command.flags) == true) && (command.flags != mesh.flags)) { if (change != '') change += ' and flags changed'; else change += 'Group "' + mesh.name + '" flags changed'; mesh.flags = command.flags; }
2274 if ((common.validateInt(command.consent) == true) && (command.consent != mesh.consent)) { if (change != '') change += ' and consent changed'; else change += 'Group "' + mesh.name + '" consent changed'; mesh.consent = command.consent; }
2275 +
2276 + if (command.invite === '*') {
2277 + // Clear invite codes
2278 + if (mesh.invite != null) { delete mesh.invite; }
2279 + if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
2280 + } else if (typeof command.invite === 'object') {
2281 + // Set invite codes
2282 + if ((mesh.invite == null) || (mesh.invite.codes != command.invite.codes) || (mesh.invite.flags != command.invite.flags)) {
2283 + mesh.invite = { codes: command.invite.codes, flags: command.invite.flags };
2284 + if (change != '') { change += ' and invite code changed'; } else { change += 'Group "' + mesh.name + '" invite code changed'; }
2285 + }
2286 + }
2287 +
2288 if (change != '') {
2289 db.Set(common.escapeLinksFieldName(mesh));
2277 - var event = { etype: 'mesh', userid: user._id, username: user.name, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, flags: mesh.flags, consent: mesh.consent, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id };
2290 + var event = { etype: 'mesh', userid: user._id, username: user.name, meshid: mesh._id, name: mesh.name, mtype: mesh.mtype, desc: mesh.desc, flags: mesh.flags, consent: mesh.consent, action: 'meshchange', links: mesh.links, msg: change, domain: domain.id, invite: mesh.invite };
2291 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.
2292 parent.parent.DispatchEvent(['*', mesh._id, user._id], obj, event);
2293 }
translate/translate.json
+1 -1
@@ -22018,4 +22018,4 @@
22018 ]
22019 }
22020 ]
22021 -}
22021 +}
\ No newline at end of file
views/default.handlebars
+67 -1
@@ -2319,6 +2319,7 @@
2319 if (message.event.consent != null) { meshes[message.event.meshid].consent = message.event.consent; }
2320 if (message.event.links) { meshes[message.event.meshid].links = message.event.links; }
2321 if (message.event.amt) { meshes[message.event.meshid].amt = message.event.amt; }
2322 + if (message.event.invite != null) { meshes[message.event.meshid].invite = message.event.invite; } else { delete meshes[message.event.meshid].invite; }
2323
2324 // Check if we lost rights to this mesh in this change.
2325 if (IsMeshViewable(message.event.meshid) == false) {
@@ -8136,7 +8137,7 @@
8137 x += addHtmlValue("User Consent", addLinkConditional(meshFeatures, 'p20editmeshconsent()', meshrights & 1));
8138 }
8139
8139 - // Display user consent
8140 + // Display user notification
8141 var meshNotify = 0, meshNotifyStr = [];
8142 if (userinfo.links && userinfo.links[currentMesh._id] && userinfo.links[currentMesh._id].notify) { meshNotify = userinfo.links[currentMesh._id].notify; }
8143 if (meshNotify & 2) { meshNotifyStr.push("Connect"); }
@@ -8145,6 +8146,14 @@
8146 if (meshNotifyStr.length == 0) { meshNotifyStr.push('<i>' + "None" + '</i>'); }
8147 x += addHtmlValue("Notifications", addLink(meshNotifyStr.join(', '), 'p20editMeshNotify()'));
8148
8149 + // Display invitation codes
8150 + if (features & 0x01000000) {
8151 + var inviteCodeStr = '<i>' + "None" + '</i>', icodes = false;
8152 + if (currentMesh.invite != null) { icodes = true; inviteCodeStr = currentMesh.invite.codes.join(', '); /* + ', ' + currentMesh.invite.flags;*/ }
8153 + //x += addHtmlValue("Invite Codes", addLink(inviteCodeStr, 'p20editmeshInviteCode()'));
8154 + x += addHtmlValue("Invite Codes", addLinkConditional(inviteCodeStr, 'p20editmeshInviteCode()', (meshrights & 1) || (icodes)));
8155 + }
8156 +
8157 // Intel AMT setup
8158 var intelAmtPolicy = "No Policy";
8159 if (currentMesh.amt) {
@@ -8659,6 +8668,63 @@
8668 function p20deleteUser(e, userid) { haltEvent(e); p20viewuserEx(2, decodeURIComponent(userid)); return false; }
8669 function p20viewuserEx2(button, userid) { meshserver.send({ action: 'removemeshuser', meshid: currentMesh._id, meshname: currentMesh.name, userid: userid }); }
8670
8671 + function p20editmeshInviteCode() {
8672 + if (xxdialogMode) return false;
8673 + var meshrights = GetMeshRights(currentMesh);
8674 +
8675 + var servername = serverinfo.name;
8676 + if ((servername.indexOf('.') == -1) || ((features & 2) != 0)) { servername = window.location.hostname; } // If the server name is not set or it's in LAN-only mode, use the URL hostname as server name.
8677 + var url;
8678 + if (serverinfo.https == true) {
8679 + var portStr = (serverinfo.port == 443) ? '' : (':' + serverinfo.port);
8680 + url = 'https://' + servername + portStr + domainUrl + 'invite';
8681 + } else {
8682 + var portStr = (serverinfo.port == 80) ? '' : (':' + serverinfo.port);
8683 + url = 'http://' + servername + portStr + domainUrl + 'invite';
8684 + }
8685 +
8686 + if (meshrights & 1) {
8687 + // We can edit the mesh invite codes
8688 + var x = "When enabled, invitation codes can be used by anyone to join devices to this device group using the following public link:" + '<br /><br />';
8689 + x += '<div style=width:100%;text-align:center><a target=_blank href="' + url + '">' + url + '</a></div><br />';
8690 + x += '<div style=margin-bottom:5px><label><input id=agentJoinCheck type=checkbox onclick=p20editmeshInviteCodeValidate() />' + "Enable Invite Codes" + '</label></div>';
8691 + x += addHtmlValue("Invite Codes", '<input id=agentInviteCode style=width:236px onkeyup=p20editmeshInviteCodeValidate() placeholder="code1, code2, code3" />');
8692 + x += addHtmlValue("Installation Type", '<select id=agentInviteType style=width:236px><option value=0>' + "Background and interactive" + '</option><option value=2>' + "Background only" + '</option><option value=1>' + "Interactive only" + '</option></select>');
8693 + setDialogMode(2, "Invite Codes", 3, p20editmeshInviteCodeEx, x);
8694 + if (currentMesh.invite != null) {
8695 + Q('agentJoinCheck').checked = true;
8696 + Q('agentInviteCode').value = currentMesh.invite.codes.join(', ');
8697 + Q('agentInviteType').value = (currentMesh.invite.flags & 3);
8698 + }
8699 + p20editmeshInviteCodeValidate();
8700 + } else {
8701 + // View codes only
8702 + var x = "Invitation codes can be used by anyone to join devices to this device group using the following public link:" + '<br /><br />';
8703 + x += '<div style=width:100%;text-align:center><a target=_blank href="' + url + '">' + url + '</a></div><br />';
8704 + x += addHtmlValue("Invite Codes", currentMesh.invite.codes.join(', '));
8705 + x += addHtmlValue("Installation Type", ["Background and interactive", "Background only", "Interactive only"][currentMesh.invite.flags & 3]);
8706 + setDialogMode(2, "Invite Codes", 1, null, x);
8707 + }
8708 + }
8709 +
8710 + function p20editmeshInviteCodeValidate() {
8711 + var ok = true, codes = Q('agentInviteCode').value.split(',');
8712 + for (var i in codes) { codes[i] = codes[i].trim(); if (codes[i] == '') { ok = false; } }
8713 + QE('agentInviteCode', Q('agentJoinCheck').checked);
8714 + QE('agentInviteType', Q('agentJoinCheck').checked);
8715 + QE('idx_dlgOkButton', (Q('agentJoinCheck').checked == false) || (ok));
8716 + }
8717 +
8718 + function p20editmeshInviteCodeEx() {
8719 + if (Q('agentJoinCheck').checked == true) {
8720 + var codes = Q('agentInviteCode').value.split(',');
8721 + for (var i in codes) { codes[i] = codes[i].trim(); }
8722 + meshserver.send({ action: 'editmesh', meshid: currentMesh._id, invite: { codes: codes, flags: parseInt(Q('agentInviteType').value) } });
8723 + } else {
8724 + meshserver.send({ action: 'editmesh', meshid: currentMesh._id, invite: '*' });
8725 + }
8726 + }
8727 +
8728 function p20editMeshNotify() {
8729 if (xxdialogMode) return false;
8730 var meshNotify = 0;
webserver.js
+13 -4
@@ -1270,8 +1270,14 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1270 if ((domain.loginkey != null) && (domain.loginkey.indexOf(req.query.key) == -1)) { res.sendStatus(404); return; } // Check 3FA URL key
1271 if ((req.body.inviteCode == null) || (req.body.inviteCode == '')) { render(req, res, getRenderPage('invite', req), getRenderArgs({ messageid: 0 }, domain)); return; } // No invitation code
1272
1273 - // Send invitation link, valid for 1 minute.
1274 - //res.redirect(domain.url + 'agentinvite?c=' + parent.encodeCookie({ a: 4, mid: 'mesh//xxxxx', f: 0, expire: 1 }, parent.invitationLinkEncryptionKey));
1273 + // Each for a device group that has this invite code.
1274 + for (var i in obj.meshes) {
1275 + if ((obj.meshes[i].invite != null) && (obj.meshes[i].invite.codes.indexOf(req.body.inviteCode) >= 0)) {
1276 + // Send invitation link, valid for 1 minute.
1277 + res.redirect(domain.url + 'agentinvite?c=' + parent.encodeCookie({ a: 4, mid: i, f: obj.meshes[i].invite.flags, expire: 1 }, parent.invitationLinkEncryptionKey) + (req.query.key ? ('&key=' + req.query.key) : ''));
1278 + return;
1279 + }
1280 + }
1281
1282 render(req, res, getRenderPage('invite', req), getRenderArgs({ messageid: 100 }, domain)); // Bad invitation code
1283 }
@@ -1639,6 +1645,7 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
1645 if (domain.usernameisemail) { features += 0x00200000; } // Username is email address
1646 if (parent.mqttbroker != null) { features += 0x00400000; } // This server supports MQTT channels
1647 if (((typeof domain.passwordrequirements != 'object') || (domain.passwordrequirements.email2factor != false)) && (parent.mailserver != null)) { features += 0x00800000; } // using email for 2FA is allowed
1648 + if (domain.agentinvitecodes == true) { features += 0x01000000; } // Support for agent invite codes
1649
1650 // Create a authentication cookie
1651 const authCookie = obj.parent.encodeCookie({ userid: user._id, domainid: domain.id, ip: cleanRemoteAddr(req.ip) }, obj.parent.loginCookieEncryptionKey);
@@ -3609,8 +3616,6 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3616 obj.app.post(url + 'resetpassword', handleResetPasswordRequest);
3617 obj.app.post(url + 'resetaccount', handleResetAccountRequest);
3618 obj.app.get(url + 'checkmail', handleCheckMailRequest);
3612 - obj.app.get(url + 'invite', handleInviteRequest);
3613 - obj.app.post(url + 'invite', handleInviteRequest);
3619 obj.app.get(url + 'agentinvite', handleAgentInviteRequest);
3620 obj.app.post(url + 'amtevents.ashx', obj.handleAmtEventRequest);
3621 obj.app.get(url + 'meshagents', obj.handleMeshAgentRequest);
@@ -3636,6 +3641,10 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3641 obj.app.get(url + 'player.htm', handlePlayerRequest);
3642 obj.app.get(url + 'player', handlePlayerRequest);
3643 obj.app.ws(url + 'amtactivate', handleAmtActivateWebSocket);
3644 + if (parent.config.domains[i].agentinvitecodes == true) {
3645 + obj.app.get(url + 'invite', handleInviteRequest);
3646 + obj.app.post(url + 'invite', handleInviteRequest);
3647 + }
3648 if (parent.pluginHandler != null) {
3649 obj.app.get(url + 'pluginadmin.ashx', obj.handlePluginAdminReq);
3650 obj.app.post(url + 'pluginadmin.ashx', obj.handlePluginAdminPostReq);