moved notFound notAllowed to helpers

v1.18.x
barisusakli 10 years ago
parent f8cfd82667
commit 556e7066e8

@ -566,6 +566,9 @@ accountsController.getChats = function(req, res, next) {
async.waterfall([
async.apply(user.getUidByUserslug, req.params.userslug),
function(toUid, next) {
if (!toUid) {
return notFound(res, '[[error:no-user]]');
}
async.parallel({
toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']),
messages: async.apply(messaging.getMessages, req.user.uid, toUid, 'recent', false),

@ -10,13 +10,9 @@ var categoriesController = {},
topics = require('../topics'),
meta = require('../meta'),
plugins = require('../plugins'),
helpers = require('./helpers'),
utils = require('../../public/src/utils');
// todo: This might be better placed somewhere else
var apiToRegular = function(url) {
return url.replace(/^\/api/, '');
};
categoriesController.recent = function(req, res, next) {
var uid = req.user ? req.user.uid : 0;
var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
@ -106,7 +102,7 @@ categoriesController.get = function(req, res, next) {
userPrivileges;
if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
async.waterfall([
@ -128,15 +124,15 @@ categoriesController.get = function(req, res, next) {
},
function(results, next) {
if (!results.exists || (results.categoryData && parseInt(results.categoryData.disabled, 10) === 1)) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
if (cid + '/' + req.params.slug !== results.categoryData.slug) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
if (!results.privileges.read) {
return categoriesController.notAllowed(req, res);
return helpers.notAllowed(req, res);
}
var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0;
@ -260,32 +256,6 @@ categoriesController.get = function(req, res, next) {
});
};
categoriesController.notFound = function(req, res) {
if (res.locals.isAPI) {
res.status(404).json('not-found');
} else {
res.status(404).render('404');
}
};
categoriesController.notAllowed = function(req, res) {
var uid = req.user ? req.user.uid : 0;
if (uid) {
if (res.locals.isAPI) {
res.status(403).json('not-allowed');
} else {
res.status(403).render('403');
}
} else {
if (res.locals.isAPI) {
req.session.returnTo = apiToRegular(req.url);
res.status(401).json('not-authorized');
} else {
req.session.returnTo = req.url;
res.redirect(nconf.get('relative_path') + '/login');
}
}
};
module.exports = categoriesController;

@ -0,0 +1,35 @@
'use strict';
var helpers = {};
helpers.notFound = function(res) {
if (res.locals.isAPI) {
res.status(404).json('not-found');
} else {
res.status(404).render('404');
}
};
helpers.notAllowed = function(req, res) {
var uid = req.user ? req.user.uid : 0;
if (uid) {
if (res.locals.isAPI) {
res.status(403).json('not-allowed');
} else {
res.status(403).render('403');
}
} else {
if (res.locals.isAPI) {
req.session.returnTo = req.url.replace(/^\/api/, '');
res.status(401).json('not-authorized');
} else {
req.session.returnTo = req.url;
res.redirect(nconf.get('relative_path') + '/login');
}
}
};
module.exports = helpers;

@ -9,6 +9,7 @@ var topicsController = require('./topics'),
staticController = require('./static'),
apiController = require('./api'),
adminController = require('./admin'),
helpers = require('./helpers'),
async = require('async'),
nconf = require('nconf'),
@ -240,7 +241,7 @@ Controllers.outgoing = function(req, res, next) {
Controllers.termsOfUse = function(req, res, next) {
if (!meta.config.termsOfUse) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
res.render('tos', {termsOfUse: meta.config.termsOfUse});
};

@ -11,7 +11,7 @@ var topicsController = {},
topics = require('../topics'),
posts = require('../posts'),
privileges = require('../privileges'),
categoriesController = require('./categories'),
helpers = require('./helpers'),
utils = require('../../public/src/utils');
topicsController.get = function(req, res, next) {
@ -22,7 +22,7 @@ topicsController.get = function(req, res, next) {
userPrivileges;
if (req.params.post_index && !utils.isNumber(req.params.post_index)) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
async.waterfall([
@ -43,15 +43,15 @@ topicsController.get = function(req, res, next) {
userPrivileges = results.privileges;
if (userPrivileges.disabled) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
if (tid + '/' + req.params.slug !== results.topic.slug) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
if (!userPrivileges.read) {
return categoriesController.notAllowed(req, res);
return helpers.notAllowed(req, res);
}
var settings = results.settings;
@ -67,7 +67,7 @@ topicsController.get = function(req, res, next) {
}
if (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
var set = 'tid:' + tid + ':posts',
@ -110,13 +110,13 @@ topicsController.get = function(req, res, next) {
topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) {
if (err && err.message === '[[error:no-topic]]' && !topicData) {
return categoriesController.notFound(req, res);
return helpers.notFound(res);
}
if (err && !topicData) {
return next(err);
}
if (topicData.deleted && !userPrivileges.view_deleted) {
return categoriesController.notAllowed(req, res);
return helpers.notAllowed(req, res);
}
topicData.pageCount = pageCount;

Loading…
Cancel
Save