Skip to content

Commit

Permalink
handle action policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jun 7, 2015
1 parent 90c8eb9 commit 9efb376
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ module.exports = function(sails){
// make it work for windows
controllerName = controllerName.split(path.sep).join('/');

var actionName = route.actionName

sails.config.routes[route.method + ' ' + route.url] = {
controller: controllerName,
action: route.actionName,
action: actionName,
};


// Handle policies

for(var policy in route.annotations){

if(!(controllerName in sails.config.policies)){
sails.config.policies[controllerName] = {};
}

if(!(actionName in sails.config.policies[controllerName])){
sails.config.policies[controllerName][actionName] = [];
}

sails.config.policies[controllerName][actionName].push(policy);
}
});
}
};
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/rootController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ module.exports.getItems = function(req, res){
module.exports.getItem = function(req, res){
res.send(+req.params.id);
};

// @isLoggedIn()
// @httpGet()
// @route('/private')
module.exports.private = function(req, res){
res.ok();
};
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ describe('hook annotation router', function(){
.get('/test')
.expect(200, done);
});

it('should allow the user to access /private route', function(done){
request(sails.hooks.http.app)
.get('/private')
.expect(403, done);
})
})
2 changes: 1 addition & 1 deletion test/policies/isLoggedIn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


module.exports = function(req, res, next){
console.log('policy called');
res.forbidden();
}

0 comments on commit 9efb376

Please sign in to comment.