From 25502f06873db1e7c8b9e15d69d4667a47d5cfc5 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 28 Aug 2015 14:31:35 -0400 Subject: [PATCH] closes #3508 --- src/controllers/accounts.js | 145 +++++++++----------------------- src/controllers/admin/groups.js | 6 +- src/controllers/categories.js | 12 +-- src/controllers/groups.js | 43 ++++------ src/controllers/helpers.js | 22 ----- src/controllers/index.js | 6 +- src/controllers/posts.js | 6 +- src/controllers/search.js | 2 +- src/controllers/topics.js | 12 +-- src/groups.js | 5 +- src/middleware/middleware.js | 4 - src/routes/feeds.js | 50 +++++++---- src/routes/plugins.js | 4 +- 13 files changed, 113 insertions(+), 204 deletions(-) diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index 5023d24d55..17562f0201 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -22,14 +22,10 @@ var fs = require('fs'), function getUserDataByUserSlug(userslug, callerUID, callback) { user.getUidByUserslug(userslug, function(err, uid) { - if (err) { + if (err || !uid) { return callback(err); } - if (!uid) { - return callback(null, null); - } - async.parallel({ userData : function(next) { user.getUserData(uid, next); @@ -50,7 +46,7 @@ function getUserDataByUserSlug(userslug, callerUID, callback) { groups.getUserGroups([uid], next); } }, function(err, results) { - if(err || !results.userData) { + if (err || !results.userData) { return callback(err || new Error('[[error:invalid-uid]]')); } @@ -111,17 +107,13 @@ accountsController.getUserByUID = function(req, res, next) { var uid = req.params.uid ? req.params.uid : 0; async.parallel({ - settings: async.apply(user.getSettings, uid), - userData: async.apply(user.getUserData, uid) + userData: async.apply(user.getUserData, uid), + settings: async.apply(user.getSettings, uid) }, function(err, results) { - if (err) { + if (err || !results.userData) { return next(err); } - if (!results.userData) { - return helpers.notFound(req, res); - } - results.userData.email = results.settings.showemail ? results.userData.email : undefined; results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; @@ -141,14 +133,10 @@ accountsController.getAccount = function(req, res, next) { } getUserDataByUserSlug(req.params.userslug, req.uid, function (err, userData) { - if (err) { + if (err || !userData) { return next(err); } - if (!userData) { - return helpers.notFound(req, res); - } - if (req.uid !== parseInt(userData.uid, 10)) { user.incrementUserFieldBy(userData.uid, 'profileviews', 1); } @@ -208,7 +196,7 @@ accountsController.getFollowers = function(req, res, next) { getFollow('account/followers', 'followers', req, res, next); }; -function getFollow(tpl, name, req, res, next) { +function getFollow(tpl, name, req, res, callback) { var userData; async.waterfall([ @@ -218,14 +206,14 @@ function getFollow(tpl, name, req, res, next) { function(data, next) { userData = data; if (!userData) { - return helpers.notFound(req, res); + return callback(); } var method = name === 'following' ? 'getFollowing' : 'getFollowers'; user[method](userData.uid, 0, 49, next); } ], function(err, users) { if (err) { - return next(err); + return callback(err); } userData.users = users; @@ -254,14 +242,10 @@ accountsController.getTopics = function(req, res, next) { accountsController.getGroups = function(req, res, next) { accountsController.getBaseUser(req.params.userslug, req.uid, function(err, userData) { - if (err) { + if (err || !userData) { return next(err); } - if (!userData) { - return helpers.notFound(req, res); - } - groups.getUserGroups([userData.uid], function(err, groupsData) { if (err) { return next(err); @@ -284,13 +268,11 @@ function getFromUserSet(tpl, set, method, type, req, res, next) { accountsController.getBaseUser(req.params.userslug, req.uid, next); } }, function(err, results) { - if (err) { + if (err || !results.userData) { return next(err); } + var userData = results.userData; - if (!userData) { - return helpers.notFound(req, res); - } var setName = 'uid:' + userData.uid + ':' + set; @@ -385,7 +367,7 @@ accountsController.accountEdit = function(req, res, next) { }); }; -accountsController.accountSettings = function(req, res, next) { +accountsController.accountSettings = function(req, res, callback) { var userData; async.waterfall([ function(next) { @@ -394,7 +376,7 @@ accountsController.accountSettings = function(req, res, next) { function(_userData, next) { userData = _userData; if (!userData) { - return helpers.notFound(req, res); + return callback(); } async.parallel({ settings: function(next) { @@ -421,7 +403,7 @@ accountsController.accountSettings = function(req, res, next) { } ], function(err) { if (err) { - return next(err); + return callback(err); } userData.dailyDigestFreqOptions = [ @@ -433,74 +415,23 @@ accountsController.accountSettings = function(req, res, next) { userData.bootswatchSkinOptions = [ - { - "name": "Default", - "value": "default" - }, - { - "name": "Cerulean", - "value": "cerulean" - }, - { - "name": "Cosmo", - "value": "cosmo" - }, - { - "name": "Cyborg", - "value": "cyborg" - }, - { - "name": "Darkly", - "value": "darkly" - }, - { - "name": "Flatly", - "value": "flatly" - }, - { - "name": "Journal", - "value": "journal" - }, - { - "name": "Lumen", - "value": "lumen" - }, - { - "name": "Paper", - "value": "paper" - }, - { - "name": "Readable", - "value": "readable" - }, - { - "name": "Sandstone", - "value": "sandstone" - }, - { - "name": "Simplex", - "value": "simplex" - }, - { - "name": "Slate", - "value": "slate" - }, - { - "name": "Spacelab", - "value": "spacelab" - }, - { - "name": "Superhero", - "value": "superhero" - }, - { - "name": "United", - "value": "united" - }, - { - "name": "Yeti", - "value": "yeti" - } + { "name": "Default", "value": "default" }, + { "name": "Cerulean", "value": "cerulean" }, + { "name": "Cosmo", "value": "cosmo" }, + { "name": "Cyborg", "value": "cyborg" }, + { "name": "Darkly", "value": "darkly" }, + { "name": "Flatly", "value": "flatly" }, + { "name": "Journal", "value": "journal" }, + { "name": "Lumen", "value": "lumen" }, + { "name": "Paper", "value": "paper" }, + { "name": "Readable", "value": "readable" }, + { "name": "Sandstone", "value": "sandstone" }, + { "name": "Simplex", "value": "simplex" }, + { "name": "Slate", "value": "slate" }, + { "name": "Spacelab", "value": "spacelab" }, + { "name": "Superhero", "value": "superhero" }, + { "name": "United", "value": "united" }, + { "name": "Yeti", "value": "yeti" } ]; userData.bootswatchSkinOptions.forEach(function(skin) { @@ -574,9 +505,9 @@ accountsController.getNotifications = function(req, res, next) { }); }; -accountsController.getChats = function(req, res, next) { - if (parseInt(meta.config.disableChat) === 1) { - return helpers.notFound(req, res); +accountsController.getChats = function(req, res, callback) { + if (parseInt(meta.config.disableChat, 10) === 1) { + return callback(); } // In case a userNAME is passed in instead of a slug, the route should not 404 @@ -590,7 +521,7 @@ accountsController.getChats = function(req, res, next) { recentChats: async.apply(messaging.getRecentChats, req.user.uid, 0, 19) }, function(err, results) { if (err) { - return next(err); + return callback(err); } //Remove entries if they were already present as a followed contact @@ -618,7 +549,7 @@ accountsController.getChats = function(req, res, next) { async.apply(user.getUidByUserslug, req.params.userslug), function(toUid, next) { if (!toUid || parseInt(toUid, 10) === parseInt(req.user.uid, 10)) { - return helpers.notFound(req, res); + return callback(); } async.parallel({ @@ -634,7 +565,7 @@ accountsController.getChats = function(req, res, next) { } ], function(err, data) { if (err) { - return next(err); + return callback(err); } res.render('chats', { diff --git a/src/controllers/admin/groups.js b/src/controllers/admin/groups.js index c52204f7a5..38b59a45e2 100644 --- a/src/controllers/admin/groups.js +++ b/src/controllers/admin/groups.js @@ -50,7 +50,7 @@ groupsController.list = function(req, res, next) { }); }; -groupsController.get = function(req, res, next) { +groupsController.get = function(req, res, callback) { var groupName = req.params.name; async.waterfall([ function(next){ @@ -58,13 +58,13 @@ groupsController.get = function(req, res, next) { }, function(exists, next) { if (!exists) { - return helpers.notFound(req, res); + return callback(); } groups.get(groupName, {uid: req.uid}, next); } ], function(err, group) { if (err) { - return next(err); + return callback(err); } res.render('admin/manage/group', {group: group}); }); diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 5509b0530a..b770598311 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -76,13 +76,13 @@ categoriesController.list = function(req, res, next) { }); }; -categoriesController.get = function(req, res, next) { +categoriesController.get = function(req, res, callback) { var cid = req.params.category_id, page = parseInt(req.query.page, 10) || 1, userPrivileges; if ((req.params.topic_index && !utils.isNumber(req.params.topic_index)) || !utils.isNumber(cid)) { - return helpers.notFound(req, res); + return callback(); } async.waterfall([ @@ -106,7 +106,7 @@ categoriesController.get = function(req, res, next) { userPrivileges = results.privileges; if (!results.exists || (results.categoryData && parseInt(results.categoryData.disabled, 10) === 1)) { - return helpers.notFound(req, res); + return callback(); } if (!results.privileges.read) { @@ -127,7 +127,7 @@ categoriesController.get = function(req, res, next) { } if (settings.usePagination && (page < 1 || page > pageCount)) { - return helpers.notFound(req, res); + return callback(); } if (!settings.usePagination) { @@ -245,7 +245,7 @@ categoriesController.get = function(req, res, next) { } ], function (err, data) { if (err) { - return next(err); + return callback(err); } data.currentPage = page; @@ -259,7 +259,7 @@ categoriesController.get = function(req, res, next) { plugins.fireHook('filter:category.build', {req: req, res: res, templateData: data}, function(err, data) { if (err) { - return next(err); + return callback(err); } res.render('category', data.templateData); }); diff --git a/src/controllers/groups.js b/src/controllers/groups.js index a44307d41e..2d4f97bc89 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -42,39 +42,34 @@ groupsController.getGroupsFromSet = function(uid, sort, start, stop, callback) { }); }; -groupsController.details = function(req, res, next) { +groupsController.details = function(req, res, callback) { async.waterfall([ async.apply(groups.exists, res.locals.groupName), function(exists, next) { if (!exists) { - return next(undefined, null); + return callback(); } - // Ensure the group isn't hidden either groups.isHidden(res.locals.groupName, next); }, function(hidden, next) { - if (hidden === null) { return next(undefined, false); } // Group didn't exist, not ok - if (!hidden) { - next(null, true); - } else { - // If not, only members are granted access - async.parallel([ - async.apply(groups.isMember, req.uid, res.locals.groupName), - async.apply(groups.isInvited, req.uid, res.locals.groupName) - ], function(err, checks) { - next(err, checks[0] || checks[1]); - }); + return next(); } + + async.parallel({ + isMember: async.apply(groups.isMember, req.uid, res.locals.groupName), + isInvited: async.apply(groups.isInvited, req.uid, res.locals.groupName) + }, function(err, checks) { + if (err || checks.isMember || checks.isInvited) { + return next(err); + } + callback(); + }); } - ], function(err, ok) { + ], function(err) { if (err) { - return next(err); - } - - if (!ok) { - return helpers.redirect(res, '/groups'); + return callback(err); } async.parallel({ @@ -90,12 +85,8 @@ groupsController.details = function(req, res, next) { }, isAdmin: async.apply(user.isAdministrator, req.uid) }, function(err, results) { - if (err) { - return next(err); - } - - if (!results.group) { - return helpers.notFound(req, res); + if (err || !results.group) { + return callback(err); } results.title = '[[pages:group, ' + results.group.displayName + ']]'; diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index ffc20fb90a..5486bcf009 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -11,28 +11,6 @@ var nconf = require('nconf'), var helpers = {}; -helpers.notFound = function(req, res, error) { - if (plugins.hasListeners('action:meta.override404')) { - plugins.fireHook('action:meta.override404', { - req: req, - res: res, - error: error - }); - } else if (res.locals.isAPI) { - res.status(404).json({ - path: req.path.replace(/^\/api/, ''), - error: error, - title: '[[global:404.title]]' - }); - } else { - res.status(404).render('404', { - path: req.path, - error: error, - title: '[[global:404.title]]' - }); - } -}; - helpers.notAllowed = function(req, res, error) { if (req.uid) { if (res.locals.isAPI) { diff --git a/src/controllers/index.js b/src/controllers/index.js index f6cf3c3c6d..53ae658937 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -101,7 +101,7 @@ Controllers.register = function(req, res, next) { var registrationType = meta.config.registrationType || 'normal'; if (registrationType === 'disabled') { - return helpers.notFound(req, res); + return next(); } async.waterfall([ @@ -164,7 +164,7 @@ Controllers.confirmEmail = function(req, res, next) { Controllers.sitemap = function(req, res, next) { if (parseInt(meta.config['feeds:disableSitemap'], 10) === 1) { - return helpers.notFound(req, res); + return next(); } var sitemap = require('../sitemap.js'); @@ -204,7 +204,7 @@ Controllers.outgoing = function(req, res, next) { Controllers.termsOfUse = function(req, res, next) { if (!meta.config.termsOfUse) { - return helpers.notFound(req, res); + return next(); } res.render('tos', {termsOfUse: meta.config.termsOfUse}); }; diff --git a/src/controllers/posts.js b/src/controllers/posts.js index a2bbfab226..33e314a191 100644 --- a/src/controllers/posts.js +++ b/src/controllers/posts.js @@ -16,12 +16,10 @@ postsController.getPost = function(req, res, next) { posts.getPostData(req.params.pid, next); } }, function(err, results) { - if (err) { + if (err || !results.postData) { return next(err); } - if (!results.postData) { - return helpers.notFound(req, res); - } + if (!results.canRead) { return helpers.notAllowed(req, res); } diff --git a/src/controllers/search.js b/src/controllers/search.js index 2148e6644b..5da395c2b8 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -12,7 +12,7 @@ var searchController = {}, searchController.search = function(req, res, next) { if (!plugins.hasListeners('filter:search.query')) { - return helpers.notFound(req, res); + return next(); } var breadcrumbs = helpers.buildBreadcrumbs([{text: '[[global:search]]'}]); diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 2975c73cb3..c1798d5bb4 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -16,13 +16,13 @@ var topicsController = {}, pagination = require('../pagination'), utils = require('../../public/src/utils'); -topicsController.get = function(req, res, next) { +topicsController.get = function(req, res, callback) { var tid = req.params.topic_id, sort = req.query.sort, userPrivileges; if ((req.params.post_index && !utils.isNumber(req.params.post_index)) || !utils.isNumber(tid)) { - return helpers.notFound(req, res); + return callback(); } async.waterfall([ @@ -60,7 +60,7 @@ topicsController.get = function(req, res, next) { } if (settings.usePagination && (page < 1 || page > pageCount)) { - return helpers.notFound(req, res); + return callback(); } var set = 'tid:' + tid + ':posts', @@ -109,7 +109,7 @@ topicsController.get = function(req, res, next) { topics.getTopicWithPosts(tid, set, req.uid, start, stop, reverse, function (err, topicData) { if (err && err.message === '[[error:no-topic]]' && !topicData) { - return helpers.notFound(req, res); + return callback(); } if (err && !topicData) { @@ -247,7 +247,7 @@ topicsController.get = function(req, res, next) { } ], function (err, data) { if (err) { - return next(err); + return callback(err); } data.privileges = userPrivileges; @@ -264,7 +264,7 @@ topicsController.get = function(req, res, next) { plugins.fireHook('filter:topic.build', {req: req, res: res, templateData: data}, function(err, data) { if (err) { - return next(err); + return callback(err); } res.render('topic', data.templateData); }); diff --git a/src/groups.js b/src/groups.js index f0f7efe066..13bae53e5f 100644 --- a/src/groups.js +++ b/src/groups.js @@ -296,11 +296,10 @@ var async = require('async'), Groups.isHidden = function(groupName, callback) { Groups.getGroupFields(groupName, ['hidden'], function(err, values) { if (err) { - winston.warn('[groups.isHidden] Could not determine group hidden state (group: ' + groupName + ')'); - return callback(null, true); // Default true + return callback(err); } - callback(null, parseInt(values.hidden, 10)); + callback(null, parseInt(values.hidden, 10) === 1); }); }; diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index df6f0a79ff..dc456c5d51 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -134,10 +134,6 @@ middleware.checkAccountPermissions = function(req, res, next) { user.getUidByUserslug(req.params.userslug, next); }, function (uid, next) { - if (!uid) { - return controllers.helpers.notFound(req, res); - } - if (parseInt(uid, 10) === req.uid) { return next(null, true); } diff --git a/src/routes/feeds.js b/src/routes/feeds.js index be018c33c3..798882e222 100644 --- a/src/routes/feeds.js +++ b/src/routes/feeds.js @@ -39,6 +39,10 @@ function hasPrivileges(method, id, req, res, next) { } function generateForTopic(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } + var tid = req.params.topic_id; privileges.topics.get(tid, req.uid, function(err, userPrivileges) { @@ -52,7 +56,7 @@ function generateForTopic(req, res, next) { } if (topicData.deleted && !userPrivileges.view_deleted) { - return helpers.notFound(req, res); + return next(); } var description = topicData.posts.length ? topicData.posts[0].content : ''; @@ -94,6 +98,10 @@ function generateForTopic(req, res, next) { } function generateForUserTopics(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } + var userslug = req.params.userslug; async.waterfall([ @@ -119,6 +127,9 @@ function generateForUserTopics(req, res, next) { } function generateForCategory(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } var cid = req.params.category_id; categories.getCategoryById({ @@ -149,6 +160,9 @@ function generateForCategory(req, res, next) { } function generateForRecent(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } generateForTopics({ uid: req.uid, title: 'Recently Active Topics', @@ -159,6 +173,9 @@ function generateForRecent(req, res, next) { } function generateForPopular(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } var terms = { daily: 'day', weekly: 'week', @@ -187,14 +204,6 @@ function generateForPopular(req, res, next) { }); } -function disabledRSS(req, res, next) { - if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { - return helpers.notFound(req, res); - } - - next(); -} - function generateForTopics(options, set, req, res, next) { topics.getTopicsFromSet(set, req.uid, 0, 19, function (err, data) { if (err) { @@ -262,6 +271,10 @@ function generateTopicsFeed(feedOptions, feedTopics, callback) { } function generateForRecentPosts(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } + posts.getRecentPosts(req.uid, 0, 19, 'month', function(err, posts) { if (err) { return next(err); @@ -279,6 +292,9 @@ function generateForRecentPosts(req, res, next) { } function generateForCategoryRecentPosts(req, res, next) { + if (parseInt(meta.config['feeds:disableRSS'], 10) === 1) { + return next(); + } var cid = req.params.category_id; async.parallel({ @@ -337,12 +353,12 @@ function sendFeed(feed, res) { } module.exports = function(app, middleware, controllers){ - app.get('/topic/:topic_id.rss', hasTopicPrivileges, disabledRSS, generateForTopic); - app.get('/category/:category_id.rss', hasCategoryPrivileges, disabledRSS, generateForCategory); - app.get('/recent.rss', disabledRSS, generateForRecent); - app.get('/popular.rss', disabledRSS, generateForPopular); - app.get('/popular/:term.rss', disabledRSS, generateForPopular); - app.get('/recentposts.rss', disabledRSS, generateForRecentPosts); - app.get('/category/:category_id/recentposts.rss', hasCategoryPrivileges, disabledRSS, generateForCategoryRecentPosts); - app.get('/user/:userslug/topics.rss', disabledRSS, generateForUserTopics); + app.get('/topic/:topic_id.rss', hasTopicPrivileges, generateForTopic); + app.get('/category/:category_id.rss', hasCategoryPrivileges, generateForCategory); + app.get('/recent.rss', generateForRecent); + app.get('/popular.rss', generateForPopular); + app.get('/popular/:term.rss', generateForPopular); + app.get('/recentposts.rss', generateForRecentPosts); + app.get('/category/:category_id/recentposts.rss', hasCategoryPrivileges, generateForCategoryRecentPosts); + app.get('/user/:userslug/topics.rss', generateForUserTopics); }; diff --git a/src/routes/plugins.js b/src/routes/plugins.js index a035b853aa..ad1c0e84fa 100644 --- a/src/routes/plugins.js +++ b/src/routes/plugins.js @@ -44,11 +44,11 @@ module.exports = function(app, middleware, controllers) { if (matches.length) { res.sendFile(matches[0]); } else { - helpers.notFound(req, res); + next(); } }); } else { - helpers.notFound(req, res); + next(); } }); };