Merge pull request #19922 from overleaf/msm-fix-reversedHostname-launchpad

[web] Add missing `reversedHostname` for admins in CE/SP

GitOrigin-RevId: 2ce273e963a7471c514289cc042890bd1a14d4d2
This commit is contained in:
Miguel Serrano 2024-08-21 10:28:46 +02:00 committed by Copybot
parent 5db9bc63b7
commit 1175b3b080
4 changed files with 87 additions and 7 deletions

View file

@ -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 () => {}

View file

@ -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()

View file

@ -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')
})
})

View file

@ -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)