2017-01-30 08:36:06 -05:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
mongojs = require("mongojs")
|
|
|
|
ObjectId = mongojs.ObjectId
|
2017-06-14 12:03:05 -04:00
|
|
|
db = mongojs(Settings.mongo.url, ['users', 'projects', 'subscriptions'])
|
2017-01-30 08:36:06 -05:00
|
|
|
async = require "async"
|
|
|
|
|
2017-01-30 09:40:31 -05:00
|
|
|
module.exports = HoldingAccountMigration =
|
|
|
|
DRY_RUN: true
|
2017-01-30 08:36:06 -05:00
|
|
|
|
2017-01-30 09:40:31 -05:00
|
|
|
findHoldingAccounts: (callback = (error, users) ->) ->
|
2017-03-24 09:46:31 -04:00
|
|
|
db.users.find({holdingAccount: true, hashedPassword: { $exists: false }}, {holdingAccount: 1, email: 1}, callback)
|
2017-01-30 08:36:06 -05:00
|
|
|
|
2017-01-30 09:40:31 -05:00
|
|
|
deleteUserProjects: (user_id, callback = (error) ->) ->
|
|
|
|
# Holding accounts can't own projects, so only remove from
|
|
|
|
# collaberator_refs and readOnly_refs
|
|
|
|
console.log "[Removing user from projects]", user_id
|
|
|
|
db.projects.find {
|
|
|
|
$or: [
|
|
|
|
{collaberator_refs: user_id},
|
|
|
|
{readOnly_refs: user_id}
|
|
|
|
]
|
|
|
|
}, { collaberator_refs: 1, readOnly_refs: 1 }, (error, projects = []) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
jobs = projects.map (project) ->
|
|
|
|
(cb) ->
|
|
|
|
console.log "[Removing user from project]", user_id, JSON.stringify(project)
|
2017-03-23 07:34:45 -04:00
|
|
|
if !project?._id?
|
2017-01-30 09:40:31 -05:00
|
|
|
throw new Error("no project id")
|
|
|
|
|
|
|
|
if !HoldingAccountMigration.DRY_RUN
|
|
|
|
db.projects.update {
|
|
|
|
_id: project._id
|
|
|
|
}, {
|
|
|
|
$pull: {
|
|
|
|
collaberator_refs: user_id,
|
|
|
|
readOnly_refs: user_id
|
|
|
|
}
|
|
|
|
}, (error, result) ->
|
|
|
|
return cb(error) if error?
|
|
|
|
console.log "[Removed user from project]", user_id, project._id, result
|
|
|
|
cb()
|
|
|
|
else
|
|
|
|
console.log "[Would have removed user from project]", user_id, project._id
|
|
|
|
cb()
|
|
|
|
|
|
|
|
async.series jobs, callback
|
2017-01-30 08:36:06 -05:00
|
|
|
|
2017-01-30 09:40:31 -05:00
|
|
|
deleteUser: (user_id, callback = (error) ->) ->
|
|
|
|
if !user_id?
|
|
|
|
throw new Error("must have user_id")
|
2017-03-23 07:34:45 -04:00
|
|
|
if !HoldingAccountMigration.DRY_RUN
|
|
|
|
db.users.remove {_id: user_id, holdingAccount: true}, (error, result) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
console.log "[Removed user]", user_id, result
|
|
|
|
if result.n != 1
|
|
|
|
return callback(new Error("failed to remove user as expected"))
|
2017-01-30 09:40:31 -05:00
|
|
|
callback()
|
2017-03-23 07:34:45 -04:00
|
|
|
else
|
|
|
|
console.log "[Would have removed user]", user_id
|
|
|
|
callback()
|
2017-06-14 12:03:05 -04:00
|
|
|
|
|
|
|
migrateGroupInvites: (user_id, email, callback = (error) ->) ->
|
|
|
|
if !user_id?
|
|
|
|
throw new Error("must have user_id")
|
|
|
|
if !HoldingAccountMigration.DRY_RUN
|
|
|
|
db.subscriptions.update {member_ids: user_id}, {
|
|
|
|
$pull: { member_ids: user_id },
|
|
|
|
$addToSet : { invited_emails: email }
|
|
|
|
}, { multi : true }, (error, result) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
console.log "[Migrated user in group accounts]", user_id, email, result
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
console.log "[Would have migrated user in group accounts]", user_id, email
|
|
|
|
callback()
|
2017-01-30 09:40:31 -05:00
|
|
|
|
2017-03-23 07:34:45 -04:00
|
|
|
run: (done = () ->) ->
|
2017-03-23 07:50:46 -04:00
|
|
|
console.log "[Getting list of holding accounts]"
|
2017-01-30 09:40:31 -05:00
|
|
|
HoldingAccountMigration.findHoldingAccounts (error, users) ->
|
2017-01-30 08:36:06 -05:00
|
|
|
throw error if error?
|
2017-03-23 07:50:46 -04:00
|
|
|
console.log "[Got #{users.length} holding accounts]"
|
2017-03-23 07:56:46 -04:00
|
|
|
i = 0
|
2017-01-30 09:40:31 -05:00
|
|
|
jobs = users.map (u) ->
|
|
|
|
(cb) ->
|
2017-03-23 07:56:46 -04:00
|
|
|
console.log "[Removing user #{i++}/#{users.length}]"
|
2017-06-14 12:03:05 -04:00
|
|
|
HoldingAccountMigration.migrateGroupInvites u._id, u.email, (error) ->
|
2017-01-30 09:40:31 -05:00
|
|
|
return cb(error) if error?
|
2017-06-14 12:03:05 -04:00
|
|
|
HoldingAccountMigration.deleteUser u._id, (error) ->
|
2017-03-23 07:34:45 -04:00
|
|
|
return cb(error) if error?
|
2017-06-14 12:03:05 -04:00
|
|
|
HoldingAccountMigration.deleteUserProjects u._id, (error) ->
|
|
|
|
return cb(error) if error?
|
|
|
|
setTimeout cb, 50 # Small delay to not hammer DB
|
2017-01-30 09:40:31 -05:00
|
|
|
async.series jobs, (error) ->
|
|
|
|
throw error if error?
|
2017-03-23 07:34:45 -04:00
|
|
|
console.log "[FINISHED]"
|
2017-01-30 09:40:31 -05:00
|
|
|
done()
|
|
|
|
|
|
|
|
migrate: (client, done=()->) ->
|
|
|
|
HoldingAccountMigration.DRY_RUN = false
|
|
|
|
HoldingAccountMigration.run(done)
|
|
|
|
|
|
|
|
rollback: (client, done) ->
|
|
|
|
done()
|