Merge pull request #2259 from strongloop/feature/resolver-support-return-promise

Resolver support return promise
This commit is contained in:
Janny 2016-05-02 21:07:35 -04:00
commit 6b40c69bb3
2 changed files with 26 additions and 3 deletions

View File

@ -120,8 +120,9 @@ module.exports = function(Role) {
/** /**
* Add custom handler for roles. * Add custom handler for roles.
* @param {String} role Name of role. * @param {String} role Name of role.
* @param {Function} resolver Function that determines if a principal is in the specified role. * @param {Function} resolver Function that determines
* Signature must be `function(role, context, callback)` * if a principal is in the specified role.
* Should provide a callback or return a promise.
*/ */
Role.registerResolver = function(role, resolver) { Role.registerResolver = function(role, resolver) {
if (!Role.resolvers) { if (!Role.resolvers) {
@ -289,7 +290,14 @@ module.exports = function(Role) {
var resolver = Role.resolvers[role]; var resolver = Role.resolvers[role];
if (resolver) { if (resolver) {
debug('Custom resolver found for role %s', role); debug('Custom resolver found for role %s', role);
resolver(role, context, callback);
var promise = resolver(role, context, callback);
if (promise && typeof promise.then === 'function') {
promise.then(
function(result) { callback(null, result); },
callback
);
}
return; return;
} }

View File

@ -8,6 +8,7 @@ var Application = loopback.Application;
var ACL = loopback.ACL; var ACL = loopback.ACL;
var async = require('async'); var async = require('async');
var expect = require('chai').expect; var expect = require('chai').expect;
var Promise = require('bluebird');
function checkResult(err, result) { function checkResult(err, result) {
// console.log(err, result); // console.log(err, result);
@ -172,6 +173,14 @@ describe('role model', function() {
}); });
it('should support owner role resolver', function() { it('should support owner role resolver', function() {
Role.registerResolver('returnPromise', function(role, context) {
return new Promise(function(resolve) {
process.nextTick(function() {
resolve(true);
});
});
});
var Album = ds.createModel('Album', { var Album = ds.createModel('Album', {
name: String, name: String,
userId: Number, userId: Number,
@ -186,10 +195,16 @@ describe('role model', function() {
}); });
User.create({ name: 'Raymond', email: 'x@y.com', password: 'foobar' }, function(err, user) { User.create({ name: 'Raymond', email: 'x@y.com', password: 'foobar' }, function(err, user) {
Role.isInRole('returnPromise', { principalType: ACL.USER, principalId: user.id },
function(err, yes) {
assert(!err && yes);
});
Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: user.id }, Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: user.id },
function(err, yes) { function(err, yes) {
assert(!err && yes); assert(!err && yes);
}); });
Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: null }, Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: null },
function(err, yes) { function(err, yes) {
assert(!err && !yes); assert(!err && !yes);