Factor logincookie, changelang

Noah Zalev committed Dec 7, 2021 at 23:59 UTC 8bf011242398950b30c1ac1e10fa58b7ecbb7b2e
1 file changed +36 -37
meshuser.js
+36 -37
@@ -656,14 +656,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
656
657 // TODO: Send the message of user sessions connected to other servers.
658
659 - break;
660 - }
661 - case 'logincookie':
662 - {
663 - // If allowed, return a login cookie
664 - if (parent.parent.config.settings.allowlogintoken === true) {
665 - try { ws.send(JSON.stringify({ action: 'logincookie', cookie: parent.parent.encodeCookie({ u: user._id, a: 3 }, parent.parent.loginCookieEncryptionKey) })); } catch (ex) { }
666 - }
659 break;
660 }
661 case 'servertimelinestats':
@@ -1041,35 +1033,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1033 });
1034 break;
1035 }
1044 - case 'changelang':
1045 - {
1046 - // Do not allow this command when logged in using a login token
1047 - if (req.session.loginToken != null) break;
1048 -
1049 - // If this account is settings locked, return here.
1050 - if ((user.siteadmin != 0xFFFFFFFF) && ((user.siteadmin & 1024) != 0)) return;
1051 -
1052 - if (common.validateString(command.lang, 1, 6) == false) return;
1053 -
1054 - // Always lowercase the language
1055 - command.lang = command.lang.toLowerCase();
1056 -
1057 - // Update the user's language
1058 - var oldlang = user.lang;
1059 - if (command.lang == '*') { delete user.lang; } else { user.lang = command.lang; }
1060 - parent.db.SetUser(user);
1061 -
1062 - // Event the change
1063 - var message = { etype: 'user', userid: user._id, username: user.name, account: parent.CloneSafeUser(user), action: 'accountchange', domain: domain.id, msgid: 2, msgArgs: [(oldlang ? oldlang : 'default'), (user.lang ? user.lang : 'default')] };
1064 - if (db.changeStream) { message.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
1065 - message.msg = 'Changed language from ' + (oldlang ? oldlang : 'default') + ' to ' + (user.lang ? user.lang : 'default');
1066 -
1067 - var targets = ['*', 'server-users', user._id];
1068 - if (user.groups) { for (var i in user.groups) { targets.push('server-users:' + i); } }
1069 - parent.parent.DispatchEvent(targets, obj, message);
1070 -
1071 - break;
1072 - }
1036 case 'changeemail':
1037 {
1038 // Do not allow this command when logged in using a login token
@@ -5454,12 +5417,14 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5417 'adduserbatch': serverCommandAddUserBatch,
5418 'addusertousergroup': serverCommandAddUserToUserGroup,
5419 'authcookie': serverCommandAuthCookie,
5420 + 'changelang': serverCommandChangeLang,
5421 'files': serverCommandFiles,
5422 'getnetworkinfo': serverCommandGetNetworkInfo,
5423 'getsysinfo': serverCommandGetSysInfo,
5424 'intersession': serverCommandInterSession,
5425 'lastconnect': serverCommandLastConnect,
5426 'lastconnects': serverCommandLastConnects,
5427 + 'logincookie': serverCommandLoginCookie,
5428 'meshes': serverCommandMeshes,
5429 'ping': serverCommandPing,
5430 'pong': serverCommandPong,
@@ -5828,6 +5793,33 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5793 } catch (ex) { }
5794 }
5795
5796 + function serverCommandChangeLang(command) {
5797 + // Do not allow this command when logged in using a login token
5798 + if (req.session.loginToken != null) return;
5799 +
5800 + // If this account is settings locked, return here.
5801 + if ((user.siteadmin != 0xFFFFFFFF) && ((user.siteadmin & 1024) != 0)) return;
5802 +
5803 + if (common.validateString(command.lang, 1, 6) == false) return;
5804 +
5805 + // Always lowercase the language
5806 + command.lang = command.lang.toLowerCase();
5807 +
5808 + // Update the user's language
5809 + var oldlang = user.lang;
5810 + if (command.lang == '*') { delete user.lang; } else { user.lang = command.lang; }
5811 + parent.db.SetUser(user);
5812 +
5813 + // Event the change
5814 + var message = { etype: 'user', userid: user._id, username: user.name, account: parent.CloneSafeUser(user), action: 'accountchange', domain: domain.id, msgid: 2, msgArgs: [(oldlang ? oldlang : 'default'), (user.lang ? user.lang : 'default')] };
5815 + if (db.changeStream) { message.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
5816 + message.msg = 'Changed language from ' + (oldlang ? oldlang : 'default') + ' to ' + (user.lang ? user.lang : 'default');
5817 +
5818 + var targets = ['*', 'server-users', user._id];
5819 + if (user.groups) { for (var i in user.groups) { targets.push('server-users:' + i); } }
5820 + parent.parent.DispatchEvent(targets, obj, message);
5821 + }
5822 +
5823 function serverCommandFiles(command) {
5824 // Send the full list of server files to the browser app
5825 updateUserFiles(user, ws, domain);
@@ -5932,6 +5924,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5924 });
5925 }
5926
5927 + function serverCommandLoginCookie(command) {
5928 + // If allowed, return a login cookie
5929 + if (parent.parent.config.settings.allowlogintoken === true) {
5930 + try { ws.send(JSON.stringify({ action: 'logincookie', cookie: parent.parent.encodeCookie({ u: user._id, a: 3 }, parent.parent.loginCookieEncryptionKey) })); } catch (ex) { }
5931 + }
5932 + }
5933 +
5934 function serverCommandMeshes(command) {
5935 // Request a list of all meshes this user as rights to
5936 try { ws.send(JSON.stringify({ action: 'meshes', meshes: parent.GetAllMeshWithRights(user).map(parent.CloneSafeMesh), tag: command.tag })); } catch (ex) { }