Added support device email notifications when user groups are used.

Ylian Saint-Hilaire committed Oct 14, 2021 at 22:13 UTC 064540d86534f62284cefe1492024c1a8a6420d3
1 file changed +37 -29
meshcentral.js
+37 -29
@@ -2094,38 +2094,46 @@ function CreateMeshCentralServer(config, args) {
2094 const mesh = obj.webserver.meshes[meshid];
2095 if ((mesh == null) || (mesh.links == null)) return;
2096
2097 - // Check if any user needs email notification
2098 - // TODO: Add user group support.
2097 + // Get the list of users that have visibility to this device
2098 + // This includes users that are part of user groups
2099 + var users = [];
2100 for (var i in mesh.links) {
2100 - if (i.startsWith('user/')) {
2101 - const user = obj.webserver.users[i];
2102 - if ((user != null) && (user.email != null) && (user.emailVerified == true)) {
2103 - var notify = 0;
2104 -
2105 - // Device group notifications
2106 - const meshLinks = user.links[meshid];
2107 - if ((meshLinks != null) && (meshLinks.notify != null)) { notify |= meshLinks.notify; }
2108 -
2109 - // User notifications
2110 - if (user.notify != null) {
2111 - if (user.notify[meshid] != null) { notify |= user.notify[meshid]; }
2112 - if (user.notify[nodeid] != null) { notify |= user.notify[nodeid]; }
2113 - }
2101 + if (i.startsWith('user/') && (users.indexOf(i) < 0)) { users.push(i); }
2102 + if (i.startsWith('ugrp/')) {
2103 + var usergrp = obj.webserver.userGroups[i];
2104 + if (usergrp.links != null) { for (var j in usergrp.links) { if (j.startsWith('user/') && (users.indexOf(j) < 0)) { users.push(j); } } }
2105 + }
2106 + }
2107
2115 - if ((notify & 48) != 0) {
2116 - if (stateSet == true) {
2117 - if ((notify & 16) != 0) {
2118 - mailserver.notifyDeviceConnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2119 - } else {
2120 - mailserver.cancelNotifyDeviceDisconnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2121 - }
2108 + // Check if any user needs email notification
2109 + for (var i in users) {
2110 + const user = obj.webserver.users[i];
2111 + if ((user != null) && (user.email != null) && (user.emailVerified == true)) {
2112 + var notify = 0;
2113 +
2114 + // Device group notifications
2115 + const meshLinks = user.links[meshid];
2116 + if ((meshLinks != null) && (meshLinks.notify != null)) { notify |= meshLinks.notify; }
2117 +
2118 + // User notifications
2119 + if (user.notify != null) {
2120 + if (user.notify[meshid] != null) { notify |= user.notify[meshid]; }
2121 + if (user.notify[nodeid] != null) { notify |= user.notify[nodeid]; }
2122 + }
2123 +
2124 + if ((notify & 48) != 0) {
2125 + if (stateSet == true) {
2126 + if ((notify & 16) != 0) {
2127 + mailserver.notifyDeviceConnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2128 + } else {
2129 + mailserver.cancelNotifyDeviceDisconnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2130 }
2123 - else if (stateSet == false) {
2124 - if ((notify & 32) != 0) {
2125 - mailserver.notifyDeviceDisconnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2126 - } else {
2127 - mailserver.cancelNotifyDeviceConnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2128 - }
2131 + }
2132 + else if (stateSet == false) {
2133 + if ((notify & 32) != 0) {
2134 + mailserver.notifyDeviceDisconnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2135 + } else {
2136 + mailserver.cancelNotifyDeviceConnect(user, meshid, nodeid, connectTime, connectType, powerState, serverid, extraInfo);
2137 }
2138 }
2139 }