2014-09-13 21:29:45 +00:00
|
|
|
var ClientManager = new require("../clientManager");
|
2014-10-03 09:57:35 +00:00
|
|
|
var bcrypt = require("bcrypt-nodejs");
|
2014-09-13 21:29:45 +00:00
|
|
|
var program = require("commander");
|
|
|
|
var mkdirp = require("mkdirp");
|
2014-09-13 21:55:07 +00:00
|
|
|
var Helper = require("../helper");
|
2014-09-13 17:51:10 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
program
|
2014-10-27 21:55:20 +00:00
|
|
|
.command("add <name>")
|
2014-09-13 21:29:45 +00:00
|
|
|
.description("Add a new user")
|
2015-09-30 22:39:57 +00:00
|
|
|
.action(function(name/* , password */) {
|
2014-09-13 21:29:45 +00:00
|
|
|
try {
|
2016-05-08 06:21:31 +00:00
|
|
|
mkdirp.sync(Helper.USERS_PATH);
|
2014-09-13 21:29:45 +00:00
|
|
|
} catch (e) {
|
2016-05-08 06:21:31 +00:00
|
|
|
log.error("Could not create", Helper.USERS_PATH);
|
2014-09-13 21:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var manager = new ClientManager();
|
|
|
|
var users = manager.getUsers();
|
|
|
|
if (users.indexOf(name) !== -1) {
|
2016-04-16 11:32:38 +00:00
|
|
|
log.error("User '" + name + "' already exists.");
|
2014-09-13 21:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-27 21:55:20 +00:00
|
|
|
require("read")({
|
2016-03-31 23:28:25 +00:00
|
|
|
prompt: "[thelounge] Enter password: ",
|
2014-10-27 21:55:20 +00:00
|
|
|
silent: true
|
|
|
|
}, function(err, password) {
|
2016-05-01 09:41:17 +00:00
|
|
|
if (!err) {
|
|
|
|
add(manager, name, password);
|
|
|
|
}
|
2014-10-27 21:55:20 +00:00
|
|
|
});
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
2014-09-15 21:31:25 +00:00
|
|
|
|
2014-09-16 17:33:15 +00:00
|
|
|
function add(manager, name, password) {
|
2014-10-03 09:57:35 +00:00
|
|
|
var salt = bcrypt.genSaltSync(8);
|
|
|
|
var hash = bcrypt.hashSync(password, salt);
|
2014-10-14 20:53:26 +00:00
|
|
|
manager.addUser(
|
2014-09-16 17:33:15 +00:00
|
|
|
name,
|
|
|
|
hash
|
|
|
|
);
|
2016-04-16 11:32:38 +00:00
|
|
|
log.info("User '" + name + "' created:");
|
2016-05-08 06:21:31 +00:00
|
|
|
log.info(Helper.getUserConfigPath(name));
|
2014-09-16 17:33:15 +00:00
|
|
|
}
|