From d3cc3edf4eeb7b13c4c65f55cf61cdd09d034044 Mon Sep 17 00:00:00 2001 From: Jessica Lawshe Date: Thu, 3 Jun 2021 12:00:54 -0500 Subject: [PATCH] Merge pull request #4146 from overleaf/jel-em-script-must-reconfirm Add script to clear users' sessions and set must_reconfirm GitOrigin-RevId: 87f85613c6eaf66298eb48e3ddb8eed12d73dd3e --- .../clear_sessions_set_must_reconfirm.js | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 services/web/scripts/clear_sessions_set_must_reconfirm.js diff --git a/services/web/scripts/clear_sessions_set_must_reconfirm.js b/services/web/scripts/clear_sessions_set_must_reconfirm.js new file mode 100644 index 0000000000..ca134957e5 --- /dev/null +++ b/services/web/scripts/clear_sessions_set_must_reconfirm.js @@ -0,0 +1,82 @@ +const fs = require('fs') +const { ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb') +const async = require('async') +const UserUpdater = require('../app/src/Features/User/UserUpdater') +const UserSessionsManager = require('../app/src/Features/User/UserSessionsManager') + +const ASYNC_LIMIT = 10 + +const processLogger = { + failedClear: [], + failedSet: [], + success: [], + printSummary: () => { + console.log( + { + success: processLogger.success, + failedClear: processLogger.failedClear, + failedSet: processLogger.failedSet, + }, + `\nDONE. ${processLogger.success.length} successful. ${processLogger.failedClear.length} failed to clear sessions. ${processLogger.failedSet.length} failed to set must_reconfirm.` + ) + }, +} + +function _validateUserIdList(userIds) { + if (!Array.isArray(userIds)) throw new Error('users is not an array') + + userIds.forEach(userId => { + if (!ObjectId.isValid(userId)) throw new Error('user ID not valid') + }) +} + +function _handleUser(userId, callback) { + UserUpdater.updateUser(userId, { $set: { must_reconfirm: true } }, error => { + if (error) { + console.log(`Failed to set must_reconfirm ${userId}`, error) + processLogger.failedSet.push(userId) + return callback() + } else { + UserSessionsManager.revokeAllUserSessions({ _id: userId }, [], error => { + if (error) { + console.log(`Failed to clear sessions for ${userId}`, error) + processLogger.failedClear.push(userId) + } else { + processLogger.success.push(userId) + } + return callback() + }) + } + }) +} + +async function _loopUsers(userIds) { + await new Promise((resolve, reject) => { + async.eachLimit(userIds, ASYNC_LIMIT, _handleUser, error => { + if (error) return reject(error) + resolve() + }) + }) +} + +const fileName = process.argv[2] +if (!fileName) throw new Error('missing filename') +const usersFile = fs.readFileSync(fileName, 'utf8') +const userIds = usersFile + .trim() + .split('\n') + .map(id => id.trim()) + +async function processUsers(userIds) { + console.log('---Starting set_must_reconfirm script---') + await waitForDb() + + _validateUserIdList(userIds) + console.log(`---Starting to process ${userIds.length} users---`) + await _loopUsers(userIds) + + processLogger.printSummary() + process.exit() +} + +processUsers(userIds)