Merge pull request #701 from Gluejar/social-auth-reset

Social auth reset
pull/43/head
eshellman 2017-09-22 18:29:54 -04:00 committed by GitHub
commit d4ecca00ba
13 changed files with 69 additions and 57 deletions

View File

@ -119,13 +119,13 @@ class BookLoaderTests(TestCase):
if not (mocking or settings.TEST_INTEGRATION):
return
# edition
edition = bookloader.add_by_isbn('9781594200090')
edition = bookloader.add_by_isbn('9780143034759')
self.assertEqual(edition.title, u'Alexander Hamilton')
self.assertEqual(edition.publication_date, u'2004')
self.assertEqual(edition.publisher, u'Perseus Books Group')
self.assertEqual(edition.isbn_10, '1594200092')
self.assertEqual(edition.isbn_13, '9781594200090')
self.assertEqual(edition.googlebooks_id, 'y1_R-rjdcb0C')
self.assertEqual(edition.publication_date, u'2005')
self.assertEqual(edition.publisher, u'Penguin')
self.assertEqual(edition.isbn_10, '0143034758')
self.assertEqual(edition.isbn_13, '9780143034759')
self.assertEqual(edition.googlebooks_id, '4iafgTEhU3QC')
# authors
self.assertEqual(edition.authors.all().count(), 1)
@ -133,12 +133,12 @@ class BookLoaderTests(TestCase):
# work
self.assertTrue(edition.work)
self.assertEqual(edition.work.googlebooks_id, 'y1_R-rjdcb0C')
self.assertEqual(edition.work.first_isbn_13(), '9781594200090')
self.assertEqual(edition.work.googlebooks_id, '4iafgTEhU3QC')
self.assertEqual(edition.work.first_isbn_13(), '9780143034759')
# test duplicate pubname
ed2 = Edition.objects.create(work=edition.work)
ed2.set_publisher(u'Perseus Books Group')
ed2.set_publisher(u'Penguin')
# publisher names
old_pub_name = edition.publisher_name
@ -149,7 +149,7 @@ class BookLoaderTests(TestCase):
self.assertEqual(edition.work.publishers().count(), 1)
old_pub_name.publisher = pub
old_pub_name.save()
edition.set_publisher(u'Perseus Books Group')
edition.set_publisher(u'Penguin')
self.assertEqual(edition.publisher, u'test publisher name') # Perseus has been aliased
def test_language_locale_mock(self):

View File

@ -9,7 +9,7 @@
<div>
<h2>Other Account Tools</h2>
<ul>
<li>Want to <a href="{% url 'auth_password_change' %}">change your password</a>?</li>
<li>Want to <a href="{% url 'libraryauth_password_change' %}">change your password</a>?</li>
<li>... or <a href="{% url 'manage_account' %}">manage your pledges and payment info</a>?</li>
<li>... or <a href="{% url 'edit_user' %}">change your username</a>?</li>
<li>... or <a href="{% url 'notification_notice_settings' %}">manage your contact preferences</a>?</li>

View File

@ -22,7 +22,7 @@ Make sure the username box has your <b>username, not your email</b> -- some brow
<br />
<a href="{% url 'auth_password_reset' %}?next={% url 'receive_gift' gift.acq.nonce %}">Forgot</a> your password? <a href="{% url 'registration_register' %}?next={% url 'receive_gift' gift.acq.nonce %}">Need an account</a>? <a href="/faq/basics/account">Other questions</a>?
<a href="{% url 'libraryauth_password_reset' %}?next={% url 'receive_gift' gift.acq.nonce %}">Forgot</a> your password? <a href="{% url 'registration_register' %}?next={% url 'receive_gift' gift.acq.nonce %}">Need an account</a>? <a href="/faq/basics/account">Other questions</a>?
<br /><br />

View File

@ -6,7 +6,7 @@
<h2> Other Account Management Tools </h2>
<ul>
<li>Want to <a href="{% url 'auth_password_change' %}">change your password</a>?</li>
<li>Want to <a href="{% url 'libraryauth_password_change' %}">change your password</a>?</li>
<li>... or <a href="{% url 'notification_notice_settings' %}">manage your contact preferences</a>?</li>
<li>... or <a href="{% url 'email_change' %}">change your email address</a>?</li>
<li>... or <a href="{% url 'edit_user' %}">change your username</a>?</li>

View File

@ -104,7 +104,7 @@ You can complete your last transaction by <a href="{% url 'fund' user.profile.la
<h2> Other Account Management Tools </h2>
<ul>
<li>Want to <a href="{% url 'auth_password_change' %}">change your password</a>?</li>
<li>Want to <a href="{% url 'libraryauth_password_change' %}">change your password</a>?</li>
<li>... or <a href="{% url 'notification_notice_settings' %}">manage your contact preferences</a>?</li>
<li>... or <a href="{% url 'email_change' %}">change your email address</a>?</li>
<li>... or <a href="{% url 'edit_user' %}">change your username</a>?</li>

View File

@ -11,7 +11,7 @@
</div>
<br />
<div>
<a href="{% url 'auth_password_reset' %}">Forgot</a> your password?
<a href="{% url 'libraryauth_password_reset' %}">Forgot</a> your password?
</div>
{% else %}
<div>

View File

@ -71,7 +71,7 @@ function put_un_in_cookie(){
</div>
<div class="halfcolumn1 login_box">
<h3>Already Have an Unglue.it Account?</h3>
<a href="{% url 'auth_password_reset' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Forgot</a> your password? </li>
<a href="{% url 'libraryauth_password_reset' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Forgot</a> your password? </li>
{% include "login_form.html" %}
</div>

View File

@ -28,7 +28,7 @@ Make sure the username box has your <b>username, not your email</b> -- some brow
<br />
<a href="{% url 'auth_password_reset' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Forgot</a> your password? <a href="{% url 'registration_register' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Need an account</a>? <a href="/faq/basics/account">Other questions</a>?
<a href="{% url 'libraryauth_password_reset' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Forgot</a> your password? <a href="{% url 'registration_register' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}{{ request.get_full_path|urlencode}}{% endif %}">Need an account</a>? <a href="/faq/basics/account">Other questions</a>?
<br /><br />

View File

@ -4,25 +4,12 @@
{% block doccontent %}
{% if request.user.is_authenticated %}
{% if not request.user.has_usable_password %}
<div>
Because you registered using your account on another site (such as Google), you'll need to reset your password before you can change it.
</div>
<form method='post' action='{% url 'social_auth_reset_password' %}'>{% csrf_token %}
<input id="id_email" type="hidden" name="email" value="{{request.user.email}}" />
<p><input type='submit' value="Reset password" /></p>
</form>
{% else %}
<form method='post' action=''>{% csrf_token %}
{{ form.as_p }}
<p><input type='submit' value="Change password" /></p>
</form>
{% endif %}
{% else %}
<div>You must be logged in to change your password.</div>
<a href="{% url 'superlogin' %}?next={% if request.GET.next %}{{ request.GET.next|urlencode }}{% else %}/next/{% endif %}" class="nounderline"><div class="actionbutton">Log in</div></a>

View File

@ -17,7 +17,7 @@
<div>
<h2>Other Account Tools</h2>
<ul>
<li>Want to <a href="{% url 'auth_password_change' %}">change your password</a>?</li>
<li>Want to <a href="{% url 'libraryauth_password_change' %}">change your password</a>?</li>
<li>... or <a href="{% url 'manage_account' %}">manage your pledges and payment info</a>?</li>
<li>... or <a href="{% url 'email_change' %}">change your email address</a>?</li>
<li>... or <a href="{% url 'notification_notice_settings' %}">manage your contact preferences</a>?</li>

View File

@ -1,6 +1,7 @@
import logging
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm, PasswordResetForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from registration.forms import RegistrationForm
@ -57,7 +58,6 @@ class RegistrationFormNoDisposableEmail(RegistrationForm):
if is_disposable(self.cleaned_data['email']):
raise forms.ValidationError(_("Please supply a permanent email address."))
return self.cleaned_data['email']
class AuthForm(AuthenticationForm):
def __init__(self, request=None, *args, **kwargs):
@ -67,6 +67,22 @@ class AuthForm(AuthenticationForm):
else:
super(AuthForm, self).__init__(*args, **kwargs)
class SocialAwarePasswordResetForm(PasswordResetForm):
def get_users(self, email):
"""
Send the reset form even if the user password is not usable
"""
active_users = get_user_model()._default_manager.filter(
email__iexact=email, is_active=True)
return active_users
def clean_email(self):
email = self.cleaned_data['email']
if not get_user_model().objects.filter(email__iexact=email, is_active=True).exists():
raise forms.ValidationError("There aren't ungluers with that email address!")
return email
class NewLibraryForm(forms.ModelForm):
username = forms.RegexField(
label=_("Library Username"),

View File

@ -1,8 +1,8 @@
from django.conf.urls import patterns, url, include
from django.core.urlresolvers import reverse
#from django.views.generic.simple import direct_to_template
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import password_reset
from . import views, models, forms
from .views import superlogin
@ -20,23 +20,23 @@ class ExtraContextTemplateView(TemplateView):
return context
urlpatterns = [
url(r"^libraryauth/(?P<library_id>\d+)/join/$", views.join_library, name="join_library"),
url(r"^libraryauth/(?P<library_id>\d+)/deny/$", TemplateView.as_view(template_name='libraryauth/denied.html'), name="bad_library"),
url(r"^libraryauth/(?P<library_id>\d+)/users/$", views.library, {'template':'libraryauth/users.html'}, name="library_users"),
url(r"^libraryauth/(?P<library_id>\d+)/admin/$", login_required(views.UpdateLibraryView.as_view()), name="library_admin"),
url(r"^libraryauth/(?P<library_id>\d+)/login/$", views.login_as_library, name="library_login"),
url(r"^libraryauth/create/$", login_required(views.CreateLibraryView.as_view()), name="library_create"),
url(r"^libraryauth/list/$", ExtraContextTemplateView.as_view(
url(r'^libraryauth/(?P<library_id>\d+)/join/$', views.join_library, name='join_library'),
url(r'^libraryauth/(?P<library_id>\d+)/deny/$', TemplateView.as_view(template_name='libraryauth/denied.html'), name='bad_library'),
url(r'^libraryauth/(?P<library_id>\d+)/users/$', views.library, {'template':'libraryauth/users.html'}, name='library_users'),
url(r'^libraryauth/(?P<library_id>\d+)/admin/$', login_required(views.UpdateLibraryView.as_view()), name='library_admin'),
url(r'^libraryauth/(?P<library_id>\d+)/login/$', views.login_as_library, name='library_login'),
url(r'^libraryauth/create/$', login_required(views.CreateLibraryView.as_view()), name='library_create'),
url(r'^libraryauth/list/$', ExtraContextTemplateView.as_view(
template_name='libraryauth/list.html',
extra_context={'libraries_to_show':'approved'}
), name="library_list"),
url(r"^libraryauth/unapproved/$", ExtraContextTemplateView.as_view(
), name='library_list'),
url(r'^libraryauth/unapproved/$', ExtraContextTemplateView.as_view(
template_name='libraryauth/list.html',
extra_context={'libraries_to_show':'new'}
), name="new_libraries"),
), name='new_libraries'),
url(r'^accounts/register/$', views.CustomRegistrationView.as_view(), name='registration_register'),
url(r'^accounts/superlogin/$', views.superlogin, name='superlogin'),
url(r"^accounts/superlogin/welcome/$", ExtraContextTemplateView.as_view(
url(r'^accounts/superlogin/welcome/$', ExtraContextTemplateView.as_view(
template_name='registration/welcome.html',
extra_context={'suppress_search_box': True,}
) ),
@ -50,12 +50,21 @@ urlpatterns = [
{'template_name': 'registration/activation_complete.html'}),
url(r'^accounts/login-error/$', superlogin,
{'template_name': 'registration/from_error.html'}),
url(r'^accounts/edit/$', views.edit_user, name="edit_user"),
url(r"^accounts/login/welcome/$", ExtraContextTemplateView.as_view(
url(r'^accounts/edit/$', views.edit_user, name='edit_user'),
url(r'^accounts/login/welcome/$', ExtraContextTemplateView.as_view(
template_name='registration/welcome.html',
extra_context={'suppress_search_box': True,}
) ),
url(r'^socialauth/reset_password/$', views.social_auth_reset_password, name="social_auth_reset_password"),
url(r'^accounts/password/change/$',
views.social_aware_password_change,
{'post_change_redirect': reverse_lazy('auth_password_change_done')},
name='libraryauth_password_change'),
url(r'^password/reset/$',
password_reset,
{'post_reset_redirect': reverse_lazy('auth_password_reset_done'),
'password_reset_form': forms.SocialAwarePasswordResetForm},
name='libraryauth_password_reset'),
url(r'^socialauth/', include('social.apps.django_app.urls', namespace='social')),
url('accounts/', include('email_change.urls')),
url(r'^accounts/', include('registration.backends.model_activation.urls')),

View File

@ -3,14 +3,16 @@ import random
from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render
from django.contrib.auth.views import login, password_reset
from django.contrib.auth.forms import SetPasswordForm
from django.contrib.auth.views import login, password_reset, password_change
from django.contrib.auth import login as login_to_user
from django.contrib.auth import load_backend
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.views.generic.edit import FormView, CreateView, UpdateView, SingleObjectMixin
from registration.backends.model_activation.views import RegistrationView
from . import backends
from .models import Library
from .forms import AuthForm, LibraryForm, NewLibraryForm, RegistrationFormNoDisposableEmail, UserData
@ -54,6 +56,10 @@ def superlogin(request, extra_context={}, **kwargs):
request.session["add_wishlist"]=request.GET["add"]
return login(request, extra_context=extra_context, authentication_form=AuthForm, **kwargs)
def social_aware_password_change(request, **kwargs):
if request.user.has_usable_password():
return password_change(request, **kwargs)
return password_change(request, password_change_form=SetPasswordForm, **kwargs)
class Authenticator:
request=None
@ -266,11 +272,5 @@ def edit_user(request, redirect_to=None):
return HttpResponseRedirect(redirect_to if redirect_to else reverse('home')) # Redirect after POST
return render(request,'registration/user_change_form.html', {'form': form})
@login_required
def social_auth_reset_password(request):
if not request.user.has_usable_password():
request.user.set_password('%010x' % random.randrange(16**10))
request.user.save()
return password_reset(request)