mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	moved notFound notAllowed to helpers
This commit is contained in:
		| @@ -566,6 +566,9 @@ accountsController.getChats = function(req, res, next) { | |||||||
| 		async.waterfall([ | 		async.waterfall([ | ||||||
| 			async.apply(user.getUidByUserslug, req.params.userslug), | 			async.apply(user.getUidByUserslug, req.params.userslug), | ||||||
| 			function(toUid, next) { | 			function(toUid, next) { | ||||||
|  | 				if (!toUid) { | ||||||
|  | 					return notFound(res, '[[error:no-user]]'); | ||||||
|  | 				} | ||||||
| 				async.parallel({ | 				async.parallel({ | ||||||
| 					toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']), | 					toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']), | ||||||
| 					messages: async.apply(messaging.getMessages, req.user.uid, toUid, 'recent', false), | 					messages: async.apply(messaging.getMessages, req.user.uid, toUid, 'recent', false), | ||||||
|   | |||||||
| @@ -10,13 +10,9 @@ var categoriesController = {}, | |||||||
| 	topics = require('../topics'), | 	topics = require('../topics'), | ||||||
| 	meta = require('../meta'), | 	meta = require('../meta'), | ||||||
| 	plugins = require('../plugins'), | 	plugins = require('../plugins'), | ||||||
|  | 	helpers = require('./helpers'), | ||||||
| 	utils = require('../../public/src/utils'); | 	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) { | categoriesController.recent = function(req, res, next) { | ||||||
| 	var uid = req.user ? req.user.uid : 0; | 	var uid = req.user ? req.user.uid : 0; | ||||||
| 	var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; | 	var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1; | ||||||
| @@ -106,7 +102,7 @@ categoriesController.get = function(req, res, next) { | |||||||
| 		userPrivileges; | 		userPrivileges; | ||||||
|  |  | ||||||
| 	if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) { | 	if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) { | ||||||
| 		return categoriesController.notFound(req, res); | 		return helpers.notFound(res); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async.waterfall([ | 	async.waterfall([ | ||||||
| @@ -128,15 +124,15 @@ categoriesController.get = function(req, res, next) { | |||||||
| 		}, | 		}, | ||||||
| 		function(results, next) { | 		function(results, next) { | ||||||
| 			if (!results.exists || (results.categoryData && parseInt(results.categoryData.disabled, 10) === 1)) { | 			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) { | 			if (cid + '/' + req.params.slug !== results.categoryData.slug) { | ||||||
| 				return categoriesController.notFound(req, res); | 				return helpers.notFound(res); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (!results.privileges.read) { | 			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; | 			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; | module.exports = categoriesController; | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								src/controllers/helpers.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/controllers/helpers.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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'), | 	staticController = require('./static'), | ||||||
| 	apiController = require('./api'), | 	apiController = require('./api'), | ||||||
| 	adminController = require('./admin'), | 	adminController = require('./admin'), | ||||||
|  | 	helpers = require('./helpers'), | ||||||
|  |  | ||||||
| 	async = require('async'), | 	async = require('async'), | ||||||
| 	nconf = require('nconf'), | 	nconf = require('nconf'), | ||||||
| @@ -240,7 +241,7 @@ Controllers.outgoing = function(req, res, next) { | |||||||
|  |  | ||||||
| Controllers.termsOfUse = function(req, res, next) { | Controllers.termsOfUse = function(req, res, next) { | ||||||
| 	if (!meta.config.termsOfUse) { | 	if (!meta.config.termsOfUse) { | ||||||
| 		return categoriesController.notFound(req, res); | 		return helpers.notFound(res); | ||||||
| 	} | 	} | ||||||
| 	res.render('tos', {termsOfUse: meta.config.termsOfUse}); | 	res.render('tos', {termsOfUse: meta.config.termsOfUse}); | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ var topicsController = {}, | |||||||
| 	topics = require('../topics'), | 	topics = require('../topics'), | ||||||
| 	posts = require('../posts'), | 	posts = require('../posts'), | ||||||
| 	privileges = require('../privileges'), | 	privileges = require('../privileges'), | ||||||
| 	categoriesController = require('./categories'), | 	helpers = require('./helpers'), | ||||||
| 	utils = require('../../public/src/utils'); | 	utils = require('../../public/src/utils'); | ||||||
|  |  | ||||||
| topicsController.get = function(req, res, next) { | topicsController.get = function(req, res, next) { | ||||||
| @@ -22,7 +22,7 @@ topicsController.get = function(req, res, next) { | |||||||
| 		userPrivileges; | 		userPrivileges; | ||||||
|  |  | ||||||
| 	if (req.params.post_index && !utils.isNumber(req.params.post_index)) { | 	if (req.params.post_index && !utils.isNumber(req.params.post_index)) { | ||||||
| 		return categoriesController.notFound(req, res); | 		return helpers.notFound(res); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	async.waterfall([ | 	async.waterfall([ | ||||||
| @@ -43,15 +43,15 @@ topicsController.get = function(req, res, next) { | |||||||
| 			userPrivileges = results.privileges; | 			userPrivileges = results.privileges; | ||||||
|  |  | ||||||
| 			if (userPrivileges.disabled) { | 			if (userPrivileges.disabled) { | ||||||
| 				return categoriesController.notFound(req, res); | 				return helpers.notFound(res); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (tid + '/' + req.params.slug !== results.topic.slug) { | 			if (tid + '/' + req.params.slug !== results.topic.slug) { | ||||||
| 				return categoriesController.notFound(req, res); | 				return helpers.notFound(res); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (!userPrivileges.read) { | 			if (!userPrivileges.read) { | ||||||
| 				return categoriesController.notAllowed(req, res); | 				return helpers.notAllowed(req, res); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			var settings = results.settings; | 			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)) { | 			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', | 			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) { | 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) { | ||||||
| 				if (err && err.message === '[[error:no-topic]]' && !topicData) { | 				if (err && err.message === '[[error:no-topic]]' && !topicData) { | ||||||
| 					return categoriesController.notFound(req, res); | 					return helpers.notFound(res); | ||||||
| 				} | 				} | ||||||
| 				if (err && !topicData) { | 				if (err && !topicData) { | ||||||
| 					return next(err); | 					return next(err); | ||||||
| 				} | 				} | ||||||
| 				if (topicData.deleted && !userPrivileges.view_deleted) { | 				if (topicData.deleted && !userPrivileges.view_deleted) { | ||||||
| 					return categoriesController.notAllowed(req, res); | 					return helpers.notAllowed(req, res); | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				topicData.pageCount = pageCount; | 				topicData.pageCount = pageCount; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user