loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Role.registerResolver called twice

Open mike-aungsan opened this issue 11 years ago • 8 comments

var async = require('async')
var _ = lodash = require('lodash')
var ObjectID = require('mongodb').ObjectID 


module.exports = function(app) {

  var Role = app.models.Role;
  Role.registerResolver('aToB', function(role, context, cb) {

    var data, userId

    if (context && context.remotingContext && context.remotingContext.args && context.remotingContext.args.data) {
      data = context.remotingContext.args.data
    }

    userId = context.accessToken.userId

    if (context.modelName === 'name') {
      if (context.accessType === 'WRITE') {

        async.waterfall([

          function(asyncCb) {

            var A = app.models.a
            A.findById(data.aId, function(err, a) {

              if (a && a.name) {

                var ACL = app.models.ACL
                ACL.isMappedToRole(ACL.USER, userId, a.name, function(err, isMappedToRole) {
                  asyncCb(err, isMappedToRole)
                })
              }
              else{
                asyncCb(null, false)
              }

            })

          },

          function(isMappedToRole, asyncCb) {
            if (!isMappedToRole) {
              var Team = app.models.team
              Team.count({
                projectId: data.aId,
                memberId: userId
              }, function (err, count) {
                return asyncCb(null, count > 0)
              })

            }
            else {
              asyncCb(null, isMappedToRole)
            }
          }
        , function(err, isMappedToRole) {
          cb(err, isMappedToRole)
        })

      }

    }

  })

}

mike-aungsan avatar Nov 06 '15 02:11 mike-aungsan

console.log( arguments.callee.caller.toString())

function (role, context, callback) {
    if (!(context instanceof AccessContext)) {
      context = new AccessContext(context);
    }

    this.resolveRelatedModels();

    debug('isInRole(): %s', role);
    context.debug();

    var resolver = Role.resolvers[role];
    if (resolver) {
      debug('Custom resolver found for role %s', role);
      resolver(role, context, callback);
      return;
    }
    if (context.principals.length === 0) {                                                                               [154/9708]
      debug('isInRole() returns: false');
      process.nextTick(function() {
        if (callback) callback(null, false);
      });
      return;
    }

    var inRole = context.principals.some(function(p) {

      var principalType = p.type || undefined;
      var principalId = p.id || undefined;

      // Check if it's the same role
      return principalType === RoleMapping.ROLE && principalId === role;
    });

    if (inRole) {
      debug('isInRole() returns: %j', inRole);
      process.nextTick(function() {
        if (callback) callback(null, true);
      });
      return;
    }

    var roleMappingModel = this.roleMappingModel;
    this.findOne({where: {name: role}}, function(err, result) {
      if (err) {
        if (callback) callback(err);
        return;
      }
      if (!result) {
        if (callback) callback(null, false);
        return;                                                                                                          [121/9708]
      }
      debug('Role found: %j', result);

      // Iterate through the list of principals
      async.some(context.principals, function(p, done) {
        var principalType = p.type || undefined;
        var principalId = p.id || undefined;
        var roleId = result.id.toString();

        if (principalId !== null && principalId !== undefined && (typeof principalId !== 'string')) {
          principalId = principalId.toString();
        }

        if (principalType && principalId) {
          roleMappingModel.findOne({where: {roleId: roleId,
              principalType: principalType, principalId: principalId}},
            function(err, result) {
              debug('Role mapping found: %j', result);
              done(!err && result); // The only arg is the result
            });
        } else {
          process.nextTick(function() {
            done(false);
          });
        }
      }, function(inRole) {
        debug('isInRole() returns: %j', inRole);
        if (callback) callback(null, inRole);
      });
    });

  }

mike-aungsan avatar Nov 06 '15 02:11 mike-aungsan

Can you please provide more info? What seems to be the error that you are getting so I can reproduce it on my end? SEE https://github.com/strongloop/loopback/wiki/Reporting-issues#bug-report

loay avatar Nov 06 '15 18:11 loay

@mike-aungsan Any news on that issue? Still having the same problem? if yes, can you please provide me some info so I can help out. Thanks

loay avatar Dec 01 '15 15:12 loay

Hey, sorry I had a surgery and on medical leave. I found out why it happened but I don't remember right now. I will find out and post the cause.

mike-aungsan avatar Dec 02 '15 06:12 mike-aungsan

@mike-aungsan Thanks for your reply. I hope you get well very soon. Best Regards. Don't worry about it. If you see a similar issue, please add a comment and we will re-open the issue.

again, I hope you get well soon.

loay avatar Dec 02 '15 16:12 loay

I definitely see the same issue. When setting a breakpoint in VS Code and then calling a single endpoint wrapped with an ACL that points to a role that needs to be resolved, I hit the breakpoint twice. Its very repeatable on any role.

adamwlarson avatar Nov 01 '16 03:11 adamwlarson

@mike-aungsan I have the same problem. Could you explain to me why this it is called twice?

VMois avatar Dec 04 '17 19:12 VMois

If I remember correctly, there is an issue with ACL rule. I did not really solve the problem. I just change ACL rule and try to avoid those rules combination that cause the problem. I did not remember exactly what ACL rules combination cause the problem.

mike-aungsan avatar Dec 04 '17 19:12 mike-aungsan