I currently try to set up a simple authorization system (authentication already in place) for a sails (v0.10) application based on the policy system sails provides. For this purpose I'd need to get the controller and action the current request targets from within my policy.
I'd do roughly do the following in the policy:
module.exports = function (req, res, next) {
// Get target controller and action
var target = req.target; // How to do this?
var action = req.action; // and this?
// Lookup in database, let through if matches or render error
User.findOne...
};
I recall the target information was embedded in the request object at some point but isn't found there now (commit 5d98ec8).
I am aware of the possibility to parse req.route and get the needed information by mimicking the sails router, but I'd rather avoid duplicating the routing logic for the sole purpose of a policy.
Edit:
The controller identity can be retrieved from the request object like this:
var controller = req.options.controller || req.options.model;
Example req.options:
{ detectedVerb: { verb: '', original: '/user/:id?', path: '/user/:id?' },
associations:
[ { alias: 'auth', type: 'model', model: 'key' },
{ alias: 'group', type: 'model', model: 'group' },
{ alias: 'role', type: 'model', model: 'role' } ],
actions: true,
rest: true,
shortcuts: true,
prefix: '',
pluralize: false,
index: true,
model: 'user' }
Still hunting for a sane way to get the target controller action.
- Related Issue: https://github.com/balderdashy/sails/issues/1372
- Relate Google Group Posting: https://groups.google.com/forum/#!topic/sailsjs/faX54hqcNZE