Merge pull request #1443 from thelounge/astorije/improve-user-mgmt-cli

Improve CLI user management
This commit is contained in:
Jérémie Astori 2017-08-23 19:49:18 -04:00 committed by GitHub
commit e292ef2bed
10 changed files with 88 additions and 42 deletions

View File

@ -8,9 +8,9 @@ module.exports = {
// Set to 'false' to enable users.
//
// @type boolean
// @default true
// @default false
//
public: true,
public: false,
//
// IP address or hostname for the web server to listen on.

View File

@ -18,7 +18,7 @@ ClientManager.prototype.init = function(identHandler, sockets) {
this.identHandler = identHandler;
this.webPush = new WebPush();
if (!Helper.config.public) {
if (!Helper.config.public && !Helper.config.ldap.enable) {
if ("autoload" in Helper.config) {
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
}
@ -32,12 +32,29 @@ ClientManager.prototype.findClient = function(name) {
};
ClientManager.prototype.autoloadUsers = function() {
this.getUsers().forEach((name) => this.loadUser(name));
const users = this.getUsers();
const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`;
// There was an error, already logged, but we have to crash the server as
// user directory could not be accessed
if (users === undefined) {
process.exit(1);
}
if (!users.length) {
log.info(noUsersWarning);
}
users.forEach((name) => this.loadUser(name));
fs.watch(Helper.USERS_PATH, _.debounce(() => {
const loaded = this.clients.map((c) => c.name);
const updatedUsers = this.getUsers();
if (!updatedUsers.length) {
log.info(noUsersWarning);
}
// New users created since last time users were loaded
_.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name));
@ -80,7 +97,7 @@ ClientManager.prototype.getUsers = function() {
}
});
} catch (e) {
log.error("Failed to get users", e);
log.error(`Failed to get users (${e})`);
return;
}
return users;

View File

@ -11,12 +11,22 @@ program
.description("Add a new user")
.on("--help", Utils.extraHelp)
.action(function(name) {
var manager = new ClientManager();
var users = manager.getUsers();
if (Helper.config.public) {
log.warn(`Users have no effect in ${colors.bold("public")} mode.`);
}
const manager = new ClientManager();
const users = manager.getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) !== -1) {
log.error(`User ${colors.bold(name)} already exists.`);
return;
}
log.prompt({
text: "Enter password:",
silent: true

View File

@ -13,6 +13,11 @@ program
.on("--help", Utils.extraHelp)
.action(function(name) {
var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) === -1) {
log.error(`User ${colors.bold(name)} does not exist.`);
return;

View File

@ -3,6 +3,7 @@
var ClientManager = new require("../clientManager");
var program = require("commander");
var colors = require("colors/safe");
const Helper = require("../helper");
const Utils = require("./utils");
program
@ -10,9 +11,18 @@ program
.description("List all users")
.on("--help", Utils.extraHelp)
.action(function() {
if (Helper.config.public) {
log.warn(`Users have no effect in ${colors.bold("public")} mode.`);
}
var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (!users.length) {
log.warn("No users found.");
log.info(`There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`);
} else {
log.info("Users:");
for (var i = 0; i < users.length; i++) {

View File

@ -10,10 +10,15 @@ program
.description("Remove an existing user")
.on("--help", Utils.extraHelp)
.action(function(name) {
var manager = new ClientManager();
if (manager.removeUser(name)) {
log.info(`User ${colors.bold(name)} removed.`);
} else {
log.error(`User ${colors.bold(name)} does not exist.`);
const manager = new ClientManager();
try {
if (manager.removeUser(name)) {
log.info(`User ${colors.bold(name)} removed.`);
} else {
log.error(`User ${colors.bold(name)} does not exist.`);
}
} catch (e) {
// There was an error, already logged
}
});

View File

@ -13,6 +13,11 @@ program
.on("--help", Utils.extraHelp)
.action(function(name) {
var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) === -1) {
log.error(`User ${colors.bold(name)} does not exist.`);
return;

View File

@ -1,8 +1,6 @@
"use strict";
var ClientManager = new require("../clientManager");
var program = require("commander");
var colors = require("colors/safe");
var server = require("../server");
var Helper = require("../helper");
const Utils = require("./utils");
@ -17,8 +15,6 @@ program
.description("Start the server")
.on("--help", Utils.extraHelp)
.action(function(options) {
var users = new ClientManager().getUsers();
var mode = Helper.config.public;
if (options.public) {
mode = true;
@ -26,13 +22,6 @@ program
mode = false;
}
if (!mode && !users.length && !Helper.config.ldap.enable) {
log.warn("No users found.");
log.info(`Create a new user with ${colors.bold("lounge add <name>")}.`);
return;
}
Helper.config.host = options.host || Helper.config.host;
Helper.config.port = options.port || Helper.config.port;
Helper.config.bind = options.bind || Helper.config.bind;

View File

@ -89,28 +89,32 @@ module.exports = function() {
}, () => {
const protocol = config.https.enable ? "https" : "http";
var address = server.address();
log.info(`Available on ${colors.green(protocol + "://" + address.address + ":" + address.port + "/")} \
in ${config.public ? "public" : "private"} mode`);
});
var sockets = io(server, {
serveClient: false,
transports: config.transports
});
log.info(
"Available at " +
colors.green(`${protocol}://${address.address}:${address.port}/`) +
` in ${colors.bold(config.public ? "public" : "private")} mode`
);
sockets.on("connect", function(socket) {
if (config.public) {
performAuthentication.call(socket, {});
} else {
socket.emit("auth", {success: true});
socket.on("auth", performAuthentication);
}
});
const sockets = io(server, {
serveClient: false,
transports: config.transports
});
manager = new ClientManager();
sockets.on("connect", (socket) => {
if (config.public) {
performAuthentication.call(socket, {});
} else {
socket.emit("auth", {success: true});
socket.on("auth", performAuthentication);
}
});
new Identification((identHandler) => {
manager.init(identHandler, sockets);
manager = new ClientManager();
new Identification((identHandler) => {
manager.init(identHandler, sockets);
});
});
};

View File

@ -2,6 +2,7 @@
var config = require("../../../defaults/config.js");
config.public = true;
config.prefetch = true;
config.host = config.bind = "127.0.0.1";
config.port = 61337;