mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #2628 from overleaf/jel-backfill-sso
Add script to backfill SSO affiliations GitOrigin-RevId: 5dc6678531311871b09fd79ed96f45f718753ad5
This commit is contained in:
parent
4e760e276a
commit
bb722f3bff
1 changed files with 53 additions and 0 deletions
53
services/web/scripts/backfill_sso_affiliations.js
Normal file
53
services/web/scripts/backfill_sso_affiliations.js
Normal 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)
|
||||
})
|
Loading…
Reference in a new issue