-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi owner for one model #2441
Comments
I tried to fix this by change the method like this Role.isOwner = function isOwner(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 present
if (!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);
var ownerId = inst.userId || inst.owner;
// Ensure ownerId exists and is not a function/relation
if (ownerId && 'function' !== typeof ownerId) {
if (callback) callback(null, matches(ownerId, userId));
} else {
var rel_tries = 0;
var matched = false;
function processRelatedUser(err, user) {
if(matched) {
return;
}
var checkFinish = 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);
var result = 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 belongsTo
for (var r in modelClass.relations) {
var rel = 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! |
@0candy Yeah I'd like to. |
Any update on when the PR will be available in master? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$owner
is defined by the methodRole.isOwner = function isOwner(modelClass, modelId, userId, callback)
It will define if the current user is an "owner" of the model instance through key
owner
oruserId
orbelongsTo
relationsThe 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
?The text was updated successfully, but these errors were encountered: