Esempio n. 1
0
    def testCleanup(self):
        user1 = User(username='******',
                     email='*****@*****.**',
                     is_active=True)
        user1.save()

        old_enough = (datetime.now() -
                      timedelta(days=email_verification_days() + 1))
        not_old_enough = (datetime.now() -
                          timedelta(days=email_verification_days() - 1))

        email1 = UserEmail(user=user1,
                           email='*****@*****.**',
                           verified=True,
                           default=True,
                           verification_key=UserEmail.VERIFIED + 'asd',
                           code_creation_date=old_enough)
        email1.save()

        user2 = User(username='******',
                     email='*****@*****.**',
                     is_active=False)
        user2.save()

        email2 = UserEmail(user=user2,
                           email='*****@*****.**',
                           verified=False,
                           default=True,
                           verification_key='key1',
                           code_creation_date=old_enough)
        email2.save()

        user3 = User(username='******',
                     email='*****@*****.**',
                     is_active=False)
        user3.save()

        email3 = UserEmail(user=user3,
                           email='*****@*****.**',
                           verified=False,
                           default=True,
                           verification_key='key2',
                           code_creation_date=not_old_enough)
        email3.save()

        UserEmail.objects.delete_expired()

        user_ids = [user.id for user in User.objects.all()]
        user_email_ids = [
            user_email.id for user_email in UserEmail.objects.all()
        ]

        self.assertEqual(list(sorted(user_ids)),
                         list(sorted([user1.id, user3.id])))
        self.assertEqual(list(sorted(user_email_ids)),
                         list(sorted([email1.id, email3.id])))
Esempio n. 2
0
def reset_password(request, reset_code,
    template_name='emailauth/reset_password.html'):

    user_email = get_object_or_404(UserEmail, verification_key=reset_code)
    if (user_email.verification_key == UserEmail.VERIFIED or
        user_email.code_creation_date +
        timedelta(days=email_verification_days()) < datetime.now()):

        raise Http404()

    if request.method == 'POST':
        form = PasswordResetForm(request.POST)
        if form.is_valid():
            user = user_email.user
            user.set_password(form.cleaned_data['password1'])
            user.save()

            user_email.verification_key = UserEmail.VERIFIED
            user_email.save()

            from django.contrib.auth import login
            user.backend = 'emailauth.backends.EmailBackend'
            login(request, user)
            return HttpResponseRedirect(reverse('emailauth_account'))
    else:
        form = PasswordResetForm()

    context = RequestContext(request)
    return render_to_response(template_name,
        {'form': form},
        context_instance=context)
Esempio n. 3
0
def reset_password(request, reset_code,
    template_name='emailauth/reset_password.html'):

    user_email = get_object_or_404(UserEmail, verification_key=reset_code)
    if (user_email.verification_key == UserEmail.VERIFIED or
        user_email.code_creation_date +
        timedelta(days=email_verification_days()) < datetime.now()):

        raise Http404()

    if request.method == 'POST':
        form = PasswordResetForm(request.POST)
        if form.is_valid():
            user = user_email.user
            user.set_password(form.cleaned_data['password1'])
            user.save()

            user_email.verification_key = UserEmail.VERIFIED
            user_email.save()

            from django.contrib.auth import login
            user.backend = 'emailauth.backends.EmailBackend'
            login(request, user)
            return HttpResponseRedirect(reverse('emailauth_account'))
    else:
        form = PasswordResetForm()

    context = RequestContext(request)
    return render_to_response(template_name,
        {'form': form},
        context_instance=context)
Esempio n. 4
0
    def send_invite_email(self, network_name):
        current_site = Site.objects.get_current()

        subject = render_to_string('emailauth/invite_email_subject.txt',
            {'site': current_site})
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        emails = set()
        if self.user is not None:
            for email in self.__class__.objects.filter(user=self.user):
                emails.add(email.email)
        emails.add(self.email)
        first_email = len(emails) == 1

        message = render_to_string('emailauth/invite_email.txt', {
            'verification_key': self.verification_key,
            'expiration_days': email_verification_days(),
            'site': current_site,
            'first_email': first_email,
            'network_name': network_name,
        })

        self.code_creation_date = datetime.datetime.now()

        django.core.mail.send_mail(subject, message,
            settings.DEFAULT_FROM_EMAIL, [self.email])
Esempio n. 5
0
    def send_verification_email(self, first_name=None):
        current_site = Site.objects.get_current()

        subject = render_to_string("emailauth/verification_email_subject.txt", {"site": current_site})
        # Email subject *must not* contain newlines
        subject = "".join(subject.splitlines())

        emails = set()
        if self.user is not None:
            for email in self.__class__.objects.filter(user=self.user):
                emails.add(email.email)
        emails.add(self.email)
        first_email = len(emails) == 1

        if first_name is None:
            first_name = self.user.first_name

        message = render_to_string(
            "emailauth/verification_email.txt",
            {
                "verification_key": self.verification_key,
                "expiration_days": email_verification_days(),
                "site": current_site,
                "first_name": first_name,
                "first_email": first_email,
            },
        )

        self.code_creation_date = datetime.datetime.now()

        django.core.mail.send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [self.email])
Esempio n. 6
0
def verify(request, verification_key, template_name='emailauth/verify.html',
    extra_context=None, callback=default_verify_callback):

    verification_key = verification_key.lower() # Normalize before trying anything with it.
    email = UserEmail.objects.verify(verification_key)

    
    if email is not None:
        email.user.message_set.create(message='%s email confirmed.' % email.email)

        if use_single_email():
            email.default = True
            email.save()
            UserEmail.objects.filter(user=email.user, default=False).delete()

    if email is not None and callback is not None:
        cb_result = callback(request, email)
        if cb_result is not None:
            return cb_result

    context = RequestContext(request)
    if extra_context is not None:
        for key, value in extra_context.items():
            context[key] = value() if callable(value) else value

    return render_to_response(template_name,
        {
            'email': email,
            'expiration_days': email_verification_days(),
        },
        context_instance=context)
Esempio n. 7
0
def verify(request, verification_key, template_name='emailauth/verify.html',
    extra_context=None, callback=default_verify_callback):

    verification_key = verification_key.lower() # Normalize before trying anything with it.
    email = UserEmail.objects.verify(verification_key)

    
    if email is not None:
        email.user.message_set.create(message=_('%s email confirmed.') % email.email)

        if use_single_email():
            email.default = True
            email.save()
            UserEmail.objects.filter(user=email.user, default=False).delete()

    if email is not None and callback is not None:
        cb_result = callback(request, email)
        if cb_result is not None:
            return cb_result

    context = RequestContext(request)
    if extra_context is not None:
        for key, value in extra_context.items():
            context[key] = value() if callable(value) else value

    return render_to_response(template_name,
        {
            'email': email,
            'expiration_days': email_verification_days(),
        },
        context_instance=context)
Esempio n. 8
0
    def send_verification_email(self, first_name=None):
        current_site = Site.objects.get_current()
        
        subject = render_to_string('emailauth/verification_email_subject.txt',
            {'site': current_site})
        # Email subject *must not* contain newlines
        subject = ''.join(subject.splitlines())

        emails = set()
        if self.user is not None:
            for email in self.__class__.objects.filter(user=self.user):
                emails.add(email.email)
        emails.add(self.email)
        first_email = len(emails) == 1

        if first_name is None:
            first_name = self.user.first_name
        
        message = render_to_string('emailauth/verification_email.txt', {
            'verification_key': self.verification_key,
            'expiration_days': email_verification_days(),
            'site': current_site,
            'first_name': first_name,
            'first_email': first_email,
        })

        self.code_creation_date = datetime.datetime.now()

        django.core.mail.send_mail(subject, message,
            settings.DEFAULT_FROM_EMAIL, [self.email])
Esempio n. 9
0
    def testPasswordResetFail2(self):
        reset_url, user_email = self.prepare()
        client = Client()
        user_email.code_creation_date = (
            datetime.now() - timedelta(days=email_verification_days() + 1))
        user_email.save()

        response = client.get(reset_url)
        self.assertStatusCode(response, Status.NOT_FOUND)
Esempio n. 10
0
    def testPasswordResetFail2(self):
        reset_url, user_email = self.prepare()
        client = Client()
        user_email.code_creation_date = (datetime.now() -
            timedelta(days=email_verification_days() + 1))
        user_email.save()

        response = client.get(reset_url)
        self.assertStatusCode(response, Status.NOT_FOUND)
Esempio n. 11
0
def request_password_reset(request,
    template_name='emailauth/request_password.html'):

    if request.method == 'POST':
        form = PasswordResetRequestForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            user_email = UserEmail.objects.get(email=email)
            user_email.make_new_key()
            user_email.save()

            current_site = Site.objects.get_current()

            subject = render_to_string(
                'emailauth/request_password_email_subject.txt',
                {'site': current_site})
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('emailauth/request_password_email.txt', {
                'reset_code': user_email.verification_key,
                'expiration_days': email_verification_days(),
                'site': current_site,
                'first_name': user_email.user.first_name,
            })

            django.core.mail.send_mail(subject, message,
                settings.DEFAULT_FROM_EMAIL, [email])

            return HttpResponseRedirect(
                reverse('emailauth_request_password_reset_continue',
                args=[email]))
    else:
        form = PasswordResetRequestForm()

    context = RequestContext(request)
    return render_to_response(template_name,
        {
            'form': form,
            'expiration_days': email_verification_days(),
        },
        context_instance=context)
Esempio n. 12
0
def request_password_reset(request,
    template_name='emailauth/request_password.html'):

    if request.method == 'POST':
        form = PasswordResetRequestForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            user_email = UserEmail.objects.get(email=email)
            user_email.make_new_key()
            user_email.save()

            current_site = Site.objects.get_current()

            subject = render_to_string(
                'emailauth/request_password_email_subject.txt',
                {'site': current_site})
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('emailauth/request_password_email.txt', {
                'reset_code': user_email.verification_key,
                'expiration_days': email_verification_days(),
                'site': current_site,
                'first_name': user_email.user.first_name,
            })

            django.core.mail.send_mail(subject, message,
                settings.DEFAULT_FROM_EMAIL, [email])

            return HttpResponseRedirect(
                reverse('emailauth_request_password_reset_continue',
                args=[quote_plus(email)]))
    else:
        form = PasswordResetRequestForm()

    context = RequestContext(request)
    return render_to_response(template_name,
        {
            'form': form,
            'expiration_days': email_verification_days(),
        },
        context_instance=context)
Esempio n. 13
0
    def delete_expired(self):
        date_threshold = datetime.datetime.now() - datetime.timedelta(days=email_verification_days())
        expired_emails = self.filter(code_creation_date__lt=date_threshold)

        for email in expired_emails:
            if not email.verified:
                user = email.user
                emails = user.useremail_set.all()
                if not user.is_active:
                    user.delete()
                else:
                    email.delete()
Esempio n. 14
0
    def testCleanup(self):
        user1 = User(username='******', email='*****@*****.**', is_active=True)
        user1.save()

        old_enough = (datetime.now() - timedelta(days=email_verification_days() + 1))
        not_old_enough = (datetime.now() -
            timedelta(days=email_verification_days() - 1))

        email1 = UserEmail(user=user1, email='*****@*****.**',
            verified=True, default=True, 
            verification_key=UserEmail.VERIFIED + 'asd',
            code_creation_date=old_enough)
        email1.save() 

        user2 = User(username='******', email='*****@*****.**', is_active=False)
        user2.save()

        email2 = UserEmail(user=user2, email='*****@*****.**',
            verified=False, default=True, 
            verification_key='key1',
            code_creation_date=old_enough)
        email2.save()

        user3 = User(username='******', email='*****@*****.**', is_active=False)
        user3.save()

        email3 = UserEmail(user=user3, email='*****@*****.**',
            verified=False, default=True, 
            verification_key='key2',
            code_creation_date=not_old_enough)
        email3.save()

        UserEmail.objects.delete_expired()

        user_ids = [user.id for user in User.objects.all()]
        user_email_ids = [user_email.id for user_email in UserEmail.objects.all()]

        self.assertEqual(list(sorted(user_ids)), list(sorted([user1.id, user3.id])))
        self.assertEqual(list(sorted(user_email_ids)), list(sorted([email1.id, email3.id])))
Esempio n. 15
0
 def delete_expired(self):
     date_threshold = (datetime.datetime.now() -
         datetime.timedelta(days=email_verification_days()))
     expired_emails = self.filter(code_creation_date__lt=date_threshold)
 
     for email in expired_emails:
         if not email.verified:
             user = email.user
             emails = user.useremail_set.all()
             if not user.is_active:
                 user.delete()
             else:
                 email.delete()
Esempio n. 16
0
 def verification_key_expired(self):
     expiration_date = datetime.timedelta(days=email_verification_days())
     return self.verification_key == self.VERIFIED or (
         self.code_creation_date + expiration_date <= datetime.datetime.now()
     )
Esempio n. 17
0
 def verification_key_expired(self):
     expiration_date = datetime.timedelta(days=email_verification_days())
     return (self.verification_key == self.VERIFIED or
         (self.code_creation_date + expiration_date <= datetime.datetime.now()))