From e790a72e59e1df17dfd9f982a25cd7841007639c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Taavi=20V=C3=A4=C3=A4n=C3=A4nen?= Date: Sun, 4 Oct 2020 18:03:26 +0300 Subject: [PATCH] Make `add` and `reset` CLI commands scriptable Add CLI options `--password` and `--save-logs` (for `add` only) in order to make adding users and changing user passwords scriptable. Closes #3913 --- src/command-line/users/add.js | 9 +++++++- src/command-line/users/reset.js | 40 ++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/command-line/users/add.js b/src/command-line/users/add.js index f1975d98..4f3b9ba7 100644 --- a/src/command-line/users/add.js +++ b/src/command-line/users/add.js @@ -11,7 +11,9 @@ program .command("add ") .description("Add a new user") .on("--help", Utils.extraHelp) - .action(function (name) { + .option("--password [password]", "new password, will be prompted if not specified") + .option("--save-logs", "if password is specified, this enables saving logs to disk") + .action(function (name, cmdObj) { if (!fs.existsSync(Helper.getUsersPath())) { log.error(`${Helper.getUsersPath()} does not exist.`); return; @@ -31,6 +33,11 @@ program return; } + if (cmdObj.password) { + add(manager, name, cmdObj.password, !!cmdObj.saveLogs); + return; + } + log.prompt( { text: "Enter password:", diff --git a/src/command-line/users/reset.js b/src/command-line/users/reset.js index a5f57dc3..f7c1cbac 100644 --- a/src/command-line/users/reset.js +++ b/src/command-line/users/reset.js @@ -11,7 +11,8 @@ program .command("reset ") .description("Reset user password") .on("--help", Utils.extraHelp) - .action(function (name) { + .option("--password [password]", "new password, will be prompted if not specified") + .action(function (name, cmdObj) { if (!fs.existsSync(Helper.getUsersPath())) { log.error(`${Helper.getUsersPath()} does not exist.`); return; @@ -30,9 +31,10 @@ program return; } - const pathReal = Helper.getUserConfigPath(name); - const pathTemp = pathReal + ".tmp"; - const user = JSON.parse(fs.readFileSync(pathReal, "utf-8")); + if (cmdObj.password) { + change(name, cmdObj.password); + return; + } log.prompt( { @@ -44,17 +46,25 @@ program return; } - user.password = Helper.password.hash(password); - user.sessions = {}; - - const newUser = JSON.stringify(user, null, "\t"); - - // Write to a temp file first, in case the write fails - // we do not lose the original file (for example when disk is full) - fs.writeFileSync(pathTemp, newUser); - fs.renameSync(pathTemp, pathReal); - - log.info(`Successfully reset password for ${colors.bold(name)}.`); + change(name, password); } ); }); + +function change(name, password) { + const pathReal = Helper.getUserConfigPath(name); + const pathTemp = pathReal + ".tmp"; + const user = JSON.parse(fs.readFileSync(pathReal, "utf-8")); + + user.password = Helper.password.hash(password); + user.sessions = {}; + + const newUser = JSON.stringify(user, null, "\t"); + + // Write to a temp file first, in case the write fails + // we do not lose the original file (for example when disk is full) + fs.writeFileSync(pathTemp, newUser); + fs.renameSync(pathTemp, pathReal); + + log.info(`Successfully reset password for ${colors.bold(name)}.`); +}