Skip to content

Instantly share code, notes, and snippets.

@navarroaxel
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save navarroaxel/0c16f30f25c7db42a513 to your computer and use it in GitHub Desktop.

Select an option

Save navarroaxel/0c16f30f25c7db42a513 to your computer and use it in GitHub Desktop.
A permission middleware
/**
* Returns a middleware that checks the permission in the user's session
* @param permission
* @returns (Function) A middleware that check the user's permission.
*/
module.exports = function (permission) {
return function (req, res, next) {
if (permission && req.body.permissions.indexOf(permission) == -1) {
debug('User has no permission');
if (req.xhr) {
// If ajax.
return res.json(403, { message: 'You don\'t have permission to perform this action.'});
}
return res.redirect('/');
}
next();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment