Merge pull request #1766 from overleaf/ta-dropbox-duplicates-script

Add Script to Unlink all Duplicate Dropbox uids

GitOrigin-RevId: 6db73dee9f04e9006d0aec558cf370b5079df671
This commit is contained in:
Timothée Alby 2019-05-15 09:19:03 +02:00 committed by sharelatex
parent bd722dda8e
commit 7e32ed5158
2 changed files with 83 additions and 0 deletions

View file

@ -130,6 +130,17 @@ templates.verifyEmailToJoinTeam = CTAEmailTemplate({
ctaURL: (opts) -> opts.acceptInviteUrl ctaURL: (opts) -> opts.acceptInviteUrl
}) })
templates.dropboxUnlinkedDuplicate = CTAEmailTemplate({
subject: () -> "Your Dropbox Account has been Unlinked - #{settings.appName}"
message: (opts) -> """
Our automated systems have detected that your Dropbox account was linked to more than one Overleaf accounts. This should not have been allowed and might be causing issues with the Dropbox sync feature.
We have now unlinked all your Dropbox and Overleaf Accounts. To ensure your project will keep syncing you can link your Dropbox account to the Overleaf account of your choice now.
"""
ctaText: () -> "Link Dropbox Account"
ctaURL: (opts) -> "#{settings.siteUrl}/user/settings"
})
templates.testEmail = CTAEmailTemplate({ templates.testEmail = CTAEmailTemplate({
subject: () -> "A Test Email from #{settings.appName}" subject: () -> "A Test Email from #{settings.appName}"
title: () -> "A Test Email from #{settings.appName}" title: () -> "A Test Email from #{settings.appName}"

View file

@ -0,0 +1,72 @@
const { db } = require('../../app/js/infrastructure/mongojs')
const DropboxHandler = require('../../modules/dropbox/app/js/DropboxHandler')
const EmailHandler = require('../../app/js/Features/Email/EmailHandler')
const async = require('async')
const minimist = require('minimist')
const argv = minimist(process.argv.slice(2))
const commit = argv.commit !== undefined
if (!commit) {
console.log('DOING DRY RUN. TO SAVE CHANGES PASS --commit')
}
db.users.aggregate(
[
{
$group: {
// group by Dropbox access token uid and count distinct users
_id: '$dropbox.access_token.uid',
count: { $sum: 1 },
_ids: { $addToSet: '$_id' }
}
},
{
$match: {
// select only uids userd more than once
_id: { $ne: null },
count: { $gt: 1 }
}
},
{
$project: {
// filter output
_id: false,
dropbox_uid: '$_id',
_ids: '$_ids'
}
}
],
{ allowDiskUse: true },
function(error, results) {
if (error) throw error
console.log('FOUND ' + results.length + ' DUPLICATES')
async.mapSeries(results, removeDuplicates, function(error) {
if (error) throw error
console.log('DONE')
process.exit()
})
}
)
function removeDuplicates(duplicate, callback) {
async.mapSeries(duplicate._ids, unlinkUser, function(error) {
callback(error)
})
}
function unlinkUser(_id, callback) {
db.users.findOne({ _id: _id }, function(error, user) {
if (error) return callback(error)
console.log('UNLINKING USER ' + _id + ' (' + user.email + ')')
if (!commit) return callback()
DropboxHandler.unlinkAccount(_id, function(error) {
if (error) return callback(error)
EmailHandler.sendEmail(
'dropboxUnlinkedDuplicate',
{ to: user.email },
callback
)
})
})
}