2022-06-19 00:25:21 +00:00
|
|
|
import log from "../../log";
|
|
|
|
import colors from "chalk";
|
|
|
|
import {Command} from "commander";
|
|
|
|
import fs from "fs";
|
|
|
|
import Helper from "../../helper";
|
|
|
|
import Config from "../../config";
|
|
|
|
import Utils from "../utils";
|
2014-09-11 21:00:08 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const program = new Command("reset");
|
2014-09-11 21:00:08 +00:00
|
|
|
program
|
|
|
|
.description("Reset user password")
|
2017-08-21 05:49:32 +00:00
|
|
|
.on("--help", Utils.extraHelp)
|
2022-06-19 00:25:21 +00:00
|
|
|
.argument("<name>", "name of the user")
|
2020-10-04 15:03:26 +00:00
|
|
|
.option("--password [password]", "new password, will be prompted if not specified")
|
|
|
|
.action(function (name, cmdObj) {
|
2022-05-01 19:12:39 +00:00
|
|
|
if (!fs.existsSync(Config.getUsersPath())) {
|
|
|
|
log.error(`${Config.getUsersPath()} does not exist.`);
|
2017-07-18 09:36:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
2022-06-21 07:51:24 +00:00
|
|
|
const ClientManager = require("../../clientManager").default;
|
2017-12-07 02:28:21 +00:00
|
|
|
const users = new ClientManager().getUsers();
|
2017-08-23 05:42:59 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (users === undefined) {
|
|
|
|
// There was an error, already logged
|
2017-08-23 05:42:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:39:32 +00:00
|
|
|
if (!users.includes(name)) {
|
2016-12-15 06:13:43 +00:00
|
|
|
log.error(`User ${colors.bold(name)} does not exist.`);
|
2014-09-11 21:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2020-10-04 15:03:26 +00:00
|
|
|
if (cmdObj.password) {
|
|
|
|
change(name, cmdObj.password);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-15 14:56:17 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
log.prompt(
|
|
|
|
{
|
|
|
|
text: "Enter new password:",
|
|
|
|
silent: true,
|
|
|
|
},
|
2020-03-21 20:55:36 +00:00
|
|
|
function (err, password) {
|
2019-07-17 09:33:59 +00:00
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2020-10-04 15:03:26 +00:00
|
|
|
change(name, password);
|
2019-07-17 09:33:59 +00:00
|
|
|
}
|
|
|
|
);
|
2014-09-11 21:00:08 +00:00
|
|
|
});
|
2020-10-04 15:03:26 +00:00
|
|
|
|
|
|
|
function change(name, password) {
|
2022-05-01 19:12:39 +00:00
|
|
|
const pathReal = Config.getUserConfigPath(name);
|
2020-10-04 15:03:26 +00:00
|
|
|
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)
|
2022-04-12 00:47:22 +00:00
|
|
|
fs.writeFileSync(pathTemp, newUser, {
|
|
|
|
mode: 0o600,
|
|
|
|
});
|
2020-10-04 15:03:26 +00:00
|
|
|
fs.renameSync(pathTemp, pathReal);
|
|
|
|
|
|
|
|
log.info(`Successfully reset password for ${colors.bold(name)}.`);
|
|
|
|
}
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
export default program;
|