From e90d4d824b3e75486e98c7498faedbc285876014 Mon Sep 17 00:00:00 2001 From: Erona Date: Mon, 29 Oct 2018 22:33:26 +0800 Subject: [PATCH] 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