Devendra Verma Devendra Verma - 2 months ago 10
Node.js Question

Apply multiple policies in sails on a route

How to apply multiple policies in sails on routes which are generated by sails like this :

/users/:id/orders
. I can apply a single policy like this in config/routes.js

'/users/:id/orders' : {
policy : 'isAuthenticated'
}


But how can apply more than one policy in similar manner

Answer

Sadly the documentation http://sailsjs.org/documentation/concepts/routes/custom-routes#?policy-target-syntax does not talk about chaining policies in routes.

As an alternative your could protect the populate action in your user controller like so: edit config/policies.js

UserController: {
    populate: ['isAuthenticated', 'isAllowed']
}

http://sailsjs.org/documentation/reference/blueprint-api/populate-where

If you just want to apply the policy only to the orders association, you can retrieve the association parameter (/:model/:id/:association) from the req object inside the policy and handle your case:

module.exports = function(req, res, next) {
  if (req.param('association') == 'orders') {
      // do your magic
  } else {
     return next();
  }
};