Skip to content

Commit

Permalink
Merge pull request #5856 from Countly/johnweak-next
Browse files Browse the repository at this point in the history
Script to remove 2FA from user(s)
  • Loading branch information
ArtursKadikis authored Dec 11, 2024
2 parents e756bf3 + f132020 commit 530ced0
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions bin/scripts/member-managament/disable_2fa.js
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();
}

0 comments on commit 530ced0

Please sign in to comment.