Last active
August 29, 2015 14:02
-
-
Save navarroaxel/0c16f30f25c7db42a513 to your computer and use it in GitHub Desktop.
A permission middleware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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