-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix crash in User model's "before delete" hook #3917
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
Conversation
|
Can one of the admins verify this patch? To accept patch and trigger a build add comment ".ok\W+to\W+test." |
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.
Thank you for the pull request! Before we can merge it, we need you to add a new unit-test verifying the fix and preventing regressions in the future.
You can use the commit which introduced "before delete" hook for inspiration, see b53a22b
common/models/user.js
Outdated
|
|
||
| User.observe('before delete', function(ctx, next) { | ||
|
|
||
| // Do nothing and just go to next when the user is not logged in |
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.
when the user is not logged in
This description feels misleading to me. How about this?
Do nothing and when the access control was disabled for user model.
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.
Uh oh, my proposed description is not even proper English. Sorry for the typo! Here is what I meant:
Do nothing when the access control was disabled for this user model.
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.
Ok, I fixed all the linting problem, and excuse my english because I'm not very english. Feel free to modify anything you want.
|
Few more things to fix:
|
c8bdc7d to
71cb8b6
Compare
88a5470 to
682df35
Compare
70f00b0 to
0636e6f
Compare
|
@mcitdev hello, I have rebased the patch on top of the latest master, remove There is one thing I don't understand though. How do you disable "user has many access tokens" relation in your application? Is the solution used by my new test similar to what you use? // Simulate the situation when "accessTokens" relation was not configured
delete PrivateUser.definition.settings.relations.accessTokens;
delete PrivateUser.definition.relations.accessTokens; |
|
I didn't disable access tokens for the user, I do just not use ACL at all by default. This was the scenario I've tested - user with no access control - |
|
@bajtos , Ok I just see your test, this is it, it describes what I'm trying to do |
|
Ok, I think I should describe the scenario: I create the user with no ACL configuration, So I can perform actions on it without needing to login. That's why I do not have the access token, because I'm not logged in. So, I think this is not the same thing, because I didn't disabled the access control manually, but it's disabled by default due to not using the access control. Maybe this situation is not realistic, but it preserves the consistency of the use of acl. |
|
@mcitdev Hmm. Somehow I cannot find the test you are talking about. Did you perhaps forget to push it to GitHub? It's also possible I have accidentally discarded it from this branch. Could you please check your local code, make sure to commit any outstanding changes (run
When you say "perform actions", do you mean to invoke REST API (make HTTP requests), or just call model methods via JavaScript API?
Yes, I agree my test is a different scenario than yours. The trouble is, I am not able to reproduce your scenario. When you take my test and modify the lines removing
As I wrote above, somehow I lost the test you are talking about. Could you please add it to the pull request again? |
|
@bajtos Ok, it was my fault, I reviewed the user test file, I totally agree with your test, I'm with you to use your test instead of mine. |
OK. However, I'd still like to understand how the way how you are configuring your app. Do you really delete It looks like you are not the only one running into this problem, #3921 opened by @ryanxwelch found a different bug that manifests when a user does not have any "accessTokens" relation. It's a mystery to me which I'd like to fully understand before trying to fix the symptoms. |
I do not delete the accessTokens from relations, I just follow the cli commands to generate the custom user from an initial empty server loopback configuration.
It may have a relation with the problem mentionned by @ryanxwelch in #3921 , in that the user has no "accessTokens" relation configured, but more details about the error thrown with the scenario that causes the issue could be helpfull. |
|
I rectified my last comment, meanwhile I reviewed the doc, I still learning the framework, because I'm new on it.
That's why I do not get the warning messages of the lib/application.js because the enableAuth function is not called in my case.
So Initially the custom user has no relation configured by default and it's not inherited from the User model, thus the "accessTokens" relation does not exists yet (this leads me to say that it was not necessary to remove accessTokens from PrivateUser relations to simulate the case in the test, because it does not exist by default until it's configured). That's why I added the lines that handles this case in the "before delete" hook in the user.js model to prevent craching the app with the error : As I said before, maybe this situation is not realistic, but it preserves the consistency of the use of ACL in the application. |
|
@bajtos I've modified the test a little bit, because the last version does not fail even if I remove the line added in the "before delete" hook. This works for me : As I said before, I do not need to delete the "accessTokens" relation, because the "PrivateUser.definition.settings.relations.accessTokens" from the base model will not be used, so it's unnessacary to delete that, instead it will use the child relations wich is by default empty (it can be checked by a simple console.log which will print "undefined" for "PrivateUser.definition.relations.accessTokens") |
2e8f445 to
9a8f1b0
Compare
Interesting. I though I tried to create a new app for this new test without any luck. I confirm that your updated test works like a charm! |
bajtos
left a comment
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'd like to improve your code little bit, see my comments below. I'll make those changes myself to get your fix landed sooner.
package.json
Outdated
| "loopback-context": "^1.0.0", | ||
| "mocha": "^3.0.0", | ||
| "nyc": "^10.1.2", | ||
| "nyc": "^10.3.2", |
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.
Let's revert this change, it's not related to the fix.
test/user.test.js
Outdated
| app.model(PrivateUser, {dataSource: 'db'}); | ||
|
|
||
| var usersId; | ||
| async.series([ |
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.
Let's rewrite this block using promises.
test/user.test.js
Outdated
| it('skips token invalidation when the relation is not configured', (done) => { | ||
| // the test passed when the operation did not crash | ||
| const app = loopback({localRegistry: true, loadBuiltinModels: true}); | ||
| app.set('remoting', {errorHandler: {debug: true, log: 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.
We don't need to change remoting config because we are not making any REST calls.
Update User's "before delete" hook to take into account the case when the related AccessToken model was not configured in the application (attached to a datasource).
|
@slnode ok to test |
|
Landed 🎉 Thank you for contributing the fix and helping me to better understand the issue at hand, @mcitdev ❤️ |
|
@bajtos I'm glad to contribute to loopback, it was my pleasure. |
Description
Update the "before delete" hook to take into account the case where the user is not logged in and therefore does not have any accessToken
Related issues
Checklist
guide