diff --git a/.gitignore b/.gitignore index 123ae94..125fab6 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/index.js b/index.js index f319fc3..4597e4b 100644 --- a/index.js +++ b/index.js @@ -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); + } + }); } }; }; diff --git a/package.json b/package.json index 75aecc1..9175586 100644 --- a/package.json +++ b/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": { @@ -26,7 +26,7 @@ "supertest": "^1.0.1" }, "dependencies": { - "annotation-router": "^0.6.0" + "annotation-router": "^0.7.0" }, "sails": { "isHook": true diff --git a/test/controllers/rootController.js b/test/controllers/rootController.js index 310b14a..cfcedca 100644 --- a/test/controllers/rootController.js +++ b/test/controllers/rootController.js @@ -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(); +}; diff --git a/test/index.js b/test/index.js index 22464c6..aa605dc 100644 --- a/test/index.js +++ b/test/index.js @@ -23,6 +23,10 @@ describe('hook annotation router', function(){ }, paths: { controllers: __dirname + '/controllers', + policies: __dirname + '/policies', + }, + policies: { + //'*': ['isLoggedIn'] } }, function(err, _sails){ @@ -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); + }) }) diff --git a/test/policies/isLoggedIn.js b/test/policies/isLoggedIn.js new file mode 100644 index 0000000..b71b30a --- /dev/null +++ b/test/policies/isLoggedIn.js @@ -0,0 +1,5 @@ + + +module.exports = function(req, res, next){ + res.forbidden(); +}