-
Notifications
You must be signed in to change notification settings - Fork 56
Refactor usage of UserPassesTestMixin #3928
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
base: main
Are you sure you want to change the base?
Conversation
Running into an issue with So it seems that if you use I could:
|
2f15d4f is not strictly necessary, but I think it helps by showing that you can and should call super().test_func() if you inherit these classes. |
Thomas and I synchronously discussed this and realized that there is another |
If we all agree that this is the correct solution, then it might be worth adding the guardian mixin to the banned modules in setup.cfg to make sure we use the correct one in the future. |
I just removed the usages of UserPassesTestMixin again, but realized we have the same problem as before. Also replacing From the
And this is because dispatch must be reach there first. You cannot update the dispatch method and still benefit from decorator placed in guardians LoginRequiredMixin. So I think creating the new class is the best way to go. |
Anne and I discussed it synchronously again. Indeed the problem is that you cannot update the dispatch method with a user test in a class where you use guardian LoginRequiredMixin and still have the benefit of checking if the user is logged in first (and if not logged-in automatically skipping the user test). An alternative would be to create dedicated mixins whereever you want to create a user test together with LoginRequiredMixin while you want (or would expect) that the login required check is performed first and stops the chain if it fails. Such a mixin would then inherit AccessMixin and the dispatch method would be updated in the mixin. Then it would still be wrapped by LoginRequiredMixin. But you would need to create such a mixin even if you use it on only one class, so that is not intuitive. And nothing prevents you from forgetting it and updating dispatch in the class itself. We also discussed using this new class everywhere we are now using django's AccessMixin combined with LoginRequiredMixin, since this has the same behaviour (if you update the dispatch method in the final class, the login check is not wrapped.) However, in these cases we add tests not related to the user. So the login check does not necessarily need to go first. If we do want to go this route, I would rename to new class to AccessMixin and ban the use of both UserPassesTestMixin and django's AccessMixin. |
I'm not sure about this one, especially with leaving the default implementation to return |
@jmsmkn Did you have a pattern in mind there? I've not yet found an alternative to the guardian and django implementations, but that may be a case of WYSIATI... |
I have no idea I'm afraid! All options have their drawbacks, I wonder if we should then just stick with what the frameworks provide. |
Replace usage of UserPassesTestMixin with AccessMixin and place the test directly in dispatch to allow for subclassing with additional tests.
Closes https://github.com/DIAGNijmegen/rse-grand-challenge-admin/issues/347