From 1175b3b08056952e2ffdfbaebc5c89ba4ada9ade Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Wed, 21 Aug 2024 10:28:46 +0200 Subject: [PATCH] Merge pull request #19922 from overleaf/msm-fix-reversedHostname-launchpad [web] Add missing `reversedHostname` for admins in CE/SP GitOrigin-RevId: 2ce273e963a7471c514289cc042890bd1a14d4d2 --- ...202407131109055_admin_reversed_hostname.js | 60 +++++++++++++++++++ .../launchpad/app/src/LaunchpadController.js | 15 ++++- .../test/acceptance/src/LaunchpadTests.js | 3 + .../test/unit/src/LaunchpadControllerTests.js | 16 +++-- 4 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 services/web/migrations/202407131109055_admin_reversed_hostname.js diff --git a/services/web/migrations/202407131109055_admin_reversed_hostname.js b/services/web/migrations/202407131109055_admin_reversed_hostname.js new file mode 100644 index 0000000000..d313224c4c --- /dev/null +++ b/services/web/migrations/202407131109055_admin_reversed_hostname.js @@ -0,0 +1,60 @@ +exports.tags = ['server-ce', 'server-pro'] + +exports.migrate = async client => { + const { db } = client + + const adminsWithEmails = await db.users + .find( + { + isAdmin: true, + emails: { $exists: true }, + }, + { _id: 1, emails: 1 } + ) + .toArray() + + for (const { _id, emails } of adminsWithEmails) { + let shouldUpdateEmails = false + for (const emailObj of emails) { + if (!emailObj.reversedHostname) { + shouldUpdateEmails = true + emailObj.reversedHostname = emailObj.email + .split('@')[1] + .split('') + .reverse() + .join('') + } + } + if (shouldUpdateEmails) { + await db.users.updateOne({ _id }, { $set: { emails } }) + } + } + + const adminsNoEmails = await db.users + .find( + { + isAdmin: true, + emails: { $exists: false }, + }, + { _id: 1, email: 1 } + ) + .toArray() + + for (const { _id, email } of adminsNoEmails) { + const reversedHostname = email.split('@')[1].split('').reverse().join('') + await db.users.updateOne( + { _id }, + { + emails: [ + { + email, + reversedHostname, + createdAt: new Date(), + }, + ], + } + ) + } +} + +exports.rollback = async () => {} diff --git a/services/web/modules/launchpad/app/src/LaunchpadController.js b/services/web/modules/launchpad/app/src/LaunchpadController.js index 23e98a5ac8..980debd745 100644 --- a/services/web/modules/launchpad/app/src/LaunchpadController.js +++ b/services/web/modules/launchpad/app/src/LaunchpadController.js @@ -137,11 +137,15 @@ const _LaunchpadController = { } try { + const reversedHostname = user.email + .split('@')[1] + .split('') + .reverse() + .join('') await User.updateOne( { _id: user._id }, { - $set: { isAdmin: true }, - emails: [{ email }], + $set: { isAdmin: true, emails: [{ email, reversedHostname }] }, } ).exec() } catch (err) { @@ -206,12 +210,17 @@ const _LaunchpadController = { logger.debug({ userId: user._id }, 'making user an admin') try { + const reversedHostname = user.email + .split('@')[1] + .split('') + .reverse() + .join('') await User.updateOne( { _id: user._id }, { $set: { isAdmin: true, - emails: [{ email }], + emails: [{ email, reversedHostname }], }, } ).exec() diff --git a/services/web/modules/launchpad/test/acceptance/src/LaunchpadTests.js b/services/web/modules/launchpad/test/acceptance/src/LaunchpadTests.js index 75730a97d6..4e38820feb 100644 --- a/services/web/modules/launchpad/test/acceptance/src/LaunchpadTests.js +++ b/services/web/modules/launchpad/test/acceptance/src/LaunchpadTests.js @@ -76,5 +76,8 @@ describe('Launchpad', function () { // Check we are actually admin expect(await adminUser.isLoggedIn()).to.equal(true) expect(adminUser.user.isAdmin).to.equal(true) + + // Check reversedHostName is stored + expect(adminUser.user.emails[0].reversedHostname).to.equal('moc.elpmaxe') }) }) diff --git a/services/web/modules/launchpad/test/unit/src/LaunchpadControllerTests.js b/services/web/modules/launchpad/test/unit/src/LaunchpadControllerTests.js index f56b0e0e0d..eda45eec8b 100644 --- a/services/web/modules/launchpad/test/unit/src/LaunchpadControllerTests.js +++ b/services/web/modules/launchpad/test/unit/src/LaunchpadControllerTests.js @@ -365,7 +365,9 @@ describe('LaunchpadController', function () { { $set: { isAdmin: true, - emails: [{ email: this.user.email }], + emails: [ + { email: this.user.email, reversedHostname: 'moc.elpmaxe' }, + ], }, } ) @@ -749,7 +751,9 @@ describe('LaunchpadController', function () { { $set: { isAdmin: true, - emails: [{ email: this.user.email }], + emails: [ + { email: this.user.email, reversedHostname: 'moc.elpmaxe' }, + ], }, } ) @@ -823,8 +827,12 @@ describe('LaunchpadController', function () { .calledWith( { _id: this.user._id }, { - $set: { isAdmin: true }, - emails: [{ email: this.user.email }], + $set: { + isAdmin: true, + emails: [ + { email: this.user.email, reversedHostname: 'moc.elpmaxe' }, + ], + }, } ) .should.equal(true)