From 7b12945c498464370f6f482c273a4ac535da74e8 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:03:05 +0800 Subject: [PATCH 1/6] refactor(bin): refactor check args in bin/manage_users Signed-off-by: Erona --- bin/manage_users | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bin/manage_users b/bin/manage_users index 74ac181..8c61525 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -60,30 +60,33 @@ async function deleteUser(argv) { console.log("Deleted user "+argv["del"]+" ..."); } +var options = { + add: createUser, + del: deleteUser, +}; + // Perform commandline-parsing var argv = minimist(process.argv.slice(2)); -// Check for add/delete missing -if (argv["add"] == undefined && argv["del"] == undefined) { - console.log("You did not specify either --add or --del!"); +var keys = Object.keys(options); +var opts = keys.filter((key) => argv[key] !== undefined); +var action = opts[0]; + +// Check for options missing +if (opts.length === 0) { + console.log(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`); console.log(usage); process.exit(1); } // Check if both are specified -if (argv["add"] != undefined && argv["del"] != undefined) { - console.log("You cannot add and delete at the same time!"); +if (opts.length > 1) { + console.log(`You cannot ${action.join(' and ')} at the same time!`); console.log(usage); process.exit(1); } // Call respective processing functions -if (argv["add"] != undefined) { - createUser(argv).then(function() { - process.exit(0); - }); -} else if (argv["del"] != undefined) { - deleteUser(argv).then(function() { - process.exit(0); - }) -} +options[action](argv).then(function() { + process.exit(0); +}); From 2f82e0c86af60664372b1d9008fae0284c037557 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:11:43 +0800 Subject: [PATCH 2/6] refactor(bin): add function showUsage to refactor usage things Signed-off-by: Erona --- bin/manage_users | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bin/manage_users b/bin/manage_users index 8c61525..30b4632 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -8,7 +8,8 @@ const models = require("../lib/models/"); const readline = require("readline-sync"); const minimist = require("minimist"); -var usage = ` +function showUsage(tips) { + console.log(`${tips} Command-line utility to create users for email-signin. @@ -17,7 +18,9 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email --add Add user with the specified user-email --del Delete user with specified user-email --pass Use password from cmdline rather than prompting -` +`); + process.exit(1); +} // Using an async function to be able to use await inside async function createUser(argv) { @@ -74,16 +77,12 @@ var action = opts[0]; // Check for options missing if (opts.length === 0) { - console.log(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`); - console.log(usage); - process.exit(1); + showUsage(`You did not specify either ${keys.map((key) => `--${key}`).join(' or ')}!`); } // Check if both are specified if (opts.length > 1) { - console.log(`You cannot ${action.join(' and ')} at the same time!`); - console.log(usage); - process.exit(1); + showUsage(`You cannot ${action.join(' and ')} at the same time!`); } // Call respective processing functions From 63626b1267e77ad792f426b4e0a8e4efac4b2c95 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:19:39 +0800 Subject: [PATCH 3/6] refactor(bin): eliminate `var` and use template string refactor string things Signed-off-by: Erona --- bin/manage_users | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/bin/manage_users b/bin/manage_users index 30b4632..f748e34 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -24,43 +24,44 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email // Using an async function to be able to use await inside async function createUser(argv) { - var existing_user = await models.User.findOne({where: {email: argv["add"]}}); + const existing_user = await models.User.findOne({where: {email: argv["add"]}}); // Cannot create already-existing users if(existing_user != undefined) { - console.log("User with e-mail "+existing_user.email+" already exists! Aborting ..."); + console.log(`User with e-mail ${existing_user.email} already exists! Aborting ...`); process.exit(1); } // Find whether we use cmdline or prompt password + let pass; if(argv["pass"] == undefined) { - var pass = readline.question("Password for "+argv["add"]+":", {hideEchoBack: true}); + pass = readline.question(`Password for ${argv["add"]}:`, {hideEchoBack: true}); } else { console.log("Using password from commandline..."); - var pass = "" + argv["pass"]; + pass = "" + argv["pass"]; } // Lets try to create, and check success - var ref = await models.User.create({email: argv["add"], password: pass}); + const ref = await models.User.create({email: argv["add"], password: pass}); if(ref == undefined) { - console.log("Could not create user with email "+argv["add"]); + console.log(`Could not create user with email ${argv["add"]}`); process.exit(1); } else - console.log("Created user with email "+argv["add"]); + console.log(`Created user with email ${argv["add"]}`); } // Using an async function to be able to use await inside async function deleteUser(argv) { // Cannot delete non-existing users - var existing_user = await models.User.findOne({where: {email: argv["del"]}}); - if(existing_user == undefined) { - console.log("User with e-mail "+argv["del"]+" does not exist, cannot delete"); + const existing_user = await models.User.findOne({where: {email: argv["del"]}}); + if(existing_user === undefined) { + console.log(`User with e-mail ${argv["del"]} does not exist, cannot delete`); process.exit(1); } // Sadly .destroy() does not return any success value with all // backends. See sequelize #4124 await existing_user.destroy(); - console.log("Deleted user "+argv["del"]+" ..."); + console.log(`Deleted user ${argv["del"]} ...`); } var options = { @@ -69,11 +70,11 @@ var options = { }; // Perform commandline-parsing -var argv = minimist(process.argv.slice(2)); +const argv = minimist(process.argv.slice(2)); -var keys = Object.keys(options); -var opts = keys.filter((key) => argv[key] !== undefined); -var action = opts[0]; +const keys = Object.keys(options); +const opts = keys.filter((key) => argv[key] !== undefined); +const action = opts[0]; // Check for options missing if (opts.length === 0) { From 79842b82e8c8383ed22fce1edfe1035d5a02fc18 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:32:14 +0800 Subject: [PATCH 4/6] refactor(bin): add function getPass in bin/manage_users Signed-off-by: Erona --- bin/manage_users | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/bin/manage_users b/bin/manage_users index f748e34..0d99f36 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -22,6 +22,15 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email process.exit(1); } +function getPass(argv, action) { + // Find whether we use cmdline or prompt password + if(typeof argv["pass"] !== 'string') { + return readline.question(`Password for ${argv[action]}:`, {hideEchoBack: true}); + } + console.log("Using password from commandline..."); + return argv["pass"]; +} + // Using an async function to be able to use await inside async function createUser(argv) { const existing_user = await models.User.findOne({where: {email: argv["add"]}}); @@ -31,14 +40,8 @@ async function createUser(argv) { process.exit(1); } - // Find whether we use cmdline or prompt password - let pass; - if(argv["pass"] == undefined) { - pass = readline.question(`Password for ${argv["add"]}:`, {hideEchoBack: true}); - } else { - console.log("Using password from commandline..."); - pass = "" + argv["pass"]; - } + const pass = getPass(argv, "add"); + // Lets try to create, and check success const ref = await models.User.create({email: argv["add"], password: pass}); From e90d4d824b3e75486e98c7498faedbc285876014 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:33:26 +0800 Subject: [PATCH 5/6] feat(bin): add option --reset to reset user password Signed-off-by: Erona --- bin/manage_users | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bin/manage_users b/bin/manage_users index 0d99f36..b23a0c5 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -17,6 +17,7 @@ Usage: bin/manage_users [--pass password] (--add | --del) user-email Options: --add Add user with the specified user-email --del Delete user with specified user-email + --reset Reset user password with specified user-email --pass Use password from cmdline rather than prompting `); process.exit(1); @@ -67,9 +68,28 @@ async function deleteUser(argv) { console.log(`Deleted user ${argv["del"]} ...`); } -var options = { + +// Using an async function to be able to use await inside +async function resetUser(argv) { + const existing_user = await models.User.findOne({where: {email: argv["reset"]}}); + // Cannot reset non-existing users + if(existing_user == undefined) { + console.log(`User with e-mail ${argv["reset"]} does not exist, cannot reset`); + process.exit(1); + } + + const pass = getPass(argv, "reset"); + + // set password and save + existing_user.password = pass; + await existing_user.save(); + console.log(`User with email ${argv["reset"]} password has been reset`); +} + +const options = { add: createUser, del: deleteUser, + reset: resetUser, }; // Perform commandline-parsing From 3abf1f04eda60532a0ff3b7b5b2fab783c0e6f44 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 23:10:44 +0800 Subject: [PATCH 6/6] feat(bin): ensure email exists Signed-off-by: Erona --- bin/manage_users | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/manage_users b/bin/manage_users index b23a0c5..8c35620 100755 --- a/bin/manage_users +++ b/bin/manage_users @@ -108,6 +108,10 @@ if (opts.length === 0) { if (opts.length > 1) { showUsage(`You cannot ${action.join(' and ')} at the same time!`); } +// Check if not string +if (typeof argv[action] !== 'string') { + showUsage(`You must follow an email after --${action}`); +} // Call respective processing functions options[action](argv).then(function() {