# Middleware for both authentication and authorization errors = require '../commons/errors' module.exports = { checkDocumentPermissions: (req, res, next) -> return next() if req.user?.isAdmin() if not req.doc.hasPermissionsForMethod(req.user, req.method) if req.user return next new errors.Forbidden('You do not have permissions necessary.') return next new errors.Unauthorized('You must be logged in.') next() checkLoggedIn: -> return (req, res, next) -> if not req.user return next new errors.Unauthorized('You must be logged in.') next() checkHasPermission: (permissions) -> if _.isString(permissions) permissions = [permissions] return (req, res, next) -> if not req.user return next new errors.Unauthorized('You must be logged in.') if not _.size(_.intersection(req.user.get('permissions'), permissions)) return next new errors.Forbidden('You do not have permissions necessary.') next() }