-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow multiple ownership relations #3106
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
Changes from all commits
b091ea2
39c68af
0c1e15b
de8cc15
8d68b85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,7 +171,8 @@ module.exports = function(Role) { | |
| Role.isOwner = function isOwner(modelClass, modelId, userId, callback) { | ||
| assert(modelClass, 'Model class is required'); | ||
| debug('isOwner(): %s %s userId: %s', modelClass && modelClass.modelName, modelId, userId); | ||
| // No userId is present | ||
|
|
||
| // If no requester id, deny the resolver | ||
| if (!userId) { | ||
| process.nextTick(function() { | ||
| callback(null, false); | ||
|
|
@@ -193,34 +194,41 @@ module.exports = function(Role) { | |
| if (callback) callback(err, false); | ||
| return; | ||
| } | ||
| 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)); | ||
| return; | ||
| } else { | ||
| // 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); | ||
| return; | ||
| } | ||
|
|
||
| // 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); | ||
| if (inst[r](processRelatedUser)) return callback(null, true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we should use async.some() to find whether any of the relation grants the owner role. async.some(
Object.keys(modelClass.relations),
function(r, next) {
var rel = modelClass.relations[r];
// ...
next(null, matches(user.id, userId));
},
function(err, matchFound) {
if (err) return callback(err);
if (matchFound) return callback(null, matchFound);
// handle userId/owner properties
});
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, something is wrong here |
||
| } | ||
|
|
||
| debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId); | ||
| if (callback) callback(null, false); | ||
| } | ||
|
|
||
| function processRelatedUser(err, user) { | ||
| if (!err && user) { | ||
| debug('User found: %j', user.id); | ||
| if (callback) callback(null, matches(user.id, userId)); | ||
| } else { | ||
| if (callback) callback(err, false); | ||
| if (callback && matches(user.id, userId)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Checking the userId or owner field for resolving owner role | ||
| // is now done after fetching belongsTo relation to make possible | ||
| // to have multiple resolver role | ||
| var ownerId = inst.userId || inst.owner; | ||
| // Ensure ownerId exists and is not a function/relation | ||
| if (ownerId && 'function' !== typeof ownerId) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bajtos Concerning your comment on backward compatibility, the fallback is here : I choose to first check the belongsTo relations and then fallback to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
While the scenario may look wrong to us, it is supported by the current LoopBack version, thus there may be existing application relying on it, and therefore we need to preserve it for backwards compatibility. |
||
| if (callback && matches(ownerId, userId)) callback(null, true); | ||
| return; | ||
| } | ||
|
|
||
| // Finally DENY the owner role after sending | ||
| if (callback) callback(null, false); | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this block should be preserved. If there is no
userIdprovided by the request context, then there is no point in running any checks - they must all fail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, done in de8cc15