Merge pull request #2628 from overleaf/jel-backfill-sso

Add script to backfill SSO affiliations

GitOrigin-RevId: 5dc6678531311871b09fd79ed96f45f718753ad5
This commit is contained in:
Timothée Alby 2020-02-28 10:08:06 -05:00 committed by Copybot
parent 4e760e276a
commit bb722f3bff

View file

@ -0,0 +1,53 @@
const { User } = require('../app/src/models/User')
const UserUpdater = require('../app/src/Features/User/UserUpdater')
require('logger-sharelatex').logger.level('error')
const pLimit = require('p-limit')
const CONCURRENCY = 10
const unexpectedUserStates = []
console.log('Starting SSO affiliation backfill')
const query = {
emails: {
$elemMatch: {
samlProviderId: { $exists: true },
confirmedAt: { $exists: false }
}
}
}
async function backfillAffiliation(user) {
const ssoEmail = user.emails.filter(
emailData => !emailData.confirmedAt && emailData.samlProviderId
)
if (ssoEmail.length > 1 || ssoEmail.length === 0) {
unexpectedUserStates.push(user._id)
}
const { email } = ssoEmail[0]
await UserUpdater.promises.confirmEmail(user._id, email)
}
async function getUsers() {
return User.find(query, { emails: 1 }).exec()
}
async function run() {
const limit = pLimit(CONCURRENCY)
const users = await getUsers()
console.log(`Found ${users.length} users`)
await Promise.all(users.map(user => limit(() => backfillAffiliation(user))))
console.log('Finished')
console.log(
`Found ${unexpectedUserStates.length} in unexpected states`,
unexpectedUserStates
)
}
run()
.then(() => {
process.exit()
})
.catch(error => {
console.log(error)
process.exit(1)
})