3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 21:09:31 +01:00
dbot/modules/web/api.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-09-08 12:42:59 +02:00
var _ = require('underscore')._;
2013-09-08 13:20:48 +02:00
var api = function(dbot) {
return {
'getUrl': function(path) {
if(path.charAt(0) == '/') path = path.substr(1);
2015-01-07 22:47:12 +01:00
path = encodeURI(path);
2013-09-08 13:20:48 +02:00
if(this.config.externalPath) {
return this.config.externalPath + '/' + path;
} else {
return 'http://' + this.config.webHost + ':' + this.config.webPort + '/' + path;
}
},
'addIndexLink': function(route, title) {
this.indexLinks[route] = title;
},
'getWebUser': function(id, callback) {
this.db.read('web_users', id, function(err, webUser) {
callback(webUser);
});
},
'hasAccess': function(req, res, next) {
var path = req.route.path,
module = dbot.pages[path].module,
2014-06-06 03:16:30 +02:00
mConfig = dbot.config.modules[module],
accessNeeded,
allowedNicks;
2013-09-08 13:20:48 +02:00
if(mConfig.requireWebLogin == true) {
if(req.isAuthenticated()) {
2014-06-27 00:03:45 +02:00
if(_.has(mConfig, 'pageAccess') && _.has(mConfig.pageAccess, path)) {
2013-09-08 13:20:48 +02:00
accessNeeded = mConfig.pageAccess[path];
} else if(!_.isUndefined(mConfig.webAccess)) {
accessNeeded = mConfig.webAccess;
}
2014-06-06 03:16:30 +02:00
if(!_.isUndefined(accessNeeded) || accessNeeded == null) {
return next();
}
2013-09-08 13:20:48 +02:00
2014-06-06 03:16:30 +02:00
if(!_.isFunction(accessNeeded)) {
if(_.has(dbot.access, accessNeeded)) {
accessNeeded = dbot.access[accessNeeded];
2013-09-08 13:20:48 +02:00
} else {
2014-06-06 03:16:30 +02:00
return next();
2013-09-08 13:20:48 +02:00
}
2014-06-06 03:16:30 +02:00
}
2014-10-21 23:25:50 +02:00
if(_.include(accessNeeded(), req.user.primaryNick)) {
2013-09-08 13:20:48 +02:00
return next();
2014-06-06 03:16:30 +02:00
} else {
res.redirect('/');
2013-09-08 13:20:48 +02:00
}
} else {
res.render('login', {
'message': 'You need to log in to access this module.',
2014-10-21 17:08:26 +02:00
'redirect': req.originalUrl,
'routes': dbot.modules.web.indexLinks
2013-09-08 13:20:48 +02:00
});
}
} else {
return next();
}
}
};
};
exports.fetch = function(dbot) {
return api(dbot);
};