Skip to content

Commit 54ee8d8

Browse files
authored
Merge pull request #2742 from codyolsen/feature/role-context
Fix context within listByPrincipalType role method
2 parents 7d64a92 + 3f5e49c commit 54ee8d8

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
@@ -78,26 +78,27 @@ module.exports = function(Role) {
7878
};
7979

8080
var model = relsToModels[rel];
81-
listByPrincipalType(model, relsToTypes[rel], query, callback);
81+
listByPrincipalType(this, model, relsToTypes[rel], query, callback);
8282
};
8383
});
8484

8585
/**
8686
* Fetch all models assigned to this role
8787
* @private
88+
* @param {object} Context role context
8889
* @param {*} model model type to fetch
8990
* @param {String} [principalType] principalType used in the rolemapping for model
9091
* @param {object} [query] query object passed to model find call
9192
* @param {Function} [callback] callback function called with `(err, models)` arguments.
9293
*/
93-
function listByPrincipalType(model, principalType, query, callback) {
94+
function listByPrincipalType(context, model, principalType, query, callback) {
9495
if (callback === undefined) {
9596
callback = query;
9697
query = {};
9798
}
9899

99100
roleModel.roleMappingModel.find({
100-
where: { roleId: this.id, principalType: principalType },
101+
where: { roleId: context.id, principalType: principalType },
101102
}, function(err, mappings) {
102103
var ids;
103104
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+
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)