1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #31405 -- Added LoginRequiredMiddleware.

Co-authored-by: Adam Johnson <me@adamj.eu>
Co-authored-by: Mehmet İnce <mehmet@mehmetince.net>
Co-authored-by: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com>
This commit is contained in:
Hisham Mahmood
2024-05-05 11:21:28 +05:00
committed by Sarah Boyce
parent 7857507c7f
commit c7fc9f20b4
17 changed files with 633 additions and 12 deletions

View File

@@ -1,6 +1,10 @@
from django.contrib import admin
from django.contrib.auth import views
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.decorators import (
login_not_required,
login_required,
permission_required,
)
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.urls import urlpatterns as auth_urlpatterns
from django.contrib.auth.views import LoginView
@@ -9,6 +13,8 @@ from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.template import RequestContext, Template
from django.urls import path, re_path, reverse_lazy
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.cache import never_cache
from django.views.i18n import set_language
@@ -88,6 +94,42 @@ class CustomDefaultRedirectURLLoginView(LoginView):
return "/custom/"
class EmptyResponseBaseView(View):
def get(self, request, *args, **kwargs):
return HttpResponse()
@method_decorator(login_not_required, name="dispatch")
class PublicView(EmptyResponseBaseView):
pass
class ProtectedView(EmptyResponseBaseView):
pass
@method_decorator(
login_required(login_url="/custom_login/", redirect_field_name="step"),
name="dispatch",
)
class ProtectedViewWithCustomLoginRequired(EmptyResponseBaseView):
pass
@login_not_required
def public_view(request):
return HttpResponse()
def protected_view(request):
return HttpResponse()
@login_required(login_url="/custom_login/", redirect_field_name="step")
def protected_view_with_login_required_decorator(request):
return HttpResponse()
# special urls for auth test cases
urlpatterns = auth_urlpatterns + [
path(
@@ -198,7 +240,14 @@ urlpatterns = auth_urlpatterns + [
"login_and_permission_required_exception/",
login_and_permission_required_exception,
),
path("public_view/", PublicView.as_view()),
path("public_function_view/", public_view),
path("protected_view/", ProtectedView.as_view()),
path("protected_function_view/", protected_view),
path(
"login_required_decorator_view/", protected_view_with_login_required_decorator
),
path("login_required_cbv_view/", ProtectedViewWithCustomLoginRequired.as_view()),
path("setlang/", set_language, name="set_language"),
# This line is only required to render the password reset with is_admin=True
path("admin/", admin.site.urls),
]