From 5a50a098d8a36302ee56b2bafb76d7462e1a6de1 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 28 Jul 2015 07:38:22 -0400 Subject: [PATCH] added a new middleware and plugin hook - Middleware added: requireUser, redirects to login if there is no user session. - Hook added: filter:router.page --- src/middleware/middleware.js | 17 +++++++++++++++++ src/routes/helpers.js | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 77951daf74..7295efbb7f 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -66,6 +66,15 @@ middleware.pageView = function(req, res, next) { } }; +middleware.pluginHooks = function(req, res, next) { + async.each(plugins.loadedHooks['filter:router.page'], function(hookObj, next) { + hookObj.method(req, res, next) + }, function(req, res) { + // If it got here, then none of the subscribed hooks did anything, or there were no hooks + next(); + }); +}; + middleware.redirectToAccountIfLoggedIn = function(req, res, next) { if (!req.user) { return next(); @@ -485,6 +494,14 @@ middleware.exposeUid = function(req, res, next) { } }; +middleware.requireUser = function(req, res, next) { + if (req.user) { + return next(); + } + + res.render('403', {}); +}; + function redirectToLogin(req, res) { req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, ''); return controllers.helpers.redirect(res, '/login'); diff --git a/src/routes/helpers.js b/src/routes/helpers.js index 31f3ece5b2..3cd1524fc5 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -3,7 +3,7 @@ var helpers = {}; helpers.setupPageRoute = function(router, name, middleware, middlewares, controller) { - middlewares = middlewares.concat([middleware.pageView]); + middlewares = middlewares.concat([middleware.pageView, middleware.pluginHooks]); router.get(name, middleware.buildHeader, middlewares, controller); router.get('/api' + name, middlewares, controller);