-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5856 from Countly/johnweak-next
Script to remove 2FA from user(s)
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Script should be placed in ./bin/scripts/member-managament/disable_2fa.js | ||
Script is used to disable user(s) 2FA by email. | ||
*/ | ||
|
||
var pluginManager = require('../../../plugins/pluginManager.js'); | ||
|
||
const dry_run = false; //if set true, there will be only information outputted about users in the email list, but 2FA disable operation will not be triggered. | ||
//const EMAILS = ["[email protected]", "[email protected]"]; | ||
const EMAILS = ['']; | ||
|
||
if (dry_run) { | ||
console.log("This is a dry run"); | ||
console.log("Members will only be listed, 2FA will not be disabled"); | ||
} | ||
|
||
pluginManager.dbConnection().then(async(countlyDb) => { | ||
try { | ||
// Find the users by email | ||
let users = []; | ||
users = await getUsers(countlyDb, EMAILS); | ||
|
||
console.log(`The following ${users.length} user(s) 2FA will be disabled: `); | ||
console.log(JSON.stringify(users)); | ||
if (!dry_run) { | ||
await countlyDb.collection('members').updateMany({_id: {$in: users.map(user=>user._id)}}, | ||
{ | ||
$set: {"two_factor_auth.enabled": false}, | ||
$unset: {"two_factor_auth.secret_token": ""} | ||
}); | ||
console.log("All done"); | ||
} | ||
} | ||
catch (error) { | ||
console.log("ERROR: "); | ||
console.log(error); | ||
} | ||
finally { | ||
countlyDb.close(); | ||
} | ||
}); | ||
|
||
function getUsers(db, emails) { | ||
const query = {}; | ||
if (emails?.length) { | ||
query.email = { | ||
$in: emails | ||
}; | ||
} | ||
return db.collection('members').find(query, { | ||
projection: { _id: 1, email: 1 } | ||
}).toArray(); | ||
} |