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-13 23:43:11 +00:00
|
|
|
|
|
|
|
module.exports = ClientManager;
|
|
|
|
|
|
|
|
function ClientManager() {
|
2014-08-14 16:35:37 +00:00
|
|
|
this.clients = [];
|
2014-08-13 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 16:35:37 +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;
|
|
|
|
}
|
2014-08-14 16:35:37 +00:00
|
|
|
if (!this.findClient(name)) {
|
|
|
|
this.clients.push(new Client(
|
2014-08-14 01:51:54 +00:00
|
|
|
sockets,
|
|
|
|
json
|
2014-08-14 16:35:37 +00:00
|
|
|
));
|
2014-08-14 01:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.loadUser = function(name) {
|
|
|
|
try {
|
|
|
|
var json = fs.readFileSync("users/" + name + "/user.json", "utf-8");
|
|
|
|
json = JSON.parse(json);
|
|
|
|
} catch(e) {
|
|
|
|
console.log(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return json;
|
2014-08-13 23:43:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientManager.prototype.getUsers = function() {
|
|
|
|
var users = [];
|
|
|
|
try {
|
|
|
|
users = fs.readdirSync("users/");
|
|
|
|
} 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 = "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 = "users/" + name;
|
|
|
|
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
|
|
|
};
|