Exemple #1
0
 def signup(self, request, user):
     assert EmailAddress.objects.filter(user=user).count() == 0
     priority_addresses = []
     adapter = get_adapter()
     stashed_email = adapter.unstash_verified_email(request)
     if stashed_email:
         priority_addresses.append(EmailAddress(user=user,
                                                email=stashed_email,
                                                primary=True,
                                                verified=True))
     email = user_email(user)
     if email:
         priority_addresses.append(EmailAddress(user=user,
                                                email=email,
                                                primary=True,
                                                verified=False))
     addresses, primary = cleanup_email_addresses(request,
                                                  priority_addresses
                                                  + addresses)
     for a in addresses:
         a.user = user
         a.save()
     EmailAddress.objects.fill_cache_for_user(user, addresses)
     if (primary
             and email
             and email.lower() != primary.email.lower()):
         user_email(user, primary.email)
         user.save()
     return primary
Exemple #2
0
def add_user_email(request, user, addresses):
    """
    This is based on allauth.account.utils.setup_user_email, but targets
    the addition of email-address objects to an existing user.
    """
    for a in cleanup_email_addresses(request, addresses)[0]:
        if not EmailAddress.objects.filter(user=user, email=a.email).exists():
            a.user = user
            a.save()
Exemple #3
0
    def pre_social_login(self, request, sociallogin):
        email = sociallogin.user.email

        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            user = None

        social_emails = [addr.email for addr in sociallogin.email_addresses]
        if user and not user.is_active:
            existing_emails_q = EmailAddress.objects.filter(email__in=social_emails,
                                                            user__is_active=True)
            if (existing_emails_q.exists() and
                    all(addr.user != user for addr in existing_emails_q)):
                (EmailAddress.objects.filter(email__in=social_emails)
                                     .exclude(id__in=existing_emails_q)
                                     .update(user=existing_emails_q[0].user))
                user.delete()
                user = existing_emails_q[0].user
            elif sociallogin.account.extra_data.get('mozillian_profile'):
                user.is_active = True
                user.save()

        emails_query = EmailAddress.objects.filter(email=email)
        if not user and emails_query.exists():
            user = emails_query[0].user

        if user:
            # Sync with mozillians.org on each login
            mozillian = get_mozillian_profile(social_emails)
            if mozillian:
                user = populate_user_data(user, sociallogin, mozillian)
                user.save()

            sociallogin.user = user

            # Add additional emails, if any
            addresses, primary = cleanup_email_addresses(request, sociallogin.email_addresses)

            for address in addresses:
                address.user = user
                address.save()

            # If the cleanup_email_addresses does not return something, get the mozillians.org
            # primary email
            if not primary:
                primary_emails = [addr for addr in sociallogin.email_addresses
                                  if addr.primary]
                if primary_emails:
                    # The last email is the primary from mozillians.org
                    primary = primary_emails[-1]

            user.email = primary.email
            user.save()
            EmailAddress.objects.filter(user=user).update(primary=False)
            EmailAddress.objects.filter(user=user, email=primary.email).update(primary=True)

            if sociallogin.is_existing:
                # Social Account already connected, signin please
                _login_social_account(request, sociallogin)
            else:
                # Let's connect the account first
                sociallogin.connect(request, user)