mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 22:23:42 +01:00
Merge pull request #22433 from overleaf/tm-suspend-users-script
Add script for bulk account suspension GitOrigin-RevId: 434ac819c12a2a33c26baf963d8d8874b1864928
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Read a list of user IDs from a file and suspend their accounts.
|
||||
*
|
||||
* Usage: node scripts/suspend_users.mjs <filename>
|
||||
*/
|
||||
import fs from 'node:fs'
|
||||
import { ObjectId } from '../app/src/infrastructure/mongodb.js'
|
||||
import UserUpdater from '../app/src/Features/User/UserUpdater.js'
|
||||
import { promiseMapWithLimit } from '@overleaf/promise-utils'
|
||||
|
||||
const ASYNC_LIMIT = 10
|
||||
|
||||
const processLogger = {
|
||||
failed: [],
|
||||
success: [],
|
||||
printSummary: () => {
|
||||
console.log(
|
||||
{
|
||||
success: processLogger.success,
|
||||
failed: processLogger.failed,
|
||||
},
|
||||
`\nDONE. ${processLogger.success.length} successful. ${processLogger.failed.length} failed to suspend.`
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
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')
|
||||
})
|
||||
}
|
||||
|
||||
async function _handleUser(userId) {
|
||||
try {
|
||||
await UserUpdater.promises.suspendUser(userId, {
|
||||
ip: '0.0.0.0',
|
||||
info: { script: true },
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(`Failed to suspend ${userId}`, error)
|
||||
processLogger.failed.push(userId)
|
||||
return
|
||||
}
|
||||
processLogger.success.push(userId)
|
||||
}
|
||||
|
||||
async function _loopUsers(userIds) {
|
||||
return promiseMapWithLimit(ASYNC_LIMIT, userIds, _handleUser)
|
||||
}
|
||||
|
||||
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 suspend_users script---')
|
||||
_validateUserIdList(userIds)
|
||||
console.log(`---Starting to process ${userIds.length} users---`)
|
||||
await _loopUsers(userIds)
|
||||
|
||||
processLogger.printSummary()
|
||||
process.exit()
|
||||
}
|
||||
|
||||
processUsers(userIds)
|
||||
Reference in New Issue
Block a user