Created
August 31, 2017 18:06
-
-
Save bcomerford/1172109c393df2e984b1b23e3387f832 to your computer and use it in GitHub Desktop.
Express.js middleware for project-based permissions/roles
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
| export default function requirePermission(projectId, permissions) { | |
| const isPermitted = permission => permissions.indexOf(permission) > -1; | |
| return function(req, res, next) { | |
| const project = req.user.projects[projectId]; | |
| if(!project) { | |
| return res.status(403).send({ error: 'You are not assigned to this project.' }); | |
| } | |
| if(!isAllowed(project.permission)) { | |
| return res.status(403).send({ error: 'You are not allowed to do that.' }); | |
| } | |
| next(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment