Skip to content

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

@arlaneenalra

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 ...

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