From 5499c9d470f3b13c54e37165fe4c334f3b1864ba Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Fri, 12 Jun 2015 23:24:41 +0100 Subject: [PATCH] make parametized policies work --- index.js | 50 ++++++++++++++++++++++++++++++ test/controllers/MainController.js | 17 ++++++++++ test/index.js | 33 ++++++++++++++++++++ test/policies/accept.js | 3 ++ test/policiesFactories/is.js | 7 +++++ 5 files changed, 110 insertions(+) create mode 100644 test/controllers/MainController.js create mode 100644 test/policies/accept.js create mode 100644 test/policiesFactories/is.js diff --git a/index.js b/index.js index c02974e..07ab06f 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,57 @@ + + module.exports = function(sails){ return { + defaults: { + paths: { + policiesFactories: '/api/policiesFactories', + } + }, + + configure: function(){ + sails.config.policies = this.parse(sails.config.policies); + }, + + parse: function(obj){ + + var type = typeof obj; + + if(type === 'object'){ + for(var i in obj){ + obj[i] = this.parse(obj[i]); + } + + return obj; + } + + + if(type === 'string'){ + + var functionMatches = obj.match(/(.*)\((.*)\)/); + + + // the policy is a function + if(functionMatches !== null){ + + var functionName = functionMatches[1]; + var args = functionMatches[2].split(/\s*,\s*/); + + var policyFactory = require(sails.config.paths.policiesFactories + '/' + functionName); + + var policy = policyFactory.apply(this, args); + + return policy; + } + } + + + return obj; + } }; + + + } diff --git a/test/controllers/MainController.js b/test/controllers/MainController.js new file mode 100644 index 0000000..ee51e89 --- /dev/null +++ b/test/controllers/MainController.js @@ -0,0 +1,17 @@ + + + +// GET /admin +module.exports.admin = function(req, res){ + res.send(); +} + + +// GET /user +module.exports.user = function(req, res){ + res.send(); +} + +module.exports.ok = function(req, res){ + res.send(); +} diff --git a/test/index.js b/test/index.js index 07bf75b..56d14b7 100644 --- a/test/index.js +++ b/test/index.js @@ -23,7 +23,21 @@ describe('hook annotation router', function(){ }, paths: { controllers: __dirname + '/controllers', + policies: __dirname + '/policies', + policiesFactories: __dirname + '/policiesFactories', }, + routes: { + 'GET /ok': 'MainController.ok', + 'GET /user': 'MainController.user', + 'GET /admin': 'MainController.admin', + }, + policies: { + 'MainController': { + 'admin': ['is(\'Admin\')'], + 'user': 'is(\'User\')', + 'ok': 'accept', + } + } }, function(err, _sails){ if(err) return done(err); @@ -47,4 +61,23 @@ describe('hook annotation router', function(){ it('should start the sails server', function(){ return true; }); + + it('should not modify non function policies', function(done){ + request(sails.hooks.http.app) + .get('/ok') + .expect(200, done); + }); + + it('should parse function policy', function(done){ + request(sails.hooks.http.app) + .get('/user') + .expect('\'User\'', done); + }) + + it('should parse function policy into an array', function(done){ + request(sails.hooks.http.app) + .get('/admin') + .expect('\'Admin\'', done); + }) + }) diff --git a/test/policies/accept.js b/test/policies/accept.js new file mode 100644 index 0000000..20c7b40 --- /dev/null +++ b/test/policies/accept.js @@ -0,0 +1,3 @@ +module.exports = function(req, res, next){ + next(); +}; diff --git a/test/policiesFactories/is.js b/test/policiesFactories/is.js new file mode 100644 index 0000000..e56c651 --- /dev/null +++ b/test/policiesFactories/is.js @@ -0,0 +1,7 @@ +module.exports = function(userType){ + + return function(req, res, next){ + res.write(userType); + next(); + }; +};