Skip to content

Commit

Permalink
Merge 9efb376 into fbd862b
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jun 7, 2015
2 parents fbd862b + 9efb376 commit ea3a37a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -25,3 +25,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

.nyc_output
27 changes: 23 additions & 4 deletions index.js
Expand Up @@ -4,24 +4,43 @@ var annotationRouter = require('annotation-router');

module.exports = function(sails){
return {
initialize: function(callback){
configure: function(){

var controllersFolder = sails.config.paths.controllers;
var pattern = controllersFolder + '/**/*Controller.js';

annotationRouter(pattern, function(err, route){
var routes = annotationRouter.sync(pattern)

routes.map(function(route){

var controllerName = path.join(path.relative(controllersFolder, route.controller.dir), route.controller.name);

// 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,
};

}, callback);

// 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);
}
});
}
};
};
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "sails-hook-annotation-router",
"version": "0.1.1",
"version": "0.1.2",
"description": "sails hook which add annotation routing to controllers",
"main": "index.js",
"scripts": {
Expand All @@ -26,7 +26,7 @@
"supertest": "^1.0.1"
},
"dependencies": {
"annotation-router": "^0.6.0"
"annotation-router": "^0.7.0"
},
"sails": {
"isHook": true
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/rootController.js
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();
};
10 changes: 10 additions & 0 deletions test/index.js
Expand Up @@ -23,6 +23,10 @@ describe('hook annotation router', function(){
},
paths: {
controllers: __dirname + '/controllers',
policies: __dirname + '/policies',
},
policies: {
//'*': ['isLoggedIn']
}
}, function(err, _sails){

Expand Down Expand Up @@ -66,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);
})
})
5 changes: 5 additions & 0 deletions test/policies/isLoggedIn.js
@@ -0,0 +1,5 @@


module.exports = function(req, res, next){
res.forbidden();
}

0 comments on commit ea3a37a

Please sign in to comment.