Skip to content

Commit

Permalink
helpers.notAllowed hook
Browse files Browse the repository at this point in the history
  • Loading branch information
psychobunny committed Jun 23, 2016
1 parent 41af78d commit 371abba
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/controllers/helpers.js
Expand Up @@ -12,29 +12,35 @@ var meta = require('../meta');
var helpers = {};

helpers.notAllowed = function(req, res, error) {
if (req.uid) {
if (res.locals.isAPI) {
res.status(403).json({
path: req.path.replace(/^\/api/, ''),
loggedIn: !!req.uid, error: error,
title: '[[global:403.title]]'
});
} else {
res.status(403).render('403', {
path: req.path,
loggedIn: !!req.uid, error: error,
title: '[[global:403.title]]'
});
}
} else {
if (res.locals.isAPI) {
req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, '');
res.status(401).json('not-authorized');
plugins.fireHook('filter:helpers.notAllowed', {
req: req,
res: res,
error: error
}, function(err, data) {
if (req.uid) {

This comment has been minimized.

Copy link
@BenLubar

BenLubar Jun 24, 2016

Contributor

Shouldn't it be getting req, res, and error from the data object?

This comment has been minimized.

Copy link
@psychobunny

psychobunny Jun 24, 2016

Author Contributor

Eh, good catch. Thanks.

This comment has been minimized.

Copy link
@barisusakli

barisusakli Jun 24, 2016

Member

Shouldn't matter for req/res, you probably don't want to modify those. Error should be used from data though.

if (res.locals.isAPI) {
res.status(403).json({
path: req.path.replace(/^\/api/, ''),
loggedIn: !!req.uid, error: error,
title: '[[global:403.title]]'
});
} else {
res.status(403).render('403', {
path: req.path,
loggedIn: !!req.uid, error: error,
title: '[[global:403.title]]'
});
}
} else {
req.session.returnTo = nconf.get('relative_path') + req.url;
res.redirect(nconf.get('relative_path') + '/login');
if (res.locals.isAPI) {
req.session.returnTo = nconf.get('relative_path') + req.url.replace(/^\/api/, '');
res.status(401).json('not-authorized');
} else {
req.session.returnTo = nconf.get('relative_path') + req.url;
res.redirect(nconf.get('relative_path') + '/login');
}
}
}
});
};

helpers.redirect = function(res, url) {
Expand Down

0 comments on commit 371abba

Please sign in to comment.