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 fs = require("fs");
|
|
|
|
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-10-03 23:33:44 +00:00
|
|
|
var path = Helper.HOME + "/users";
|
2014-09-13 21:29:45 +00:00
|
|
|
try {
|
|
|
|
mkdirp.sync(path);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("");
|
|
|
|
console.log("Could not create " + path);
|
|
|
|
console.log("Try running the command as sudo.");
|
|
|
|
console.log("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var test = path + "/.test";
|
|
|
|
fs.mkdirSync(test);
|
|
|
|
fs.rmdirSync(test);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("");
|
|
|
|
console.log("You have no permissions to write to " + path);
|
|
|
|
console.log("Try running the command as sudo.");
|
|
|
|
console.log("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var manager = new ClientManager();
|
|
|
|
var users = manager.getUsers();
|
|
|
|
if (users.indexOf(name) !== -1) {
|
|
|
|
console.log("");
|
|
|
|
console.log("User '" + name + "' already exists.");
|
|
|
|
console.log("");
|
|
|
|
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) {
|
|
|
|
if (!err) add(manager, name, password);
|
|
|
|
});
|
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) {
|
|
|
|
console.log("");
|
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
|
|
|
|
);
|
|
|
|
console.log("User '" + name + "' created:");
|
2014-10-14 20:05:16 +00:00
|
|
|
console.log(Helper.HOME + "/users/" + name + ".json");
|
2014-09-16 17:33:15 +00:00
|
|
|
console.log("");
|
|
|
|
}
|