| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from functools import wraps |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.contrib.auth import REDIRECT_FIELD_NAME |
| 7 |
from django.contrib.auth.decorators import login_required |
| 8 |
from django.core.exceptions import PermissionDenied |
| 9 |
from django.http import HttpRequest, HttpResponseRedirect |
| 10 |
from django.shortcuts import render, resolve_url |
| 11 |
from django.urls import reverse |
| 12 |
|
| 13 |
from allauth.account import app_settings |
| 14 |
from allauth.account.internal.flows import reauthentication |
| 15 |
from allauth.account.internal.flows.email_verification import ( |
| 16 |
send_verification_email_for_user, |
| 17 |
) |
| 18 |
from allauth.account.models import EmailAddress |
| 19 |
from allauth.account.utils import get_next_redirect_url |
| 20 |
from allauth.core.exceptions import ReauthenticationRequired |
| 21 |
from allauth.core.internal import httpkit |
| 22 |
|
| 23 |
|
| 24 |
def verified_email_required( |
| 25 |
function=None, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME |
| 26 |
): |
| 27 |
""" |
| 28 |
Even when email verification is not mandatory during signup, there |
| 29 |
may be circumstances during which you really want to prevent |
| 30 |
unverified users to proceed. This decorator ensures the user is |
| 31 |
authenticated and has a verified email address. If the former is |
| 32 |
not the case then the behavior is identical to that of the |
| 33 |
standard `login_required` decorator. If the latter does not hold, |
| 34 |
email verification mails are automatically resend and the user is |
| 35 |
presented with a page informing them they needs to verify their email |
| 36 |
address. |
| 37 |
""" |
| 38 |
|
| 39 |
def decorator(view_func): |
| 40 |
@login_required(redirect_field_name=redirect_field_name, login_url=login_url) |
| 41 |
def _wrapped_view(request: HttpRequest, *args, **kwargs): |
| 42 |
user = request.user |
| 43 |
assert user.is_authenticated # nosec |
| 44 |
if not EmailAddress.objects.filter(user=user, verified=True).exists(): |
| 45 |
send_verification_email_for_user(request, user) |
| 46 |
if app_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED: |
| 47 |
url = httpkit.add_query_params( |
| 48 |
reverse("account_email_verification_sent"), |
| 49 |
{REDIRECT_FIELD_NAME: request.get_full_path()}, |
| 50 |
) |
| 51 |
return HttpResponseRedirect(url) |
| 52 |
return render(request, "account/verified_email_required.html") |
| 53 |
return view_func(request, *args, **kwargs) |
| 54 |
|
| 55 |
return _wrapped_view |
| 56 |
|
| 57 |
if function: |
| 58 |
return decorator(function) |
| 59 |
return decorator |
| 60 |
|
| 61 |
|
| 62 |
def reauthentication_required( |
| 63 |
function=None, |
| 64 |
redirect_field_name=REDIRECT_FIELD_NAME, |
| 65 |
allow_get=False, |
| 66 |
enabled=None, |
| 67 |
): |
| 68 |
def decorator(view_func): |
| 69 |
@wraps(view_func) |
| 70 |
def _wrapper_view(request: HttpRequest, *args, **kwargs): |
| 71 |
pass_method = allow_get and request.method == "GET" |
| 72 |
ena = (enabled is None) or ( |
| 73 |
enabled(request) if callable(enabled) else enabled |
| 74 |
) |
| 75 |
if ena and not pass_method: |
| 76 |
if ( |
| 77 |
request.user.is_anonymous |
| 78 |
or not reauthentication.did_recently_authenticate(request) |
| 79 |
): |
| 80 |
raise ReauthenticationRequired() |
| 81 |
return view_func(request, *args, **kwargs) |
| 82 |
|
| 83 |
return _wrapper_view |
| 84 |
|
| 85 |
if function: |
| 86 |
return decorator(function) |
| 87 |
return decorator |
| 88 |
|
| 89 |
|
| 90 |
def secure_admin_login(function=None): |
| 91 |
def decorator(view_func): |
| 92 |
@wraps(view_func) |
| 93 |
def _wrapper_view(request: HttpRequest, *args, **kwargs): |
| 94 |
if request.user.is_authenticated: |
| 95 |
if not request.user.is_staff or not request.user.is_active: |
| 96 |
raise PermissionDenied() |
| 97 |
return view_func(request, *args, **kwargs) |
| 98 |
else: |
| 99 |
next_url = get_next_redirect_url(request) |
| 100 |
if not next_url: |
| 101 |
next_url = request.get_full_path() |
| 102 |
login_url = resolve_url(settings.LOGIN_URL) |
| 103 |
login_url = httpkit.add_query_params( |
| 104 |
login_url, {REDIRECT_FIELD_NAME: next_url} |
| 105 |
) |
| 106 |
return HttpResponseRedirect(login_url) |
| 107 |
|
| 108 |
return _wrapper_view |
| 109 |
|
| 110 |
if function: |
| 111 |
return decorator(function) |
| 112 |
return decorator |