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. // Set to 'false' to enable users.
// //
// @type boolean // @type boolean
// @default true // @default false
// //
public: true, public: false,
// //
// IP address or hostname for the web server to listen on. // 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.identHandler = identHandler;
this.webPush = new WebPush(); this.webPush = new WebPush();
if (!Helper.config.public) { if (!Helper.config.public && !Helper.config.ldap.enable) {
if ("autoload" in Helper.config) { if ("autoload" in Helper.config) {
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`); 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() { 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(() => { fs.watch(Helper.USERS_PATH, _.debounce(() => {
const loaded = this.clients.map((c) => c.name); const loaded = this.clients.map((c) => c.name);
const updatedUsers = this.getUsers(); const updatedUsers = this.getUsers();
if (!updatedUsers.length) {
log.info(noUsersWarning);
}
// New users created since last time users were loaded // New users created since last time users were loaded
_.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name)); _.difference(updatedUsers, loaded).forEach((name) => this.loadUser(name));
@ -80,7 +97,7 @@ ClientManager.prototype.getUsers = function() {
} }
}); });
} catch (e) { } catch (e) {
log.error("Failed to get users", e); log.error(`Failed to get users (${e})`);
return; return;
} }
return users; return users;

View File

@ -11,12 +11,22 @@ program
.description("Add a new user") .description("Add a new user")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); if (Helper.config.public) {
var users = manager.getUsers(); 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) { if (users.indexOf(name) !== -1) {
log.error(`User ${colors.bold(name)} already exists.`); log.error(`User ${colors.bold(name)} already exists.`);
return; return;
} }
log.prompt({ log.prompt({
text: "Enter password:", text: "Enter password:",
silent: true silent: true

View File

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

View File

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

View File

@ -10,10 +10,15 @@ program
.description("Remove an existing user") .description("Remove an existing user")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); const manager = new ClientManager();
if (manager.removeUser(name)) {
log.info(`User ${colors.bold(name)} removed.`); try {
} else { if (manager.removeUser(name)) {
log.error(`User ${colors.bold(name)} does not exist.`); 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) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
log.error(`User ${colors.bold(name)} does not exist.`); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;

View File

@ -1,8 +1,6 @@
"use strict"; "use strict";
var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
var server = require("../server"); var server = require("../server");
var Helper = require("../helper"); var Helper = require("../helper");
const Utils = require("./utils"); const Utils = require("./utils");
@ -17,8 +15,6 @@ program
.description("Start the server") .description("Start the server")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(options) { .action(function(options) {
var users = new ClientManager().getUsers();
var mode = Helper.config.public; var mode = Helper.config.public;
if (options.public) { if (options.public) {
mode = true; mode = true;
@ -26,13 +22,6 @@ program
mode = false; 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.host = options.host || Helper.config.host;
Helper.config.port = options.port || Helper.config.port; Helper.config.port = options.port || Helper.config.port;
Helper.config.bind = options.bind || Helper.config.bind; Helper.config.bind = options.bind || Helper.config.bind;

View File

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

View File

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