-
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
Allow multiple ownership relations #3106
Conversation
Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test." |
Can one of the admins verify this patch? |
3 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Sorry, I see that some points are not passing : One of my comment exceed the 50 characters size but needs to be removed anyway before merging (I guess). Could we integrate this check when passing test in local ? However any help would be appreciated for the rebase action ask by the linter bot |
Hi @pierreclr, thank you for the pull request. Don't worry about the git history now, we can fix it together later, when the code is good to be merged. Are you trying to fix the same issue as #2454? cc @upupzealot I need you to add unit-tests to verify that your implementation works as intended and also to prevent unintentional regressions in the future. |
@slnode ok to test |
Hi @bajtos, I think you're right. I didn't see it (despite actively looking for it :/) That's the same issue but I choose another implementation (I think having a counter to check if we have finished is quite tricky (see 8a497eb) and prefer to return the denied callback once every possibility has been tried. However I see that I forgot to remove console.log ... Depending on the implementation you will choose I'll update my PR and write unit tests. |
Hi @bajtos, @upupzealot is not maintaining his PR anymore I added unit tests to avoid unintentional regressions in the future. Let me know when you plan to implement this feature because I really need to have that going to production soon. Thank you in advance |
@@ -171,13 +171,6 @@ 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 (!userId) { |
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 userId
provided 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
|
||
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); |
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.
inst[r]
is async function that may not return immediately, I am surprised this code actually works.
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
});
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.
Indeed, something is wrong here
I am concerned about backwards-compatibility of such change. Right now, when a model instance has I am proposing to add a feature flag to control this behaviour, defaulting to the old mode. The algorithm in
Now the remaining question is where and how to configure the feature flag. Consider the use case where a model has two relations to a User model, but only one of the relations is granting the ownership. For example, a I am proposing to add a new model-level setting, e.g.
This flag can be read via @raymondfeng @superkhau do you have any opinion how to best design this feature? |
// 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) { |
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.
@bajtos Concerning your comment on backward compatibility, the fallback is here : I choose to first check the belongsTo relations and then fallback to userId
/ owner
fields. So no problem of backward compatibility in case userId exist without belongsTo relation. However I feel not comfortable with that because it means that you can resolve the owner role without having any belongsTo
relation defined (which was already the case)
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.
However I feel not comfortable with that because it means that you can resolve the owner role without having any belongsTo relation defined (which was already the case)
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.
@bajtos Considering your comments I'll update my PR ASAP to make it clean |
@bajtos Your proposal sounds solid, my thoughts are pretty much the same as yours. |
@bajtos I would also like to allow model itself to provide |
So will this PR be merged into master now? Or should we wait for a @pierreclr if you need to use this in your production, you may create a bootscript which redefine the |
@raymondfeng I don't really see why having an |
Makes sense.
I agree adding model-specific @pierreclr could you please rework your patch according to the proposal in my #3106 (comment)?
@upupzealot The usual rules apply - we will merge it when our concerns were addressed and the code looks good. Perhaps you can work together with @pierreclr to make this happen earlier? |
Hi @bajtos, sorry for the late reply. I can upgrade my PR according to your comment but I'm not familiar with |
Our code base (the part which is published to npmjs) is still callback-based, we cannot use Promises in The tests are different, using Promises in tests is the direction we want to take, so feel free to use Promises there! Just please try to preserve existing tests unchanged and use Promises only in the newly added code.
See my previous comment #3106 (comment)
Inside To set the flag in your test model, add it to the third argument of app.registry.createModel('Message', {
name: String,
// ...
}, {
relations: {
// ...
},
ownerRelations: true,
// or
// ownerRelations: ['user', 'sender']
}); |
@bajtos working on it but I can't have |
In general, we prefer to keep the work in the same pull request, as it preserves all comments in a single place. That becomes a bit more difficult when the pull request was submitted from the You may find the following article about github and pull requests helpful: http://www.nearform.com/nodecrunch/first-time-with-open-source/ I am closing this pull request in favour of #3140 |
Description
This PR allows us to resolve multiple belongsTo role
Related issues