mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-22 00:40:23 +00:00
180 lines
4.5 KiB
JavaScript
180 lines
4.5 KiB
JavaScript
// Generated by CoffeeScript 1.6.3
|
|
/*
|
|
|
|
User Handler
|
|
============
|
|
> TODO Add documentation
|
|
*/
|
|
|
|
|
|
(function() {
|
|
var answerHandler, db, exports, fAdminCommands, fs, log, mm, objAdminCmds, onAdminCommand, path, qs;
|
|
|
|
fs = require('fs');
|
|
|
|
path = require('path');
|
|
|
|
qs = require('querystring');
|
|
|
|
log = require('./logging');
|
|
|
|
db = require('./db_interface');
|
|
|
|
mm = require('./module_manager');
|
|
|
|
/* Prepare the admin command handlers that are issued via HTTP requests.*/
|
|
|
|
|
|
objAdminCmds = {
|
|
'loadrules': mm.loadRulesFromFS,
|
|
'loadaction': mm.loadActionModuleFromFS,
|
|
'loadactions': mm.loadActionModulesFromFS,
|
|
'loadevent': mm.loadEventModuleFromFS,
|
|
'loadevents': mm.loadEventModulesFromFS
|
|
};
|
|
|
|
exports = module.exports = function(args) {
|
|
var user, users, _i, _len;
|
|
args = args != null ? args : {};
|
|
log(args);
|
|
db(args);
|
|
mm(args);
|
|
mm.addDBLink(db);
|
|
users = JSON.parse(fs.readFileSync(path.resolve(__dirname, '..', 'config', 'users.json')));
|
|
for (_i = 0, _len = users.length; _i < _len; _i++) {
|
|
user = users[_i];
|
|
db.storeUser(user);
|
|
}
|
|
return module.exports;
|
|
};
|
|
|
|
exports.addShutdownHandler = function(fShutdown) {
|
|
return objAdminCmds.shutdown = fShutdown;
|
|
};
|
|
|
|
exports.handleRequest = function(req, resp) {
|
|
req.on('end', function() {
|
|
return resp.end();
|
|
});
|
|
if (req.session && req.session.user) {
|
|
resp.send('You\'re logged in');
|
|
} else {
|
|
resp.sendfile(path.resolve(__dirname, '..', 'webpages', 'handlers', 'login.html'));
|
|
}
|
|
return req.session.lastPage = req.originalUrl;
|
|
};
|
|
|
|
exports.handleLogin = function(req, resp) {
|
|
var body;
|
|
body = '';
|
|
req.on('data', function(data) {
|
|
return body += data;
|
|
});
|
|
return req.on('end', function() {
|
|
var obj;
|
|
if (!req.session || !req.session.user) {
|
|
obj = qs.parse(body);
|
|
return db.loginUser(obj.username, obj.password, function(err, obj) {
|
|
if (!err) {
|
|
req.session.user = obj;
|
|
}
|
|
if (req.session.user) {
|
|
resp.write('Welcome ' + req.session.user.name + '!');
|
|
} else {
|
|
resp.writeHead(401, {
|
|
"Content-Type": "text/plain"
|
|
});
|
|
resp.write('Login failed!');
|
|
}
|
|
return resp.end();
|
|
});
|
|
} else {
|
|
resp.write('Welcome ' + req.session.user.name + '!');
|
|
return resp.end();
|
|
}
|
|
});
|
|
};
|
|
|
|
answerHandler = function(resp) {
|
|
var hasBeenAnswered;
|
|
hasBeenAnswered = false;
|
|
postAnswer(msg)(function() {
|
|
if (!hasBeenAnswered) {
|
|
resp.write(msg);
|
|
resp.end();
|
|
return hasBeenAnswered = true;
|
|
}
|
|
});
|
|
return {
|
|
answerSuccess: function(msg) {
|
|
if (!hasBeenAnswered) {
|
|
return postAnswer(msg);
|
|
}
|
|
},
|
|
answerError: function(msg) {
|
|
if (!hasBeenAnswered) {
|
|
resp.writeHead(400, {
|
|
"Content-Type": "text/plain"
|
|
});
|
|
}
|
|
return postAnswer(msg);
|
|
},
|
|
isAnswered: function() {
|
|
return hasBeenAnswered;
|
|
}
|
|
};
|
|
};
|
|
|
|
onAdminCommand = function(req, response) {
|
|
var q;
|
|
q = req.query;
|
|
log.print('HL', 'Received admin request: ' + req.originalUrl);
|
|
if (q.cmd) {
|
|
return fAdminCommands(q, answerHandler(response));
|
|
} else {
|
|
return answerError(response, 'I\'m not sure about what you want from me...');
|
|
}
|
|
};
|
|
|
|
/*
|
|
admin commands handler receives all command arguments and an answerHandler
|
|
object that eases response handling to the HTTP request issuer.
|
|
|
|
@private fAdminCommands( *args, answHandler* )
|
|
*/
|
|
|
|
|
|
fAdminCommands = function(args, answHandler) {
|
|
var fAnsw, _name;
|
|
if (args && args.cmd) {
|
|
if (typeof adminCmds[_name = args.cmd] === "function") {
|
|
adminCmds[_name](args, answHandler);
|
|
}
|
|
} else {
|
|
log.print('RS', 'No command in request');
|
|
}
|
|
/*
|
|
The fAnsw function receives an answerHandler object as an argument when called
|
|
and returns an anonymous function
|
|
*/
|
|
|
|
fAnsw = function(ah) {
|
|
/*
|
|
The anonymous function checks whether the answerHandler was already used to
|
|
issue an answer, if no answer was provided we answer with an error message
|
|
*/
|
|
|
|
return function() {
|
|
if (!ah.isAnswered()) {
|
|
return ah.answerError('Not handled...');
|
|
}
|
|
};
|
|
};
|
|
/*
|
|
Delayed function call of the anonymous function that checks the answer handler
|
|
*/
|
|
|
|
return setTimeout(fAnsw(answHandler), 2000);
|
|
};
|
|
|
|
}).call(this);
|