Skip to content

Commit 007b20d

Browse files
authored
Merge pull request #2944 from strongloop/feature/role-context-2x
Fix context within listByPrincipalType role method
2 parents 586fa1c + d99d608 commit 007b20d

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

common/models/role.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,27 @@ module.exports = function(Role) {
8080
};
8181

8282
var model = relsToModels[rel];
83-
listByPrincipalType(model, relsToTypes[rel], query, callback);
83+
listByPrincipalType(this, model, relsToTypes[rel], query, callback);
8484
};
8585
});
8686

8787
/**
8888
* Fetch all models assigned to this role
8989
* @private
90+
* @param {object} Context role context
9091
* @param {*} model model type to fetch
9192
* @param {String} [principalType] principalType used in the rolemapping for model
9293
* @param {object} [query] query object passed to model find call
9394
* @param {Function} [callback] callback function called with `(err, models)` arguments.
9495
*/
95-
function listByPrincipalType(model, principalType, query, callback) {
96+
function listByPrincipalType(context, model, principalType, query, callback) {
9697
if (callback === undefined) {
9798
callback = query;
9899
query = {};
99100
}
100101

101102
roleModel.roleMappingModel.find({
102-
where: {roleId: this.id, principalType: principalType}
103+
where: {roleId: context.id, principalType: principalType},
103104
}, function(err, mappings) {
104105
var ids;
105106
if (err) {

test/role.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,84 @@ describe('role model', function() {
616616
});
617617
});
618618

619+
it('should fetch all models only assigned to the role', function(done) {
620+
var principalTypesToModels = {};
621+
var mappings;
622+
623+
principalTypesToModels[RoleMapping.USER] = User;
624+
principalTypesToModels[RoleMapping.APPLICATION] = Application;
625+
principalTypesToModels[RoleMapping.ROLE] = Role;
626+
mappings = Object.keys(principalTypesToModels);
627+
628+
async.each(mappings, function(principalType, eachCallback) {
629+
var Model = principalTypesToModels[principalType];
630+
631+
async.waterfall([
632+
// Create models
633+
function(next) {
634+
Model.create([
635+
{ name: 'test', email: '[email protected]', password: 'foobar' },
636+
{ name: 'test2', email: '[email protected]', password: 'bargoo' },
637+
{ name: 'test3', email: '[email protected]', password: 'bluegoo' }],
638+
function(err, models) {
639+
if (err) return next(err);
640+
next(null, models);
641+
});
642+
},
643+
644+
// Create Roles
645+
function(models, next) {
646+
var uniqueRoleName = 'testRoleFor' + principalType;
647+
var otherUniqueRoleName = 'otherTestRoleFor' + principalType;
648+
Role.create([
649+
{ name: uniqueRoleName },
650+
{ name: otherUniqueRoleName }],
651+
function(err, roles) {
652+
if (err) return next(err);
653+
next(null, models, roles);
654+
});
655+
},
656+
657+
// Create principles
658+
function(models, roles, next) {
659+
async.parallel([
660+
function(callback) {
661+
roles[0].principals.create(
662+
{ principalType: principalType, principalId: models[0].id },
663+
function(err, p) {
664+
if (err) return callback(err);
665+
callback(p);
666+
});
667+
},
668+
function(callback) {
669+
roles[1].principals.create(
670+
{ principalType: principalType, principalId: models[1].id },
671+
function(err, p) {
672+
if (err) return callback(err);
673+
callback(p);
674+
});
675+
}],
676+
function(err, principles) {
677+
next(null, models, roles, principles);
678+
});
679+
},
680+
681+
// Run tests against unique Role
682+
function(models, roles, principles, next) {
683+
var pluralName = Model.pluralModelName.toLowerCase();
684+
var uniqueRole = roles[0];
685+
uniqueRole[pluralName](function(err, models) {
686+
if (err) return done(err);
687+
assert.equal(models.length, 1);
688+
next();
689+
});
690+
}],
691+
eachCallback);
692+
}, function(err) {
693+
done();
694+
});
695+
});
696+
619697
it('should apply query', function(done) {
620698
User.create({ name: 'Raymond', email: '[email protected]', password: 'foobar' }, function(err, user) {
621699
if (err) return done(err);

0 commit comments

Comments
 (0)