2016-10-09 21:14:02 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 22:31:06 +02:00
|
|
|
const log = require("../../log");
|
2018-03-02 19:28:54 +01:00
|
|
|
const colors = require("chalk");
|
2017-07-18 11:36:24 +02:00
|
|
|
const program = require("commander");
|
|
|
|
const fs = require("fs");
|
2017-12-07 03:23:46 +01:00
|
|
|
const Helper = require("../../helper");
|
|
|
|
const Utils = require("../utils");
|
2014-09-11 23:00:08 +02:00
|
|
|
|
|
|
|
program
|
|
|
|
.command("reset <name>")
|
|
|
|
.description("Reset user password")
|
2017-08-21 07:49:32 +02:00
|
|
|
.on("--help", Utils.extraHelp)
|
2020-10-04 17:03:26 +02:00
|
|
|
.option("--password [password]", "new password, will be prompted if not specified")
|
|
|
|
.action(function (name, cmdObj) {
|
2017-12-07 04:54:54 +01:00
|
|
|
if (!fs.existsSync(Helper.getUsersPath())) {
|
|
|
|
log.error(`${Helper.getUsersPath()} does not exist.`);
|
2017-07-18 11:36:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:23:46 +01:00
|
|
|
const ClientManager = require("../../clientManager");
|
2017-12-07 03:28:21 +01:00
|
|
|
const users = new ClientManager().getUsers();
|
2017-08-23 07:42:59 +02:00
|
|
|
|
2019-07-17 11:33:59 +02:00
|
|
|
if (users === undefined) {
|
|
|
|
// There was an error, already logged
|
2017-08-23 07:42:59 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 07:39:32 +01:00
|
|
|
if (!users.includes(name)) {
|
2016-12-15 07:13:43 +01:00
|
|
|
log.error(`User ${colors.bold(name)} does not exist.`);
|
2014-09-11 23:00:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-02-20 08:28:04 +01:00
|
|
|
|
2020-10-04 17:03:26 +02:00
|
|
|
if (cmdObj.password) {
|
|
|
|
change(name, cmdObj.password);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-15 15:56:17 +01:00
|
|
|
|
2019-07-17 11:33:59 +02:00
|
|
|
log.prompt(
|
|
|
|
{
|
|
|
|
text: "Enter new password:",
|
|
|
|
silent: true,
|
|
|
|
},
|
2020-03-21 21:55:36 +01:00
|
|
|
function (err, password) {
|
2019-07-17 11:33:59 +02:00
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-20 08:28:04 +01:00
|
|
|
|
2020-10-04 17:03:26 +02:00
|
|
|
change(name, password);
|
2019-07-17 11:33:59 +02:00
|
|
|
}
|
|
|
|
);
|
2014-09-11 23:00:08 +02:00
|
|
|
});
|
2020-10-04 17:03:26 +02:00
|
|
|
|
|
|
|
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)}.`);
|
|
|
|
}
|