Skip to content

Commit

Permalink
make parametized policies work
Browse files Browse the repository at this point in the history
  • Loading branch information
mastilver committed Jun 12, 2015
1 parent db671af commit 5499c9d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
50 changes: 50 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
};



}
17 changes: 17 additions & 0 deletions test/controllers/MainController.js
Original file line number Diff line number Diff line change
@@ -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();
}
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
})

})
3 changes: 3 additions & 0 deletions test/policies/accept.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(req, res, next){
next();
};
7 changes: 7 additions & 0 deletions test/policiesFactories/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(userType){

return function(req, res, next){
res.write(userType);
next();
};
};

0 comments on commit 5499c9d

Please sign in to comment.