Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Removed username from activation URI.
Browse files Browse the repository at this point in the history
IMPORTANT: When overriding templates, remove the username from the following
templates:

  - userena/templates/userena/emails/activation_email_message.txt
  - userena/templates/userena/emails/confirmation_email_message_new.txt
  • Loading branch information
ghinch authored and wunki committed Jul 5, 2012
1 parent 2d9861a commit 72f1fe2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 47 deletions.
7 changes: 7 additions & 0 deletions UPDATES
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
This file contains all the backwards-incompatible changes.

Version 1.1.2

- Activation view no longer contains username. If you override
`userena/templates/userena/emails/activation_email_message.txt` and
`userena/templates/userena/emails/confirmation_email_message_new.txt` be sure
to remove username from the URL.

Version 1.1

- Userena now requires Django >= 1.3 because of the use of class based views.
Expand Down
2 changes: 1 addition & 1 deletion userena/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Django accounts management made easy.
"""
VERSION = (1, 1, 1)
VERSION = (1, 1, 2)

__version__ = '.'.join((str(each) for each in VERSION[:4]))

Expand Down
17 changes: 4 additions & 13 deletions userena/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,13 @@ def create_userena_profile(self, user):
return self.create(user=user,
activation_key=activation_key)

def activate_user(self, username, activation_key):
def activate_user(self, activation_key):
"""
Activate an :class:`User` by supplying a valid ``activation_key``.
If the key is valid and an user is found, activates the user and
return it. Also sends the ``activation_complete`` signal.
:param username:
String containing the username that wants to be activated.
:param activation_key:
String containing the secret SHA1 for a valid activation.
Expand All @@ -119,8 +116,7 @@ def activate_user(self, username, activation_key):
"""
if SHA1_RE.search(activation_key):
try:
userena = self.get(user__username=username,
activation_key=activation_key)
userena = self.get(activation_key=activation_key)
except self.model.DoesNotExist:
return False
if not userena.activation_key_expired():
Expand All @@ -137,7 +133,7 @@ def activate_user(self, username, activation_key):
return user
return False

def confirm_email(self, username, confirmation_key):
def confirm_email(self, confirmation_key):
"""
Confirm an email address by checking a ``confirmation_key``.
Expand All @@ -146,10 +142,6 @@ def confirm_email(self, username, confirmation_key):
success or ``False`` when the confirmation key is
invalid. Also sends the ``confirmation_complete`` signal.
:param username:
String containing the username of the user that wants their email
verified.
:param confirmation_key:
String containing the secret SHA1 that is used for verification.
Expand All @@ -159,8 +151,7 @@ def confirm_email(self, username, confirmation_key):
"""
if SHA1_RE.search(confirmation_key):
try:
userena = self.get(user__username=username,
email_confirmation_key=confirmation_key,
userena = self.get(email_confirmation_key=confirmation_key,
email_unconfirmed__isnull=False)
except self.model.DoesNotExist:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% trans "To activate your account you should click on the link below:" %}

{{ protocol }}://{{ site.domain }}{% url userena_activate user.username activation_key %}
{{ protocol }}://{{ site.domain }}{% url userena_activate activation_key %}

{% trans "Thanks for using our site!" %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{% trans "Please confirm this email address by clicking on the link below:" %}

{{ protocol }}://{{ site.domain }}{% url userena_email_confirm user.username confirmation_key %}
{{ protocol }}://{{ site.domain }}{% url userena_email_confirm confirmation_key %}


{% trans "Thanks for using our site!" %}
Expand Down
16 changes: 7 additions & 9 deletions userena/tests/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_activation_valid(self):
"""
user = UserenaSignup.objects.create_user(**self.user_info)
active_user = UserenaSignup.objects.activate_user(user.username,
user.userena_signup.activation_key)
active_user = UserenaSignup.objects.activate_user(user.userena_signup.activation_key)

# The returned user should be the same as the one just created.
self.failUnlessEqual(user, active_user)
Expand All @@ -81,11 +80,11 @@ def test_activation_invalid(self):
"""
# Wrong key
self.failIf(UserenaSignup.objects.activate_user('john', 'wrong_key'))
self.failIf(UserenaSignup.objects.activate_user('wrong_key'))

# At least the right length
invalid_key = 10 * 'a1b2'
self.failIf(UserenaSignup.objects.activate_user('john', invalid_key))
self.failIf(UserenaSignup.objects.activate_user(invalid_key))

def test_activation_expired(self):
"""
Expand All @@ -100,7 +99,7 @@ def test_activation_expired(self):
user.save()

# Try to activate the user
UserenaSignup.objects.activate_user(user.username, user.userena_signup.activation_key)
UserenaSignup.objects.activate_user(user.userena_signup.activation_key)

active_user = User.objects.get(username='alice')

Expand All @@ -121,8 +120,7 @@ def test_confirmation_valid(self):
user.userena_signup.change_email(new_email)

# Confirm email
confirmed_user = UserenaSignup.objects.confirm_email(user.username,
user.userena_signup.email_confirmation_key)
confirmed_user = UserenaSignup.objects.confirm_email(user.userena_signup.email_confirmation_key)
self.failUnlessEqual(user, confirmed_user)

# Check the new email is set.
Expand All @@ -143,10 +141,10 @@ def test_confirmation_invalid(self):
user.userena_signup.change_email(new_email)

# Verify email with wrong SHA1
self.failIf(UserenaSignup.objects.confirm_email('john', 'sha1'))
self.failIf(UserenaSignup.objects.confirm_email('sha1'))

# Correct SHA1, but non-existend in db.
self.failIf(UserenaSignup.objects.confirm_email('john', 10 * 'a1b2'))
self.failIf(UserenaSignup.objects.confirm_email(10 * 'a1b2'))

def test_delete_expired_users(self):
"""
Expand Down
3 changes: 1 addition & 2 deletions userena/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def test_activation_used_account(self):
"""
user = UserenaSignup.objects.create_user(**self.user_info)
activated_user = UserenaSignup.objects.activate_user(user.username,
user.userena_signup.activation_key)
activated_user = UserenaSignup.objects.activate_user(user.userena_signup.activation_key)
self.failUnless(activated_user.userena_signup.activation_key_expired())

def test_activation_unexpired_account(self):
Expand Down
12 changes: 4 additions & 8 deletions userena/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_valid_activation(self):
'tos': 'on'})
user = User.objects.get(email='[email protected]')
response = self.client.get(reverse('userena_activate',
kwargs={'username': user.username,
'activation_key': user.userena_signup.activation_key}))
kwargs={'activation_key': user.userena_signup.activation_key}))
self.assertRedirects(response,
reverse('userena_profile_detail', kwargs={'username': user.username}))

Expand All @@ -37,8 +36,7 @@ def test_invalid_activation(self):
"""
response = self.client.get(reverse('userena_activate',
kwargs={'username': 'john',
'activation_key': 'fake'}))
kwargs={'activation_key': 'fake'}))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response,
'userena/activate_fail.html')
Expand All @@ -50,8 +48,7 @@ def test_valid_confirmation(self):
user.userena_signup.change_email('[email protected]')

response = self.client.get(reverse('userena_email_confirm',
kwargs={'username': user.username,
'confirmation_key': user.userena_signup.email_confirmation_key}))
kwargs={'confirmation_key': user.userena_signup.email_confirmation_key}))

self.assertRedirects(response,
reverse('userena_email_confirm_complete', kwargs={'username': user.username}))
Expand All @@ -62,8 +59,7 @@ def test_invalid_confirmation(self):
"""
response = self.client.get(reverse('userena_email_confirm',
kwargs={'username': 'john',
'confirmation_key': 'WRONG'}))
kwargs={'confirmation_key': 'WRONG'}))
self.assertTemplateUsed(response,
'userena/email_confirm_fail.html')

Expand Down
4 changes: 2 additions & 2 deletions userena/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
name='userena_signup_complete'),

# Activate
url(r'^(?P<username>[\.\w]+)/activate/(?P<activation_key>\w+)/$',

This comment has been minimized.

Copy link
@wunki

wunki Aug 3, 2012

Member

You should really take a deep breath.

url(r'^activate/(?P<activation_key>\w+)/$',
userena_views.activate,
name='userena_activate'),

Expand All @@ -61,7 +61,7 @@
userena_views.direct_to_user_template,
{'template_name': 'userena/email_confirm_complete.html'},
name='userena_email_confirm_complete'),
url(r'^(?P<username>[\.\w]+)/confirm-email/(?P<confirmation_key>\w+)/$',
url(r'^confirm-email/(?P<confirmation_key>\w+)/$',
userena_views.email_confirm,
name='userena_email_confirm'),

Expand Down
18 changes: 8 additions & 10 deletions userena/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def signup(request, signup_form=SignupForm,
extra_context=extra_context)(request)

@secure_required
def activate(request, username, activation_key,
def activate(request, activation_key,
template_name='userena/activate_fail.html',
success_url=None, extra_context=None):
"""
Expand All @@ -151,9 +151,6 @@ def activate(request, username, activation_key,
``succes_url``. If the SHA1 is not found, the user will be shown the
``template_name`` template displaying a fail message.
:param username:
String of the username that wants to be activated.
:param activation_key:
String of a SHA1 string of 40 characters long. A SHA1 is always 160bit
long, with 4 bits per character this makes it --160/4-- 40 characters
Expand All @@ -175,7 +172,7 @@ def activate(request, username, activation_key,
context. Default to an empty dictionary.
"""
user = UserenaSignup.objects.activate_user(username, activation_key)
user = UserenaSignup.objects.activate_user(activation_key)
if user:
# Sign the user in.
auth_user = authenticate(identification=user.email,
Expand All @@ -196,7 +193,7 @@ def activate(request, username, activation_key,
extra_context=extra_context)(request)

@secure_required
def email_confirm(request, username, confirmation_key,
def email_confirm(request, confirmation_key,
template_name='userena/email_confirm_fail.html',
success_url=None, extra_context=None):
"""
Expand All @@ -208,9 +205,6 @@ def email_confirm(request, username, confirmation_key,
returned the user will be represented with a fail message from
``template_name``.
:param username:
String of the username whose email address needs to be confirmed.
:param confirmation_key:
String with a SHA1 representing the confirmation key used to verify a
new email address.
Expand All @@ -230,8 +224,12 @@ def email_confirm(request, username, confirmation_key,
``template_name``.
"""
user = UserenaSignup.objects.confirm_email(username, confirmation_key)
user = UserenaSignup.objects.confirm_email(confirmation_key)
if user:
if userena_settings.USERENA_USE_MESSAGES:
messages.success(request, _('Your email address has been changed.'),
fail_silently=True)

if success_url: redirect_to = success_url
else: redirect_to = reverse('userena_email_confirm_complete',
kwargs={'username': user.username})
Expand Down

0 comments on commit 72f1fe2

Please sign in to comment.