-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support LoginRequiredMiddleware
- Loading branch information
Showing
25 changed files
with
432 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from functools import wraps | ||
|
||
from django.contrib.auth import decorators | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
|
||
from allauth.account import app_settings | ||
from allauth.account.stages import LoginStageController | ||
from allauth.account.utils import get_login_redirect_url | ||
|
||
|
||
def _dummy_login_not_required(view_func): | ||
return view_func | ||
|
||
|
||
login_not_required = getattr( | ||
decorators, "login_not_required", _dummy_login_not_required | ||
) | ||
|
||
|
||
def login_stage_required(stage: str, redirect_urlname: str): | ||
def decorator(view_func): | ||
@login_not_required | ||
@wraps(view_func) | ||
def _wrapper_view(request, *args, **kwargs): | ||
if request.user.is_authenticated: | ||
return HttpResponseRedirect(get_login_redirect_url(request)) | ||
login_stage = LoginStageController.enter(request, stage) | ||
if not login_stage: | ||
return HttpResponseRedirect(reverse(redirect_urlname)) | ||
request._login_stage = login_stage | ||
return view_func(request, *args, **kwargs) | ||
|
||
return _wrapper_view | ||
|
||
return decorator | ||
|
||
|
||
def unauthenticated_only(function=None): | ||
def decorator(view_func): | ||
@login_not_required | ||
@wraps(view_func) | ||
def _wrapper_view(request, *args, **kwargs): | ||
if ( | ||
request.user.is_authenticated | ||
# ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = False is going to | ||
# be deprecated. | ||
and app_settings.AUTHENTICATED_LOGIN_REDIRECTS | ||
): | ||
return HttpResponseRedirect(get_login_redirect_url(request)) | ||
return view_func(request, *args, **kwargs) | ||
|
||
return _wrapper_view | ||
|
||
if function: | ||
return decorator(function) | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.