Skip to content
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

Admin login as non-superuser avoids allauth login flow #4766

Closed
jkaeske opened this issue Dec 29, 2023 · 7 comments · Fixed by #5078
Closed

Admin login as non-superuser avoids allauth login flow #4766

jkaeske opened this issue Dec 29, 2023 · 7 comments · Fixed by #5078
Labels

Comments

@jkaeske
Copy link
Contributor

jkaeske commented Dec 29, 2023

What happened?

When logging into the admin console through allauth (DJANGO_ADMIN_FORCE_ALLAUTH=True), everything works fine when logging in as a superuser.
When logging in as a normal user, the user gets redirected to the Django admin site with "You are logged in but do not have access to this page. Do you want to log in as a different user?".
This seems like a way to go around the allauth login flow.

Solution

Currently, this snippet is used to check for a logged-in user:

from django.contrib.auth import decorators

if settings.DJANGO_ADMIN_FORCE_ALLAUTH:
    # Force the `admin` sign in process to go through the `django-allauth` workflow:
    # https://django-allauth.readthedocs.io/en/stable/advanced.html#admin
    admin.site.login = decorators.login_required(admin.site.login)  # type: ignore[method-assign]

But shouldn't it rather check for a superuser like specified in the allauth docu, like so?

from django.contrib.admin.views.decorators import staff_member_required

if settings.DJANGO_ADMIN_FORCE_ALLAUTH:
    # Force the `admin` sign in process to go through the `django-allauth` workflow:
    # https://django-allauth.readthedocs.io/en/stable/advanced.html#admin
    admin.site.login = staff_member_required(
        admin.site.login, login_url=settings.LOGIN_URL
    )
@jkaeske jkaeske added the bug label Dec 29, 2023
@foarsitter
Copy link
Collaborator

When applying your proposal I'm facing ERR_TOO_MANY_REDIRECTS.

@jkaeske
Copy link
Contributor Author

jkaeske commented Jan 26, 2024

It is true, I did not realize that the first time...
Maybe it is a django-allauth problem?
If I log in as a non staff member with the snippet from the allauth docs the too many redirects error is raised
If I take the current admin.site.login = decorators.login_required(admin.site.login) snippet I can go around the allauth login flow.

@foarsitter
Copy link
Collaborator

Perhaps we can destroy the current session when this occurs? The the user is redirected to the correct form with the correct redirect url.

@jkaeske
Copy link
Contributor Author

jkaeske commented Jan 27, 2024

Something like this would work to check if the user is a staff member and if not automatically perform a logout, display an error message and redirect to the login page:

from django.contrib.auth import decorators, logout
from django.shortcuts import redirect
from django.contrib import messages

if settings.DJANGO_ADMIN_FORCE_ALLAUTH:
    # Define a custom AdminSite class to check if the user is a staff member
    class CustomAdminSite(admin.AdminSite):
        def login(self, request, extra_context=None):
            if request.user.is_authenticated and not request.user.is_staff:
                logout(request)
                storage = messages.get_messages(request)
                # Clear all other messages first
                storage.used = True
                messages.error(
                    request, "You are not authorized and have been logged out."
                )
                return redirect("account_login")
            return super().login(request, extra_context)

    admin.site = CustomAdminSite()

    # Force the `admin` sign in process to go through the `django-allauth` workflow:
    # https://django-allauth.readthedocs.io/en/stable/advanced.html#admin
    admin.site.login = decorators.login_required(admin.site.login)  # type: ignore[method-assign]

Not sure if you meant somthing like that

@foarsitter
Copy link
Collaborator

Exactly what I meant! Do you mind providing a PR (when you do, do not forget to translate the error message)?

@jkaeske
Copy link
Contributor Author

jkaeske commented Jan 29, 2024

Perfect!
No worries I will do a PR in the next days :)

@jkaeske
Copy link
Contributor Author

jkaeske commented May 18, 2024

This has now been updated in allauth 0.63.1 and there is a new workaround in the allauth docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants