@@ -8,82 +8,112 @@ const models = require("../lib/models/");
88const readline = require ( "readline-sync" ) ;
99const minimist = require ( "minimist" ) ;
1010
11- var usage = `
11+ function showUsage ( tips ) {
12+ console . log ( `${ tips }
1213
1314Command-line utility to create users for email-signin.
1415
1516Usage: bin/manage_users [--pass password] (--add | --del) user-email
1617 Options:
1718 --add Add user with the specified user-email
1819 --del Delete user with specified user-email
20+ --reset Reset user password with specified user-email
1921 --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+ }
2134
2235// Using an async function to be able to use await inside
2336async 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" ] } } ) ;
2538 // Cannot create already-existing users
2639 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 ...` ) ;
2841 process . exit ( 1 ) ;
2942 }
3043
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+
3846
3947 // 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 } ) ;
4149 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" ] } ` ) ;
4351 process . exit ( 1 ) ;
4452 } else
45- console . log ( " Created user with email " + argv [ "add" ] ) ;
53+ console . log ( ` Created user with email ${ argv [ "add" ] } ` ) ;
4654}
4755
4856// Using an async function to be able to use await inside
4957async function deleteUser ( argv ) {
5058 // 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` ) ;
5462 process . exit ( 1 ) ;
5563 }
5664
5765 // Sadly .destroy() does not return any success value with all
5866 // backends. See sequelize #4124
5967 await existing_user . destroy ( ) ;
60- console . log ( " Deleted user " + argv [ "del" ] + " ..." ) ;
68+ console . log ( ` Deleted user ${ argv [ "del" ] } ...` ) ;
6169}
6270
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+
6395// Perform commandline-parsing
64- var argv = minimist ( process . argv . slice ( 2 ) ) ;
96+ const argv = minimist ( process . argv . slice ( 2 ) ) ;
6597
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 ' ) } !` ) ;
71105}
72106
73107// 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 } ` ) ;
78114}
79115
80116// 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