2012-07-07 21:29:20 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-06-27 00:51:34 +08:00
|
|
|
from django import forms
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.auth import authenticate, get_user_model
|
|
|
|
from django.contrib.auth.hashers import (
|
|
|
|
UNUSABLE_PASSWORD_PREFIX, identify_hasher,
|
|
|
|
)
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2014-05-09 14:48:41 +08:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.forms.utils import flatatt
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.template import loader
|
2013-06-22 04:59:33 +08:00
|
|
|
from django.utils.encoding import force_bytes
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import format_html, format_html_join
|
2013-06-22 04:59:33 +08:00
|
|
|
from django.utils.http import urlsafe_base64_encode
|
2011-12-10 06:10:03 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
from django.utils.text import capfirst
|
2011-12-25 03:38:37 +08:00
|
|
|
from django.utils.translation import ugettext, ugettext_lazy as _
|
2011-06-27 00:51:34 +08:00
|
|
|
|
2012-07-15 05:33:13 +08:00
|
|
|
|
2011-12-10 06:10:03 +08:00
|
|
|
class ReadOnlyPasswordHashWidget(forms.Widget):
|
|
|
|
def render(self, name, value, attrs):
|
2011-12-23 11:46:06 +08:00
|
|
|
encoded = value
|
2011-12-10 06:10:03 +08:00
|
|
|
final_attrs = self.build_attrs(attrs)
|
2011-12-23 11:46:06 +08:00
|
|
|
|
2013-06-19 02:02:00 +08:00
|
|
|
if not encoded or encoded.startswith(UNUSABLE_PASSWORD_PREFIX):
|
2012-09-12 17:21:58 +08:00
|
|
|
summary = mark_safe("<strong>%s</strong>" % ugettext("No password set."))
|
2012-03-22 16:10:19 +08:00
|
|
|
else:
|
2012-09-12 17:21:58 +08:00
|
|
|
try:
|
|
|
|
hasher = identify_hasher(encoded)
|
|
|
|
except ValueError:
|
|
|
|
summary = mark_safe("<strong>%s</strong>" % ugettext(
|
|
|
|
"Invalid password format or unknown hashing algorithm."))
|
|
|
|
else:
|
|
|
|
summary = format_html_join('',
|
2014-11-27 08:41:27 +08:00
|
|
|
"<strong>{}</strong>: {} ",
|
2012-09-12 17:21:58 +08:00
|
|
|
((ugettext(key), value)
|
|
|
|
for key, value in hasher.safe_summary(encoded).items())
|
|
|
|
)
|
2011-12-23 11:46:06 +08:00
|
|
|
|
2014-11-27 08:41:27 +08:00
|
|
|
return format_html("<div{}>{}</div>", flatatt(final_attrs), summary)
|
2011-12-10 06:10:03 +08:00
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2011-12-10 06:10:03 +08:00
|
|
|
class ReadOnlyPasswordHashField(forms.Field):
|
|
|
|
widget = ReadOnlyPasswordHashWidget
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault("required", False)
|
|
|
|
super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-12-01 19:17:00 +08:00
|
|
|
def bound_data(self, data, initial):
|
|
|
|
# Always return initial because the widget doesn't
|
|
|
|
# render an input field.
|
|
|
|
return initial
|
|
|
|
|
2014-08-07 10:56:23 +08:00
|
|
|
def has_changed(self, initial, data):
|
2013-01-26 04:27:49 +08:00
|
|
|
return False
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
|
|
"""
|
2012-01-02 22:51:25 +08:00
|
|
|
A form that creates a user, with no privileges, from the given username and
|
|
|
|
password.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2011-12-16 00:12:46 +08:00
|
|
|
error_messages = {
|
|
|
|
'password_mismatch': _("The two password fields didn't match."),
|
|
|
|
}
|
2012-01-02 22:51:25 +08:00
|
|
|
password1 = forms.CharField(label=_("Password"),
|
2013-09-21 21:34:45 +08:00
|
|
|
widget=forms.PasswordInput)
|
2012-01-02 22:51:25 +08:00
|
|
|
password2 = forms.CharField(label=_("Password confirmation"),
|
|
|
|
widget=forms.PasswordInput,
|
2012-09-05 04:47:38 +08:00
|
|
|
help_text=_("Enter the same password as above, for verification."))
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ("username",)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_password2(self):
|
2012-08-04 20:55:13 +08:00
|
|
|
password1 = self.cleaned_data.get("password1")
|
|
|
|
password2 = self.cleaned_data.get("password2")
|
|
|
|
if password1 and password2 and password1 != password2:
|
2012-01-02 22:51:25 +08:00
|
|
|
raise forms.ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
2008-07-19 07:54:34 +08:00
|
|
|
return password2
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
def save(self, commit=True):
|
|
|
|
user = super(UserCreationForm, self).save(commit=False)
|
|
|
|
user.set_password(self.cleaned_data["password1"])
|
|
|
|
if commit:
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-08-26 01:10:20 +08:00
|
|
|
class UserChangeForm(forms.ModelForm):
|
2012-01-02 22:51:25 +08:00
|
|
|
password = ReadOnlyPasswordHashField(label=_("Password"),
|
|
|
|
help_text=_("Raw passwords are not stored, so there is no way to see "
|
|
|
|
"this user's password, but you can change the password "
|
2015-03-28 03:07:35 +08:00
|
|
|
"using <a href=\"../password/\">this form</a>."))
|
2011-12-10 06:10:03 +08:00
|
|
|
|
2008-08-26 01:10:20 +08:00
|
|
|
class Meta:
|
|
|
|
model = User
|
2013-02-22 05:56:55 +08:00
|
|
|
fields = '__all__'
|
2008-08-26 01:10:20 +08:00
|
|
|
|
2010-09-04 02:56:12 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(UserChangeForm, self).__init__(*args, **kwargs)
|
2015-05-14 02:51:18 +08:00
|
|
|
f = self.fields.get('user_permissions')
|
2010-09-10 07:31:54 +08:00
|
|
|
if f is not None:
|
|
|
|
f.queryset = f.queryset.select_related('content_type')
|
2010-09-04 02:56:12 +08:00
|
|
|
|
2012-10-20 11:41:54 +08:00
|
|
|
def clean_password(self):
|
|
|
|
# Regardless of what the user provides, return the initial value.
|
|
|
|
# This is done here, rather than on the field, because the
|
|
|
|
# field does not have access to the initial value
|
|
|
|
return self.initial["password"]
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AuthenticationForm(forms.Form):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Base class for authenticating users. Extend this to get a form that accepts
|
|
|
|
username/password logins.
|
|
|
|
"""
|
2012-10-16 16:03:29 +08:00
|
|
|
username = forms.CharField(max_length=254)
|
2013-09-21 21:34:45 +08:00
|
|
|
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
error_messages = {
|
2012-12-15 22:43:44 +08:00
|
|
|
'invalid_login': _("Please enter a correct %(username)s and password. "
|
|
|
|
"Note that both fields may be case-sensitive."),
|
2011-12-16 00:12:46 +08:00
|
|
|
'inactive': _("This account is inactive."),
|
|
|
|
}
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, request=None, *args, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-02-24 07:25:05 +08:00
|
|
|
The 'request' parameter is set for custom auth use by subclasses.
|
|
|
|
The form data comes in via the standard 'data' kwarg.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
self.request = request
|
|
|
|
self.user_cache = None
|
2008-07-19 07:54:34 +08:00
|
|
|
super(AuthenticationForm, self).__init__(*args, **kwargs)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
# Set the label for the "username" field.
|
|
|
|
UserModel = get_user_model()
|
2012-12-15 22:43:44 +08:00
|
|
|
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
|
2013-05-06 18:45:56 +08:00
|
|
|
if self.fields['username'].label is None:
|
2013-01-10 16:05:01 +08:00
|
|
|
self.fields['username'].label = capfirst(self.username_field.verbose_name)
|
Fixed #3011 -- Added swappable auth.User models.
Thanks to the many people that contributed to the development and review of
this patch, including (but not limited to) Jacob Kaplan-Moss, Anssi
Kääriäinen, Ramiro Morales, Preston Holmes, Josh Ourisman, Thomas Sutton,
and Roger Barnes, as well as the many, many people who have contributed to
the design discussion around this ticket over many years.
Squashed commit of the following:
commit d84749a0f034a0a6906d20df047086b1219040d0
Merge: 531e771 7c11b1a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 18:37:04 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 531e7715da545f930c49919a19e954d41c59b446
Merge: 29d1abb 1f84b04
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Wed Sep 26 07:09:23 2012 +0800
Merged recent trunk changes.
commit 29d1abbe351fd5da855fe5ce09e24227d90ddc91
Merge: 8a527dd 54c81a1
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:49:46 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8a527dda13c9bec955b1f7e8db5822d1d9b32a01
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 24 07:48:05 2012 +0800
Ensure sequences are reset correctly in the presence of swapped models.
commit e2b6e22f298eb986d74d28b8d9906f37f5ff8eb8
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 17:53:05 2012 +0800
Modifications to the handling and docs for auth forms.
commit 98aba856b534620aea9091f824b442b47d2fdb3c
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 15:28:57 2012 +0800
Improved error handling and docs for get_user_model()
commit 0229209c844f06dfeb33b0b8eeec000c127695b6
Merge: 6494bf9 8599f64
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 23 14:50:11 2012 +0800
Merged recent Django trunk changes.
commit 6494bf91f2ddaaabec3ec017f2e3131937c35517
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 21:38:44 2012 +0800
Improved validation of swappable model settings.
commit 5a04cde342cc860384eb844cfda5af55204564ad
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Sep 17 07:15:14 2012 +0800
Removed some unused imports.
commit ffd535e4136dc54f084b6ac467e81444696e1c8a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:31:28 2012 +0800
Corrected attribute access on for get_by_natural_key
commit 913e1ac84c3d9c7c58a9b3bdbbb15ebccd8a8c0a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 20:12:34 2012 +0800
Added test for proxy model safeguards on swappable models.
commit 280bf19e94d0d534d0e51bae485c1842558f4ff4
Merge: dbb3900 935a863
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:16:49 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit dbb3900775a99df8b6cb1d7063cf364eab55621a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 18:09:27 2012 +0800
Fixes for Python 3 compatibility.
commit dfd72131d8664615e245aa0f95b82604ba6b3821
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:54:30 2012 +0800
Added protection against proxying swapped models.
commit abcb027190e53613e7f1734e77ee185b2587de31
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 15:11:10 2012 +0800
Cleanup and documentation of AbstractUser base class.
commit a9491a87763e307f0eb0dc246f54ac865a6ffb34
Merge: fd8bb4e 08bcb4a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:46:49 2012 +0800
Merge commit '08bcb4aec1ed154cefc631b8510ee13e9af0c19d' into t3011
commit fd8bb4e3e498a92d7a8b340f0684d5f088aa4c92
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 14:20:14 2012 +0800
Documentation improvements coming from community review.
commit b550a6d06d016ab6a0198c4cb2dffe9cceabe8a5
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:52:47 2012 +0800
Refactored skipIfCustomUser into the contrib.auth tests.
commit 52a02f11107c3f0d711742b8ca65b75175b79d6a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:46:10 2012 +0800
Refactored common 'get' pattern into manager method.
commit b441a6bbc7d6065175715cb09316b9f13268171b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 16 13:41:33 2012 +0800
Added note about backwards incompatible change to admin login messages.
commit 08bcb4aec1ed154cefc631b8510ee13e9af0c19d
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:33 2012 +0300
Splitted User to AbstractUser and User
commit d9f5e5addbad5e1a01f67e7358e4f5091c3cad81
Author: Anssi Kääriäinen <akaariai@gmail.com>
Date: Sat Sep 15 18:30:02 2012 +0300
Reworked REQUIRED_FIELDS + create_user() interaction
commit 579f152e4a6e06671e1ac1e59e2b43cf4d764bf4
Merge: 9184972 93e6733
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:37 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 918497218c58227f5032873ff97261627b2ceab2
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:18:19 2012 +0800
Deprecate AUTH_PROFILE_MODULE and get_profile().
commit 334cdfc1bb6a6794791497cdefda843bca2ea57a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 20:00:12 2012 +0800
Added release notes for new swappable User feature.
commit 5d7bb22e8d913b51aba1c3360e7af8b01b6c0ab6
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 19:59:49 2012 +0800
Ensure swapped models can't be queried.
commit 57ac6e3d32605a67581e875b37ec5b2284711a32
Merge: f2ec915 abfba3b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 15 14:31:54 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit f2ec915b20f81c8afeaa3df25f80689712f720f8
Merge: 1952656 5e99a3d
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:29:51 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 19526563b54fa300785c49cfb625c0c6158ced67
Merge: 2c5e833 c4aa26a
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 08:22:26 2012 +0800
Merge recent changes from master.
commit 2c5e833a30bef4305d55eacc0703533152f5c427
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 07:53:46 2012 +0800
Corrected admin_views tests following removal of the email fallback on admin logins.
commit 20d1892491839d6ef21f37db4ca136935c2076bf
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sun Sep 9 01:00:37 2012 +0800
Added conditional skips for all tests dependent on the default User model
commit 40ea8b888284775481fc1eaadeff267dbd7e3dfa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:47:02 2012 +0800
Added documentation for REQUIRED_FIELDS in custom auth.
commit e6aaf659708cf6491f5485d3edfa616cb9214cc0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Sat Sep 8 23:20:02 2012 +0800
Added first draft of custom User docs.
Thanks to Greg Turner for the initial text.
commit 75118bd242eec87649da2859e8c50a199a8a1dca
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 11:17:26 2012 +0800
Admin app should not allow username discovery
The admin app login form should not allow users to discover the username
associated with an email address.
commit d088b3af58dad7449fc58493193a327725c57c22
Author: Thomas Sutton <me@thomas-sutton.id.au>
Date: Mon Aug 20 10:32:13 2012 +0800
Admin app login form should use swapped user model
commit 7e82e83d67ee0871a72e1a3a723afdd214fcefc3
Merge: e29c010 39aa890
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Fri Sep 7 23:45:03 2012 +0800
Merged master changes.
commit e29c010beb96ca07697c4e3e0c0d5d3ffdc4c0a3
Merge: 8e3fd70 30bdf22
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:12:57 2012 +0800
Merge remote-tracking branch 'django/master' into t3011
commit 8e3fd703d02c31a4c3ac9f51f5011d03c0bd47f6
Merge: 507bb50 26e0ba0
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Aug 20 13:09:09 2012 +0800
Merged recent changes from trunk.
commit 507bb50a9291bfcdcfa1198f9fea21d4e3b1e762
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:41:37 2012 +0800
Modified auth app so that login with alternate auth app is possible.
commit dabe3628362ab7a4a6c9686dd874803baa997eaa
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 20:10:51 2012 +0800
Modified auth management commands to handle custom user definitions.
commit 7cc0baf89d490c92ef3f1dc909b8090191a1294b
Author: Russell Keith-Magee <russell@keith-magee.com>
Date: Mon Jun 4 14:17:28 2012 +0800
Added model Meta option for swappable models, and made auth.User a swappable model
2012-09-26 18:48:09 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
password = self.cleaned_data.get('password')
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
if username and password:
|
2012-01-02 22:51:25 +08:00
|
|
|
self.user_cache = authenticate(username=username,
|
|
|
|
password=password)
|
2008-07-19 07:54:34 +08:00
|
|
|
if self.user_cache is None:
|
2012-01-02 22:51:25 +08:00
|
|
|
raise forms.ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
self.error_messages['invalid_login'],
|
|
|
|
code='invalid_login',
|
|
|
|
params={'username': self.username_field.verbose_name},
|
|
|
|
)
|
2013-07-30 20:24:13 +08:00
|
|
|
else:
|
|
|
|
self.confirm_login_allowed(self.user_cache)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
return self.cleaned_data
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2013-07-30 20:24:13 +08:00
|
|
|
def confirm_login_allowed(self, user):
|
|
|
|
"""
|
|
|
|
Controls whether the given User may log in. This is a policy setting,
|
|
|
|
independent of end-user authentication. This default behavior is to
|
|
|
|
allow login by active users, and reject login by inactive users.
|
|
|
|
|
|
|
|
If the given user cannot log in, this method should raise a
|
|
|
|
``forms.ValidationError``.
|
|
|
|
|
|
|
|
If the given user may log in, this method should return None.
|
|
|
|
"""
|
|
|
|
if not user.is_active:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['inactive'],
|
|
|
|
code='inactive',
|
|
|
|
)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_user_id(self):
|
|
|
|
if self.user_cache:
|
|
|
|
return self.user_cache.id
|
|
|
|
return None
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_user(self):
|
|
|
|
return self.user_cache
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class PasswordResetForm(forms.Form):
|
2012-10-16 16:03:29 +08:00
|
|
|
email = forms.EmailField(label=_("Email"), max_length=254)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2014-05-09 14:48:41 +08:00
|
|
|
def send_mail(self, subject_template_name, email_template_name,
|
|
|
|
context, from_email, to_email, html_email_template_name=None):
|
|
|
|
"""
|
|
|
|
Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
|
|
|
|
"""
|
|
|
|
subject = loader.render_to_string(subject_template_name, context)
|
|
|
|
# Email subject *must not* contain newlines
|
|
|
|
subject = ''.join(subject.splitlines())
|
|
|
|
body = loader.render_to_string(email_template_name, context)
|
|
|
|
|
|
|
|
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
|
|
|
|
if html_email_template_name is not None:
|
|
|
|
html_email = loader.render_to_string(html_email_template_name, context)
|
|
|
|
email_message.attach_alternative(html_email, 'text/html')
|
|
|
|
|
|
|
|
email_message.send()
|
|
|
|
|
2014-09-04 01:49:56 +08:00
|
|
|
def get_users(self, email):
|
|
|
|
"""Given an email, return matching user(s) who should receive a reset.
|
|
|
|
|
|
|
|
This allows subclasses to more easily customize the default policies
|
|
|
|
that prevent inactive users and users with unusable passwords from
|
|
|
|
resetting their password.
|
|
|
|
|
|
|
|
"""
|
|
|
|
active_users = get_user_model()._default_manager.filter(
|
|
|
|
email__iexact=email, is_active=True)
|
|
|
|
return (u for u in active_users if u.has_usable_password())
|
|
|
|
|
2011-06-19 19:24:39 +08:00
|
|
|
def save(self, domain_override=None,
|
|
|
|
subject_template_name='registration/password_reset_subject.txt',
|
|
|
|
email_template_name='registration/password_reset_email.html',
|
|
|
|
use_https=False, token_generator=default_token_generator,
|
2013-07-31 10:29:34 +08:00
|
|
|
from_email=None, request=None, html_email_template_name=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2012-01-02 22:51:25 +08:00
|
|
|
Generates a one-use only link for resetting password and sends to the
|
|
|
|
user.
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2013-02-23 20:39:21 +08:00
|
|
|
email = self.cleaned_data["email"]
|
2014-09-04 01:49:56 +08:00
|
|
|
for user in self.get_users(email):
|
2007-06-20 04:04:54 +08:00
|
|
|
if not domain_override:
|
Fixed #14386, #8960, #10235, #10909, #10608, #13845, #14377 - standardize Site/RequestSite usage in various places.
Many thanks to gabrielhurley for putting most of this together. Also to
bmihelac, arthurk, qingfeng, hvendelbo, petr.pulc@s-cape.cz, Hraban for
reports and some initial patches.
The patch also contains some whitespace/PEP8 fixes.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-04 22:20:47 +08:00
|
|
|
current_site = get_current_site(request)
|
2007-06-20 04:04:54 +08:00
|
|
|
site_name = current_site.name
|
|
|
|
domain = current_site.domain
|
|
|
|
else:
|
|
|
|
site_name = domain = domain_override
|
2014-05-09 14:48:41 +08:00
|
|
|
context = {
|
2007-06-20 04:04:54 +08:00
|
|
|
'email': user.email,
|
|
|
|
'domain': domain,
|
|
|
|
'site_name': site_name,
|
2013-06-22 04:59:33 +08:00
|
|
|
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
|
2007-06-20 04:04:54 +08:00
|
|
|
'user': user,
|
2008-08-01 04:47:53 +08:00
|
|
|
'token': token_generator.make_token(user),
|
2013-05-27 10:47:50 +08:00
|
|
|
'protocol': 'https' if use_https else 'http',
|
2008-07-19 07:54:34 +08:00
|
|
|
}
|
2013-07-31 10:29:34 +08:00
|
|
|
|
2014-05-09 14:48:41 +08:00
|
|
|
self.send_mail(subject_template_name, email_template_name,
|
|
|
|
context, from_email, user.email,
|
|
|
|
html_email_template_name=html_email_template_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
class SetPasswordForm(forms.Form):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2014-07-22 01:55:37 +08:00
|
|
|
A form that lets a user change set their password without entering the old
|
|
|
|
password
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2011-12-16 00:12:46 +08:00
|
|
|
error_messages = {
|
|
|
|
'password_mismatch': _("The two password fields didn't match."),
|
|
|
|
}
|
2013-09-21 21:34:45 +08:00
|
|
|
new_password1 = forms.CharField(label=_("New password"),
|
|
|
|
widget=forms.PasswordInput)
|
|
|
|
new_password2 = forms.CharField(label=_("New password confirmation"),
|
|
|
|
widget=forms.PasswordInput)
|
2008-08-01 04:47:53 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.user = user
|
2008-08-01 04:47:53 +08:00
|
|
|
super(SetPasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_new_password2(self):
|
|
|
|
password1 = self.cleaned_data.get('new_password1')
|
|
|
|
password2 = self.cleaned_data.get('new_password2')
|
|
|
|
if password1 and password2:
|
|
|
|
if password1 != password2:
|
2012-01-02 22:51:25 +08:00
|
|
|
raise forms.ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
2008-07-19 07:54:34 +08:00
|
|
|
return password2
|
2008-08-01 04:47:53 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def save(self, commit=True):
|
|
|
|
self.user.set_password(self.cleaned_data['new_password1'])
|
|
|
|
if commit:
|
|
|
|
self.user.save()
|
|
|
|
return self.user
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
class PasswordChangeForm(SetPasswordForm):
|
|
|
|
"""
|
2014-07-22 01:55:37 +08:00
|
|
|
A form that lets a user change their password by entering their old
|
|
|
|
password.
|
2008-08-01 04:47:53 +08:00
|
|
|
"""
|
2011-12-16 00:12:46 +08:00
|
|
|
error_messages = dict(SetPasswordForm.error_messages, **{
|
2012-01-02 22:51:25 +08:00
|
|
|
'password_incorrect': _("Your old password was entered incorrectly. "
|
|
|
|
"Please enter it again."),
|
2011-12-16 00:12:46 +08:00
|
|
|
})
|
2013-09-21 21:34:45 +08:00
|
|
|
old_password = forms.CharField(label=_("Old password"),
|
|
|
|
widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2015-03-16 08:33:59 +08:00
|
|
|
field_order = ['old_password', 'new_password1', 'new_password2']
|
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
def clean_old_password(self):
|
|
|
|
"""
|
|
|
|
Validates that the old_password field is correct.
|
|
|
|
"""
|
|
|
|
old_password = self.cleaned_data["old_password"]
|
|
|
|
if not self.user.check_password(old_password):
|
2012-01-02 22:51:25 +08:00
|
|
|
raise forms.ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
self.error_messages['password_incorrect'],
|
|
|
|
code='password_incorrect',
|
|
|
|
)
|
2008-08-01 04:47:53 +08:00
|
|
|
return old_password
|
2012-07-15 05:33:13 +08:00
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AdminPasswordChangeForm(forms.Form):
|
|
|
|
"""
|
|
|
|
A form used to change the password of a user in the admin interface.
|
|
|
|
"""
|
2011-12-16 00:12:46 +08:00
|
|
|
error_messages = {
|
|
|
|
'password_mismatch': _("The two password fields didn't match."),
|
|
|
|
}
|
2014-03-24 20:49:05 +08:00
|
|
|
required_css_class = 'required'
|
2014-12-26 21:06:41 +08:00
|
|
|
password1 = forms.CharField(
|
|
|
|
label=_("Password"),
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
)
|
|
|
|
password2 = forms.CharField(
|
|
|
|
label=_("Password (again)"),
|
|
|
|
widget=forms.PasswordInput,
|
|
|
|
help_text=_("Enter the same password as above, for verification."),
|
|
|
|
)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2006-12-30 15:16:25 +08:00
|
|
|
self.user = user
|
2008-07-19 07:54:34 +08:00
|
|
|
super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_password2(self):
|
|
|
|
password1 = self.cleaned_data.get('password1')
|
|
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if password1 and password2:
|
|
|
|
if password1 != password2:
|
2012-01-02 22:51:25 +08:00
|
|
|
raise forms.ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
code='password_mismatch',
|
|
|
|
)
|
2008-07-19 07:54:34 +08:00
|
|
|
return password2
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Saves the new password.
|
|
|
|
"""
|
|
|
|
self.user.set_password(self.cleaned_data["password1"])
|
|
|
|
if commit:
|
|
|
|
self.user.save()
|
|
|
|
return self.user
|
2012-10-24 23:11:41 +08:00
|
|
|
|
|
|
|
def _get_changed_data(self):
|
|
|
|
data = super(AdminPasswordChangeForm, self).changed_data
|
|
|
|
for name in self.fields.keys():
|
|
|
|
if name not in data:
|
|
|
|
return []
|
|
|
|
return ['password']
|
|
|
|
changed_data = property(_get_changed_data)
|