mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
bb722f3bff
Add script to backfill SSO affiliations GitOrigin-RevId: 5dc6678531311871b09fd79ed96f45f718753ad5
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
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)
|
|
})
|