@@ -8,82 +8,112 @@ const models = require("../lib/models/");
8
8
const readline = require ( "readline-sync" ) ;
9
9
const minimist = require ( "minimist" ) ;
10
10
11
- var usage = `
11
+ function showUsage ( tips ) {
12
+ console . log ( `${ tips }
12
13
13
14
Command-line utility to create users for email-signin.
14
15
15
16
Usage: bin/manage_users [--pass password] (--add | --del) user-email
16
17
Options:
17
18
--add Add user with the specified user-email
18
19
--del Delete user with specified user-email
20
+ --reset Reset user password with specified user-email
19
21
--pass Use password from cmdline rather than prompting
20
- `
22
+ ` ) ;
23
+ process . exit ( 1 ) ;
24
+ }
25
+
26
+ function getPass ( argv , action ) {
27
+ // Find whether we use cmdline or prompt password
28
+ if ( typeof argv [ "pass" ] !== 'string' ) {
29
+ return readline . question ( `Password for ${ argv [ action ] } :` , { hideEchoBack : true } ) ;
30
+ }
31
+ console . log ( "Using password from commandline..." ) ;
32
+ return argv [ "pass" ] ;
33
+ }
21
34
22
35
// Using an async function to be able to use await inside
23
36
async function createUser ( argv ) {
24
- var existing_user = await models . User . findOne ( { where : { email : argv [ "add" ] } } ) ;
37
+ const existing_user = await models . User . findOne ( { where : { email : argv [ "add" ] } } ) ;
25
38
// Cannot create already-existing users
26
39
if ( existing_user != undefined ) {
27
- console . log ( " User with e-mail " + existing_user . email + " already exists! Aborting ..." ) ;
40
+ console . log ( ` User with e-mail ${ existing_user . email } already exists! Aborting ...` ) ;
28
41
process . exit ( 1 ) ;
29
42
}
30
43
31
- // Find whether we use cmdline or prompt password
32
- if ( argv [ "pass" ] == undefined ) {
33
- var pass = readline . question ( "Password for " + argv [ "add" ] + ":" , { hideEchoBack : true } ) ;
34
- } else {
35
- console . log ( "Using password from commandline..." ) ;
36
- var pass = "" + argv [ "pass" ] ;
37
- }
44
+ const pass = getPass ( argv , "add" ) ;
45
+
38
46
39
47
// Lets try to create, and check success
40
- var ref = await models . User . create ( { email : argv [ "add" ] , password : pass } ) ;
48
+ const ref = await models . User . create ( { email : argv [ "add" ] , password : pass } ) ;
41
49
if ( ref == undefined ) {
42
- console . log ( " Could not create user with email " + argv [ "add" ] ) ;
50
+ console . log ( ` Could not create user with email ${ argv [ "add" ] } ` ) ;
43
51
process . exit ( 1 ) ;
44
52
} else
45
- console . log ( " Created user with email " + argv [ "add" ] ) ;
53
+ console . log ( ` Created user with email ${ argv [ "add" ] } ` ) ;
46
54
}
47
55
48
56
// Using an async function to be able to use await inside
49
57
async function deleteUser ( argv ) {
50
58
// Cannot delete non-existing users
51
- var existing_user = await models . User . findOne ( { where : { email : argv [ "del" ] } } ) ;
52
- if ( existing_user == undefined ) {
53
- console . log ( " User with e-mail " + argv [ "del" ] + " does not exist, cannot delete" ) ;
59
+ const existing_user = await models . User . findOne ( { where : { email : argv [ "del" ] } } ) ;
60
+ if ( existing_user === undefined ) {
61
+ console . log ( ` User with e-mail ${ argv [ "del" ] } does not exist, cannot delete` ) ;
54
62
process . exit ( 1 ) ;
55
63
}
56
64
57
65
// Sadly .destroy() does not return any success value with all
58
66
// backends. See sequelize #4124
59
67
await existing_user . destroy ( ) ;
60
- console . log ( " Deleted user " + argv [ "del" ] + " ..." ) ;
68
+ console . log ( ` Deleted user ${ argv [ "del" ] } ...` ) ;
61
69
}
62
70
71
+
72
+ // Using an async function to be able to use await inside
73
+ async function resetUser ( argv ) {
74
+ const existing_user = await models . User . findOne ( { where : { email : argv [ "reset" ] } } ) ;
75
+ // Cannot reset non-existing users
76
+ if ( existing_user == undefined ) {
77
+ console . log ( `User with e-mail ${ argv [ "reset" ] } does not exist, cannot reset` ) ;
78
+ process . exit ( 1 ) ;
79
+ }
80
+
81
+ const pass = getPass ( argv , "reset" ) ;
82
+
83
+ // set password and save
84
+ existing_user . password = pass ;
85
+ await existing_user . save ( ) ;
86
+ console . log ( `User with email ${ argv [ "reset" ] } password has been reset` ) ;
87
+ }
88
+
89
+ const options = {
90
+ add : createUser ,
91
+ del : deleteUser ,
92
+ reset : resetUser ,
93
+ } ;
94
+
63
95
// Perform commandline-parsing
64
- var argv = minimist ( process . argv . slice ( 2 ) ) ;
96
+ const argv = minimist ( process . argv . slice ( 2 ) ) ;
65
97
66
- // Check for add/delete missing
67
- if ( argv [ "add" ] == undefined && argv [ "del" ] == undefined ) {
68
- console . log ( "You did not specify either --add or --del!" ) ;
69
- console . log ( usage ) ;
70
- process . exit ( 1 ) ;
98
+ const keys = Object . keys ( options ) ;
99
+ const opts = keys . filter ( ( key ) => argv [ key ] !== undefined ) ;
100
+ const action = opts [ 0 ] ;
101
+
102
+ // Check for options missing
103
+ if ( opts . length === 0 ) {
104
+ showUsage ( `You did not specify either ${ keys . map ( ( key ) => `--${ key } ` ) . join ( ' or ' ) } !` ) ;
71
105
}
72
106
73
107
// Check if both are specified
74
- if ( argv [ "add" ] != undefined && argv [ "del" ] != undefined ) {
75
- console . log ( "You cannot add and delete at the same time!" ) ;
76
- console . log ( usage ) ;
77
- process . exit ( 1 ) ;
108
+ if ( opts . length > 1 ) {
109
+ showUsage ( `You cannot ${ action . join ( ' and ' ) } at the same time!` ) ;
110
+ }
111
+ // Check if not string
112
+ if ( typeof argv [ action ] !== 'string' ) {
113
+ showUsage ( `You must follow an email after --${ action } ` ) ;
78
114
}
79
115
80
116
// Call respective processing functions
81
- if ( argv [ "add" ] != undefined ) {
82
- createUser ( argv ) . then ( function ( ) {
83
- process . exit ( 0 ) ;
84
- } ) ;
85
- } else if ( argv [ "del" ] != undefined ) {
86
- deleteUser ( argv ) . then ( function ( ) {
87
- process . exit ( 0 ) ;
88
- } )
89
- }
117
+ options [ action ] ( argv ) . then ( function ( ) {
118
+ process . exit ( 0 ) ;
119
+ } ) ;
0 commit comments