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
$owner is defined by the method Role.isOwner = function isOwner(modelClass, modelId, userId, callback)
It will define if the current user is an "owner" of the model instance through key owner or userId or belongsTo relations
The problem is what if a model has two(or more) owners(through relation)?
The codes here says the method only checks the first belongsTo relation.
But what if the user is an owner through another belongsTo?
I tried to fix this by change the method like this
Role.isOwner=functionisOwner(modelClass,modelId,userId,callback){debug('overrided!');assert(modelClass,'Model class is required');debug('isOwner(): %s %s userId: %s',modelClass&&modelClass.modelName,modelId,userId);// No userId is presentif(!userId){process.nextTick(function(){callback(null,false);});return;}// Is the modelClass User or a subclass of User?if(isUserClass(modelClass)){process.nextTick(function(){callback(null,matches(modelId,userId));});return;}modelClass.findById(modelId,function(err,inst){if(err||!inst){debug('Model not found for id %j',modelId);if(callback)callback(err,false);}debug('Model found: %j',inst);varownerId=inst.userId||inst.owner;// Ensure ownerId exists and is not a function/relationif(ownerId&&'function'!==typeofownerId){if(callback)callback(null,matches(ownerId,userId));}else{varrel_tries=0;varmatched=false;functionprocessRelatedUser(err,user){if(matched){return;}varcheckFinish=function(){if(rel_tries===modelClass.relations.length){debug('No matching belongsTo relation found for model %j and user: %j',modelId,userId);if(callback)callback(null,false);}}if(!err&&user){rel_tries++;debug('User found: %j',user.id);varresult=matches(user.id,userId);debug('User matches result: %s !',result);if(result){matched=true;if(callback)callback(null,matches(user.id,userId));}checkFinish();}else{rel_tries++;checkFinish();}}// Try to follow belongsTofor(varrinmodelClass.relations){varrel=modelClass.relations[r];if(rel.type==='belongsTo'&&isUserClass(rel.modelTo)){debug('Checking relation %s to %s: %j',r,rel.modelTo.modelName,rel);inst[r](processRelatedUser);}}}});};
We don't currently support following relations for multiple owners as you pointed out above. Would you like to submit a PR to add this feature? Thanks!
Activity
upupzealot commentedon Jun 16, 2016
I tried to fix this by change the method like this
0candy commentedon Jun 17, 2016
We don't currently support following relations for multiple owners as you pointed out above. Would you like to submit a PR to add this feature? Thanks!
upupzealot commentedon Jun 19, 2016
@0candy Yeah I'd like to.
I would do that as soon as I get time.
upupzealot commentedon Jun 20, 2016
@0candy #2454
gvillenave commentedon Oct 13, 2016
Any update on when the PR will be available in master?