Skip to content

Commit 530ced0

Browse files
Merge pull request #5856 from Countly/johnweak-next
Script to remove 2FA from user(s)
2 parents e756bf3 + f132020 commit 530ced0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Script should be placed in ./bin/scripts/member-managament/disable_2fa.js
3+
4+
Script is used to disable user(s) 2FA by email.
5+
*/
6+
7+
var pluginManager = require('../../../plugins/pluginManager.js');
8+
9+
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.
10+
//const EMAILS = ["[email protected]", "[email protected]"];
11+
const EMAILS = [''];
12+
13+
if (dry_run) {
14+
console.log("This is a dry run");
15+
console.log("Members will only be listed, 2FA will not be disabled");
16+
}
17+
18+
pluginManager.dbConnection().then(async(countlyDb) => {
19+
try {
20+
// Find the users by email
21+
let users = [];
22+
users = await getUsers(countlyDb, EMAILS);
23+
24+
console.log(`The following ${users.length} user(s) 2FA will be disabled: `);
25+
console.log(JSON.stringify(users));
26+
if (!dry_run) {
27+
await countlyDb.collection('members').updateMany({_id: {$in: users.map(user=>user._id)}},
28+
{
29+
$set: {"two_factor_auth.enabled": false},
30+
$unset: {"two_factor_auth.secret_token": ""}
31+
});
32+
console.log("All done");
33+
}
34+
}
35+
catch (error) {
36+
console.log("ERROR: ");
37+
console.log(error);
38+
}
39+
finally {
40+
countlyDb.close();
41+
}
42+
});
43+
44+
function getUsers(db, emails) {
45+
const query = {};
46+
if (emails?.length) {
47+
query.email = {
48+
$in: emails
49+
};
50+
}
51+
return db.collection('members').find(query, {
52+
projection: { _id: 1, email: 1 }
53+
}).toArray();
54+
}

0 commit comments

Comments
 (0)