Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup client manager functions #1636

Merged
merged 1 commit into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 69 additions & 77 deletions src/clientManager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"use strict";

var _ = require("lodash");
var colors = require("colors/safe");
var fs = require("fs");
var Client = require("./client");
var Helper = require("./helper");
const _ = require("lodash");
const colors = require("colors/safe");
const fs = require("fs");
const path = require("path");
const Client = require("./client");
const Helper = require("./helper");
const WebPush = require("./plugins/webpush");

module.exports = ClientManager;
Expand All @@ -19,6 +20,7 @@ ClientManager.prototype.init = function(identHandler, sockets) {
this.webPush = new WebPush();

if (!Helper.config.public && !Helper.config.ldap.enable) {
// TODO: Remove deprecated warning in v3.0.0
if ("autoload" in Helper.config) {
log.warn(`Autoloading users is now always enabled. Please remove the ${colors.yellow("autoload")} option from your configuration file.`);
}
Expand All @@ -35,12 +37,6 @@ ClientManager.prototype.autoloadUsers = function() {
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 === 0) {
log.info(noUsersWarning);
}
Expand All @@ -64,84 +60,77 @@ ClientManager.prototype.autoloadUsers = function() {
if (client) {
client.quit(true);
this.clients = _.without(this.clients, client);
log.info(`User ${colors.bold(name)} disconnected and removed`);
log.info(`User ${colors.bold(name)} disconnected and removed.`);
}
});
}, 1000, {maxWait: 10000}));
};

ClientManager.prototype.loadUser = function(name) {
let json;
try {
json = this.readUserConfig(name);
} catch (e) {
log.error("Failed to read user config", e);
const user = readUserConfig(name);

if (!user) {
return;
}
if (!this.findClient(name)) {
this.clients.push(new Client(
this,
name,
json
));

let client = this.findClient(name);

if (client) {
log.warn(`Tried to load user ${colors.bold(name)}, which is already loaded.`);
return client;
}

client = new Client(this, name, user);
this.clients.push(client);

return client;
};

ClientManager.prototype.getUsers = function() {
var users = [];
try {
var files = fs.readdirSync(Helper.USERS_PATH);
files.forEach((file) => {
if (file.indexOf(".json") !== -1) {
users.push(file.replace(".json", ""));
}
});
} catch (e) {
log.error(`Failed to get users (${e})`);
return;
}
return users;
return fs
.readdirSync(Helper.USERS_PATH)
.filter((file) => file.endsWith(".json"))
.map((file) => file.slice(0, -5));
};

ClientManager.prototype.addUser = function(name, password, enableLog) {
var users = this.getUsers();
if (users.indexOf(name) !== -1) {
if (path.basename(name) !== name) {
throw new Error(`${name} is an invalid username.`);
}

const userPath = Helper.getUserConfigPath(name);

if (fs.existsSync(userPath)) {
log.error(`User ${colors.green(name)} already exists.`);
return false;
}
try {
if (require("path").basename(name) !== name) {
throw new Error(name + " is an invalid username.");
}

var user = {
user: name,
password: password || "",
log: enableLog,
awayMessage: "",
networks: [],
sessions: {},
};
fs.writeFileSync(
Helper.getUserConfigPath(name),
JSON.stringify(user, null, "\t")
);
const user = {
password: password || "",
log: enableLog || false,
awayMessage: "",
networks: [],
sessions: {},
};

try {
fs.writeFileSync(userPath, JSON.stringify(user, null, "\t"));
} catch (e) {
log.error("Failed to add user " + name, e);
log.error(`Failed to create user ${colors.green(name)} (${e})`);
throw e;
}

return true;
};

ClientManager.prototype.updateUser = function(name, opts, callback) {
const users = this.getUsers();
if (users.indexOf(name) === -1) {
return false;
}
if (typeof opts === "undefined") {
const user = readUserConfig(name);

if (!user) {
log.error(`Tried to update invalid user ${colors.green(name)}. This is most likely a bug.`);
return false;
}

const user = this.readUserConfig(name);
const currentUser = JSON.stringify(user, null, "\t");
_.assign(user, opts);
const newUser = JSON.stringify(user, null, "\t");
Expand All @@ -153,7 +142,7 @@ ClientManager.prototype.updateUser = function(name, opts, callback) {

fs.writeFile(Helper.getUserConfigPath(name), newUser, (err) => {
if (err) {
log.error(`Failed to update user ${colors.green(name)} (${err})`);
log.error(`Failed to update user ${colors.green(name)}. (${err})`);
}

if (callback) {
Expand All @@ -162,24 +151,27 @@ ClientManager.prototype.updateUser = function(name, opts, callback) {
});
};

ClientManager.prototype.readUserConfig = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
ClientManager.prototype.removeUser = function(name) {
const userPath = Helper.getUserConfigPath(name);

if (!fs.existsSync(userPath)) {
log.error(`Tried to remove non-existing user ${colors.green(name)}.`);
return false;
}
var data = fs.readFileSync(Helper.getUserConfigPath(name), "utf-8");
return JSON.parse(data);

fs.unlinkSync(userPath);

return true;
};

ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
function readUserConfig(name) {
const userPath = Helper.getUserConfigPath(name);

if (!fs.existsSync(userPath)) {
log.error(`Tried to read non-existing user ${colors.green(name)}`);
return false;
}
try {
fs.unlinkSync(Helper.getUserConfigPath(name));
} catch (e) {
throw e;
}
return true;
};

const data = fs.readFileSync(userPath, "utf-8");
return JSON.parse(data);
}
3 changes: 1 addition & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,7 @@ function performAuthentication(data) {
// If authorization succeeded but there is no loaded user,
// load it and find the user again (this happens with LDAP)
if (!client) {
manager.loadUser(data.user);
client = manager.findClient(data.user);
client = manager.loadUser(data.user);
}

initClient();
Expand Down