From e3dd5cefe912e7aa6165aea8ef83e5e8efc7b55d Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Sun, 31 May 2015 11:23:40 +0100 Subject: [PATCH 1/3] WIP policies --- index.js | 6 +++++- test/index.js | 4 ++++ test/policies/isLoggedIn.js | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/policies/isLoggedIn.js diff --git a/index.js b/index.js index f319fc3..8feed47 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var annotationRouter = require('annotation-router'); module.exports = function(sails){ return { - initialize: function(callback){ + loadModules: function(callback){ var controllersFolder = sails.config.paths.controllers; var pattern = controllersFolder + '/**/*Controller.js'; @@ -21,6 +21,10 @@ module.exports = function(sails){ action: route.actionName, }; + sails.config.policies = { + '*': false + }; + }, callback); } }; diff --git a/test/index.js b/test/index.js index 22464c6..a261276 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){ diff --git a/test/policies/isLoggedIn.js b/test/policies/isLoggedIn.js new file mode 100644 index 0000000..b96639f --- /dev/null +++ b/test/policies/isLoggedIn.js @@ -0,0 +1,5 @@ + + +module.exports = function(req, res, next){ + console.log('policy called'); +} From 90c8eb981af0db1981e9aac6a700f2d706de5ee3 Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Sun, 7 Jun 2015 14:28:33 +0100 Subject: [PATCH 2/3] switching to synchronous code --- .gitignore | 2 ++ index.js | 13 +++++-------- package.json | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) 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 8feed47..5113783 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,14 @@ var annotationRouter = require('annotation-router'); module.exports = function(sails){ return { - loadModules: 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); @@ -20,12 +22,7 @@ module.exports = function(sails){ controller: controllerName, action: route.actionName, }; - - sails.config.policies = { - '*': false - }; - - }, callback); + }); } }; }; 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 From 9efb376d4288e2e84b98c05949d802d547e7cc29 Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Sun, 7 Jun 2015 15:05:21 +0100 Subject: [PATCH 3/3] handle action policy --- index.js | 20 +++++++++++++++++++- test/controllers/rootController.js | 7 +++++++ test/index.js | 6 ++++++ test/policies/isLoggedIn.js | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5113783..4597e4b 100644 --- a/index.js +++ b/index.js @@ -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); + } }); } }; 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 a261276..aa605dc 100644 --- a/test/index.js +++ b/test/index.js @@ -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); + }) }) diff --git a/test/policies/isLoggedIn.js b/test/policies/isLoggedIn.js index b96639f..b71b30a 100644 --- a/test/policies/isLoggedIn.js +++ b/test/policies/isLoggedIn.js @@ -1,5 +1,5 @@ module.exports = function(req, res, next){ - console.log('policy called'); + res.forbidden(); }