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
+
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 Promise . all ( users . map ( async ( user ) => {
28
+ let userId = user . _id ;
29
+ await countlyDb . collection ( "members" ) . findAndModify (
30
+ { _id : userId } ,
31
+ { } ,
32
+ {
33
+ $set : { "two_factor_auth.enabled" : false } ,
34
+ $unset : { "two_factor_auth.secret_token" : "" }
35
+ }
36
+ ) ;
37
+ console . log ( "2FA removed: " , JSON . stringify ( user ) ) ;
38
+ } ) ) ;
39
+ console . log ( "All done" ) ;
40
+ }
41
+ }
42
+ catch ( error ) {
43
+ console . log ( "ERROR: " ) ;
44
+ console . log ( error ) ;
45
+ }
46
+ finally {
47
+ countlyDb . close ( ) ;
48
+ }
49
+ } ) ;
50
+
51
+ function getUsers ( db , emails ) {
52
+ const query = { } ;
53
+ if ( emails ?. length ) {
54
+ query . email = {
55
+ $in : emails
56
+ } ;
57
+ }
58
+ return db . collection ( 'members' ) . find ( query , {
59
+ projection : { _id : 1 , email : 1 }
60
+ } ) . toArray ( ) ;
61
+ }
0 commit comments