| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.contrib import messages |
| 4 |
from django.core.exceptions import PermissionDenied, ValidationError |
| 5 |
from django.http import HttpRequest, HttpResponseRedirect |
| 6 |
|
| 7 |
from allauth import app_settings as allauth_settings |
| 8 |
from allauth.account import app_settings as account_settings |
| 9 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 10 |
from allauth.account.internal import flows |
| 11 |
from allauth.account.models import EmailAddress |
| 12 |
from allauth.core.exceptions import ReauthenticationRequired |
| 13 |
from allauth.socialaccount import signals |
| 14 |
from allauth.socialaccount.adapter import get_adapter |
| 15 |
from allauth.socialaccount.models import SocialAccount, SocialLogin |
| 16 |
|
| 17 |
|
| 18 |
def validate_disconnect(request: HttpRequest, account) -> None: |
| 19 |
""" |
| 20 |
Validate whether or not the socialaccount account can be |
| 21 |
safely disconnected. |
| 22 |
""" |
| 23 |
accounts = SocialAccount.objects.filter(user_id=account.user_id) |
| 24 |
is_last = not accounts.exclude(pk=account.pk).exists() |
| 25 |
adapter = get_adapter() |
| 26 |
if is_last: |
| 27 |
if allauth_settings.SOCIALACCOUNT_ONLY: |
| 28 |
raise adapter.validation_error("disconnect_last") |
| 29 |
# No usable password would render the local account unusable |
| 30 |
if not account.user.has_usable_password(): |
| 31 |
raise adapter.validation_error("no_password") |
| 32 |
# No email address, no password reset |
| 33 |
if ( |
| 34 |
account_settings.EMAIL_VERIFICATION |
| 35 |
== account_settings.EmailVerificationMethod.MANDATORY |
| 36 |
): |
| 37 |
if not EmailAddress.objects.filter( |
| 38 |
user=account.user, verified=True |
| 39 |
).exists(): |
| 40 |
raise adapter.validation_error("no_verified_email") |
| 41 |
adapter.validate_disconnect(account, accounts) |
| 42 |
|
| 43 |
|
| 44 |
def disconnect(request: HttpRequest, account) -> None: |
| 45 |
if account_settings.REAUTHENTICATION_REQUIRED: |
| 46 |
flows.reauthentication.raise_if_reauthentication_required(request) |
| 47 |
|
| 48 |
get_account_adapter().add_message( |
| 49 |
request, |
| 50 |
messages.INFO, |
| 51 |
"socialaccount/messages/account_disconnected.txt", |
| 52 |
) |
| 53 |
provider = account.get_provider() |
| 54 |
account.delete() |
| 55 |
signals.social_account_removed.send( |
| 56 |
sender=SocialAccount, request=request, socialaccount=account |
| 57 |
) |
| 58 |
get_adapter().send_notification_mail( |
| 59 |
"socialaccount/email/account_disconnected", |
| 60 |
request.user, |
| 61 |
context={ |
| 62 |
"account": account, |
| 63 |
"provider": provider, |
| 64 |
}, |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
def resume_connect(request: HttpRequest, serialized_state): |
| 69 |
sociallogin = SocialLogin.deserialize(serialized_state) |
| 70 |
return connect(request, sociallogin) |
| 71 |
|
| 72 |
|
| 73 |
def connect(request: HttpRequest, sociallogin: SocialLogin): |
| 74 |
try: |
| 75 |
ok, action, message = do_connect(request, sociallogin) |
| 76 |
except PermissionDenied: |
| 77 |
# This should not happen. Simply redirect to the connections |
| 78 |
# view (which has a login required) |
| 79 |
connect_redirect_url = get_adapter().get_connect_redirect_url( |
| 80 |
request, sociallogin.account |
| 81 |
) |
| 82 |
return HttpResponseRedirect(connect_redirect_url) |
| 83 |
except ReauthenticationRequired: |
| 84 |
return flows.reauthentication.stash_and_reauthenticate( |
| 85 |
request, |
| 86 |
sociallogin.serialize(), |
| 87 |
"allauth.socialaccount.internal.flows.connect.resume_connect", |
| 88 |
) |
| 89 |
except ValidationError: |
| 90 |
ok, action, message = ( |
| 91 |
False, |
| 92 |
None, |
| 93 |
"socialaccount/messages/account_connected_other.txt", |
| 94 |
) |
| 95 |
level = messages.INFO if ok else messages.ERROR |
| 96 |
default_next = get_adapter().get_connect_redirect_url(request, sociallogin.account) |
| 97 |
next_url = sociallogin.get_redirect_url(request) or default_next |
| 98 |
get_account_adapter(request).add_message( |
| 99 |
request, |
| 100 |
level, |
| 101 |
message, |
| 102 |
message_context={"sociallogin": sociallogin, "action": action}, |
| 103 |
) |
| 104 |
return HttpResponseRedirect(next_url) |
| 105 |
|
| 106 |
|
| 107 |
def do_connect(request: HttpRequest, sociallogin: SocialLogin): |
| 108 |
if request.user.is_anonymous: |
| 109 |
raise PermissionDenied() |
| 110 |
if account_settings.REAUTHENTICATION_REQUIRED: |
| 111 |
flows.reauthentication.raise_if_reauthentication_required(request) |
| 112 |
message = "socialaccount/messages/account_connected.txt" |
| 113 |
action = None |
| 114 |
ok = True |
| 115 |
if sociallogin.is_existing: |
| 116 |
if sociallogin.user != request.user: |
| 117 |
# Social account of other user. For now, this scenario |
| 118 |
# is not supported. Issue is that one cannot simply |
| 119 |
# remove the social account from the other user, as |
| 120 |
# that may render the account unusable. |
| 121 |
raise get_adapter().validation_error("connected_other") |
| 122 |
elif not sociallogin.account._state.adding: |
| 123 |
action = "updated" |
| 124 |
message = "socialaccount/messages/account_connected_updated.txt" |
| 125 |
else: |
| 126 |
action = "added" |
| 127 |
sociallogin.connect(request, request.user) |
| 128 |
else: |
| 129 |
# New account, let's connect |
| 130 |
action = "added" |
| 131 |
sociallogin.connect(request, request.user) |
| 132 |
return ok, action, message |