Merge pull request #4090 from supertassu/scripable-user-password-change

Make `add` and `reset` CLI commands scriptable
This commit is contained in:
Pavel Djundik 2020-10-07 10:03:37 +03:00 committed by GitHub
commit 0322c043e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 16 deletions

View File

@ -11,7 +11,9 @@ program
.command("add <name>") .command("add <name>")
.description("Add a new user") .description("Add a new user")
.on("--help", Utils.extraHelp) .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())) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
@ -31,6 +33,11 @@ program
return; return;
} }
if (cmdObj.password) {
add(manager, name, cmdObj.password, !!cmdObj.saveLogs);
return;
}
log.prompt( log.prompt(
{ {
text: "Enter password:", text: "Enter password:",

View File

@ -11,7 +11,8 @@ program
.command("reset <name>") .command("reset <name>")
.description("Reset user password") .description("Reset user password")
.on("--help", Utils.extraHelp) .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())) { if (!fs.existsSync(Helper.getUsersPath())) {
log.error(`${Helper.getUsersPath()} does not exist.`); log.error(`${Helper.getUsersPath()} does not exist.`);
return; return;
@ -30,9 +31,10 @@ program
return; return;
} }
const pathReal = Helper.getUserConfigPath(name); if (cmdObj.password) {
const pathTemp = pathReal + ".tmp"; change(name, cmdObj.password);
const user = JSON.parse(fs.readFileSync(pathReal, "utf-8")); return;
}
log.prompt( log.prompt(
{ {
@ -44,17 +46,25 @@ program
return; return;
} }
user.password = Helper.password.hash(password); change(name, 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)}.`);
} }
); );
}); });
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)}.`);
}