-
Notifications
You must be signed in to change notification settings - Fork 218
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
redirect_unauthenticated_users will cause raise_exception to be ignored #181
Comments
raise_exception = True
to be ignored
raise_exception = True
to be ignored
I tried to replicate this but it passed the test (I got a 302). Did you expect the redirect to happen instead? |
@kennethlove : I think this issue is related to issue #161 (redirect loop).
I've just stumbled upon the same situation. In the following view, I want to check that the user is authenticated and that it belongs to the appropriate group. If the user is not authenticated, I want it to be redirected to the LoginView. If it is authenticated, but does not belong to the right group, I want an exception to be rised.
NOTE: Actually, this problem occurs even without having LoginRequiredMixin as one of the parents. The problem resides in the method handle_no_permission from the class AccessMixin:
The problem is twofold: first, the fact that the default behaviour is a redirection to the login page, and second, how the flag redirect_unauthenticated_users is being used. Consider the following situations:
The solution I'd propose is to refactor the method handle_no_permission to simply raise an exception or redirect the user somewhere other than the login page. This other page to redirect to should be indicated by the programmer by setting the variable redirect_to_url. One of the two behaviours has to be defined by the programmer (raising an exception or redirecting the user).
Now, if a programmer wants to also have unauthenticated users redirected to the login page, then they should use LoginRequiredMixin in addition to whatever permission or group they are trying to check. Of course, the above refactoring of handle_no_permission also requires the refactoring of dispatch in LoginRequiredMixin, so that any user that is not authenticated is immediately redirected to the login page. There's no need to run any code from handle_no_permission. LoginRequiredMixin should do one thing only and do it well: if a user is not authenticated, redirect it to the login page. No more, no less.
|
Just to add another to this, I've got a class ClientListView(LoginRequiredMixin, SuperuserRequiredMixin, FormView):
"""
List of clients
"""
http_method_names = ['get', 'post']
form_class = ClientListForm
raise_exception = True
login_url = settings.LOGIN_URL
redirect_unauthenticated_users = True Oddly I was seeing the unauthenticated users redirected to a page they can access while I had I wondered if a |
@renaudManda : I've implemented the solution locally; let me know if you'd be interested in having a look at it. |
@gkeller2 yes of course. I did myself as well. You can check on https://gist.github.com/renaudManda/9db2a8185966b01ee8e9 |
How about a pull request on this from @gkeller2 or @renaudManda ? |
@kennethlove : done #196 I did it in a hurry, forking the project, creating a branch and making the changes I did in my local project. I'm a bit busy at the moment, but do contact me for anything and I'll try to assist. I didn't run any tests -- sorry for that! |
@kennethlove : have you checked out the code? |
@gkeller2 @kennethlove I tried my own shot at making a fix -- see #200 -- I think this is a cleaner fix because there's no API changes. I tested the fix inline to my project and it does the trick, but I'm having problem getting my test case to fail and succeed when it should. Can you take a look? |
bumping for interest |
+1 for @cornmander PR :) |
When chaining
LoginRequiredMixin
and anotherAccessMixin
, settingraise_exception = True
andredirect_unauthenticated_users = True
doesn't seem to do the right thing.The text was updated successfully, but these errors were encountered: