hardlounge/src/clientManager.js

112 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-08-14 17:25:22 +00:00
var _ = require("lodash");
2014-08-13 23:43:11 +00:00
var fs = require("fs");
2014-08-14 01:51:54 +00:00
var Client = require("./client");
2014-08-26 18:00:12 +00:00
var mkdirp = require("mkdirp");
var Helper = require("./helper");
2014-08-13 23:43:11 +00:00
module.exports = ClientManager;
function ClientManager() {
this.clients = [];
2014-08-13 23:43:11 +00:00
}
ClientManager.prototype.findClient = function(name) {
for (var i in this.clients) {
var client = this.clients[i];
if (client.name == name) {
return client;
}
}
return false;
};
2014-08-14 01:51:54 +00:00
ClientManager.prototype.loadUsers = function(sockets) {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
2014-08-14 01:51:54 +00:00
for (var i in users) {
var name = users[i];
var json = this.loadUser(name);
if (!json) {
continue;
}
if (!this.findClient(name)) {
this.clients.push(new Client(
2014-08-14 01:51:54 +00:00
sockets,
2014-09-16 19:47:01 +00:00
name,
2014-08-14 01:51:54 +00:00
json
));
2014-08-14 01:51:54 +00:00
}
}
};
ClientManager.prototype.loadUser = function(name) {
try {
2014-08-26 18:00:12 +00:00
var json = fs.readFileSync(
Helper.resolveHomePath("users", name, "user.json"),
2014-08-26 18:00:12 +00:00
"utf-8"
);
2014-08-14 01:51:54 +00:00
json = JSON.parse(json);
return json;
2014-08-14 01:51:54 +00:00
} catch(e) {
console.log(e);
return;
}
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.getUsers = function() {
var users = [];
var path = Helper.resolveHomePath("users");
2014-08-26 18:00:12 +00:00
mkdirp.sync(path);
2014-08-13 23:43:11 +00:00
try {
2014-08-26 18:00:12 +00:00
users = fs.readdirSync(path);
2014-08-13 23:43:11 +00:00
} catch(e) {
console.log(e);
return;
}
2014-08-14 17:25:22 +00:00
users = _.without(
users,
"example"
);
2014-08-13 23:43:11 +00:00
return users;
};
2014-08-14 01:51:54 +00:00
ClientManager.prototype.addUser = function(name, password) {
2014-08-13 23:43:11 +00:00
var users = this.getUsers();
if (users.indexOf(name) !== -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
var path = Helper.resolveHomePath("users", name);
2014-08-14 01:51:54 +00:00
var user = {
user: name,
password: password || "",
networks: []
};
2014-08-13 23:43:11 +00:00
fs.mkdirSync(path);
2014-08-14 01:51:54 +00:00
fs.writeFileSync(
path + "/user.json",
2014-08-18 23:54:14 +00:00
JSON.stringify(user, null, " "),
{mode: "0777"}
2014-08-14 01:51:54 +00:00
);
2014-08-13 23:43:11 +00:00
} catch(e) {
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};
ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
2014-08-14 17:25:22 +00:00
return false;
2014-08-13 23:43:11 +00:00
}
try {
var path = Helper.resolveHomePath("users", name);
2014-08-13 23:43:11 +00:00
fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path);
} catch(e) {
throw e;
}
2014-08-14 17:25:22 +00:00
return true;
2014-08-13 23:43:11 +00:00
};