Skip to content

Role.getRoles not reporting database backed Roles when using Mongo #676

Closed

Description

@arlaneenalra

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

  1. raymondfeng commented on Oct 21, 2014

    @raymondfeng
    Member

    Can you try the following fix to common/model/role.js?

    if(typeof principalId !== 'string') {
       principalId = principalId.toString();
    }
    roleMappingModel.find({where: {principalType: principalType,
                principalId: principalId}}, function(err, mappings) {
  2. arlaneenalra commented on Oct 21, 2014

    @arlaneenalra
    Author

    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?

  3. raymondfeng commented on Oct 21, 2014

    @raymondfeng
    Member

    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?

  4. arlaneenalra commented on Oct 22, 2014

    @arlaneenalra
    Author

    PR #680
    If I can come up with a good test case for this one I'll send a PR with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions