You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Role.getRoles not reporting database backed Roles when using Mongo #676
When using the Mongo as the primary datasource, Role.getRoles is unable to retrieve Roles associated with a principal by RoleMapping records (database based roles). The issue looks to be related to common/model/role.js lines 352-380:
var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping);
context.principals.forEach(function(p) {
// Check against the role mappings
var principalType = p.type || undefined;
var principalId = p.id || undefined;
// Add the role itself
if (principalType === RoleMapping.ROLE && principalId) {
addRole(principalId);
}
if (principalType && principalId) {
// Please find() treat undefined matches all values
inRoleTasks.push(function(done) {
roleMappingModel.find({where: {principalType: principalType,
principalId: principalId}}, function(err, mappings) {
debug('Role mappings found: %s %j', err, mappings);
if (err) {
done && done(err);
return;
}
mappings.forEach(function(m) {
addRole(m.roleId);
});
done && done();
});
});
}
});
Mongo stores the id field of a principal as an ObjectId type while RoleMapping is storing principalId as a string. The query passed to roleMappingModel.find is unable to match an ObjectId to a string, but I'm not sure what the proper resolution here is ...
Activity
raymondfeng commentedon Oct 21, 2014
Can you try the following fix to common/model/role.js?
arlaneenalra commentedon Oct 21, 2014
In a quick test that appears to fix getRoles. Am I going to be running into this problem in other areas (for instance isInRole around line 261?
raymondfeng commentedon Oct 21, 2014
The isRole() function already has logic to handle string ids, see: https://github.com/strongloop/loopback/blob/master/common/models/role.js#L286.
Would you like to submit a patch based on that?
arlaneenalra commentedon Oct 22, 2014
PR #680
If I can come up with a good test case for this one I'll send a PR with that.