Skip to content

Instantly share code, notes, and snippets.

@bcomerford
Created August 31, 2017 18:06
Show Gist options
  • Select an option

  • Save bcomerford/1172109c393df2e984b1b23e3387f832 to your computer and use it in GitHub Desktop.

Select an option

Save bcomerford/1172109c393df2e984b1b23e3387f832 to your computer and use it in GitHub Desktop.
Express.js middleware for project-based permissions/roles
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