2016-04-23 03:17:42 +08:00
|
|
|
|
import unicodedata
|
|
|
|
|
|
2011-06-27 00:51:34 +08:00
|
|
|
|
from django import forms
|
2015-03-08 22:07:57 +08:00
|
|
|
|
from django.contrib.auth import (
|
|
|
|
|
authenticate, get_user_model, password_validation,
|
|
|
|
|
)
|
2015-01-28 20:35:27 +08:00
|
|
|
|
from django.contrib.auth.hashers import (
|
|
|
|
|
UNUSABLE_PASSWORD_PREFIX, identify_hasher,
|
|
|
|
|
)
|
2018-07-03 06:10:36 +08:00
|
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 20:35:27 +08:00
|
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
|
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2020-02-12 21:48:49 +08:00
|
|
|
|
from django.core.exceptions import ValidationError
|
2014-05-09 14:48:41 +08:00
|
|
|
|
from django.core.mail import EmailMultiAlternatives
|
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
|
|
|
|
|
from django.utils.http import urlsafe_base64_encode
|
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
|
2017-01-27 03:58:33 +08:00
|
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
2011-06-27 00:51:34 +08:00
|
|
|
|
|
2016-10-01 04:06:02 +08:00
|
|
|
|
UserModel = get_user_model()
|
|
|
|
|
|
2012-07-15 05:33:13 +08:00
|
|
|
|
|
2019-12-17 10:51:57 +08:00
|
|
|
|
def _unicode_ci_compare(s1, s2):
|
|
|
|
|
"""
|
|
|
|
|
Perform case-insensitive comparison of two identifiers, using the
|
|
|
|
|
recommended algorithm from Unicode Technical Report 36, section
|
|
|
|
|
2.11.2(B)(2).
|
|
|
|
|
"""
|
|
|
|
|
return unicodedata.normalize('NFKC', s1).casefold() == unicodedata.normalize('NFKC', s2).casefold()
|
|
|
|
|
|
|
|
|
|
|
2011-12-10 06:10:03 +08:00
|
|
|
|
class ReadOnlyPasswordHashWidget(forms.Widget):
|
2016-12-28 06:00:56 +08:00
|
|
|
|
template_name = 'auth/widgets/read_only_password_hash.html'
|
2018-09-13 21:08:41 +08:00
|
|
|
|
read_only = True
|
2011-12-23 11:46:06 +08:00
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
context = super().get_context(name, value, attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
|
summary = []
|
|
|
|
|
if not value or value.startswith(UNUSABLE_PASSWORD_PREFIX):
|
2017-01-27 03:58:33 +08:00
|
|
|
|
summary.append({'label': gettext("No password set.")})
|
2012-03-22 16:10:19 +08:00
|
|
|
|
else:
|
2012-09-12 17:21:58 +08:00
|
|
|
|
try:
|
2016-12-28 06:00:56 +08:00
|
|
|
|
hasher = identify_hasher(value)
|
2012-09-12 17:21:58 +08:00
|
|
|
|
except ValueError:
|
2017-01-27 03:58:33 +08:00
|
|
|
|
summary.append({'label': gettext("Invalid password format or unknown hashing algorithm.")})
|
2012-09-12 17:21:58 +08:00
|
|
|
|
else:
|
2016-12-28 06:00:56 +08:00
|
|
|
|
for key, value_ in hasher.safe_summary(value).items():
|
2017-01-27 03:58:33 +08:00
|
|
|
|
summary.append({'label': gettext(key), 'value': value_})
|
2016-12-28 06:00:56 +08:00
|
|
|
|
context['summary'] = summary
|
|
|
|
|
return context
|
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)
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__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
|
|
|
|
|
2016-04-23 03:17:42 +08:00
|
|
|
|
class UsernameField(forms.CharField):
|
|
|
|
|
def to_python(self, value):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
return unicodedata.normalize('NFKC', super().to_python(value))
|
2016-04-23 03:17:42 +08:00
|
|
|
|
|
2019-03-25 02:53:31 +08:00
|
|
|
|
def widget_attrs(self, widget):
|
2019-09-02 16:50:56 +08:00
|
|
|
|
return {
|
|
|
|
|
**super().widget_attrs(widget),
|
|
|
|
|
'autocapitalize': 'none',
|
|
|
|
|
'autocomplete': 'username',
|
|
|
|
|
}
|
2019-03-25 02:53:31 +08:00
|
|
|
|
|
2016-04-23 03:17:42 +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 = {
|
2019-06-28 00:39:47 +08:00
|
|
|
|
'password_mismatch': _('The two password fields didn’t match.'),
|
2011-12-16 00:12:46 +08:00
|
|
|
|
}
|
2016-03-29 06:33:29 +08:00
|
|
|
|
password1 = forms.CharField(
|
|
|
|
|
label=_("Password"),
|
2016-03-15 04:21:05 +08:00
|
|
|
|
strip=False,
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
2016-04-15 01:12:30 +08:00
|
|
|
|
help_text=password_validation.password_validators_help_text_html(),
|
2016-03-29 06:33:29 +08:00
|
|
|
|
)
|
|
|
|
|
password2 = forms.CharField(
|
|
|
|
|
label=_("Password confirmation"),
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
2016-03-15 04:21:05 +08:00
|
|
|
|
strip=False,
|
2016-03-29 06:33:29 +08:00
|
|
|
|
help_text=_("Enter the same password as before, for verification."),
|
|
|
|
|
)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
|
class Meta:
|
2018-07-03 06:10:36 +08:00
|
|
|
|
model = User
|
|
|
|
|
fields = ("username",)
|
|
|
|
|
field_classes = {'username': UsernameField}
|
2008-08-06 00:36:20 +08:00
|
|
|
|
|
2015-11-23 18:46:19 +08:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2016-08-25 01:20:12 +08:00
|
|
|
|
if self._meta.model.USERNAME_FIELD in self.fields:
|
2019-09-02 16:50:56 +08:00
|
|
|
|
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True
|
2015-11-23 18:46:19 +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:
|
2020-02-12 21:48:49 +08:00
|
|
|
|
raise 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
|
|
|
|
|
2017-04-26 03:26:58 +08:00
|
|
|
|
def _post_clean(self):
|
|
|
|
|
super()._post_clean()
|
|
|
|
|
# Validate the password after self.instance is updated with form data
|
|
|
|
|
# by super().
|
|
|
|
|
password = self.cleaned_data.get('password2')
|
|
|
|
|
if password:
|
|
|
|
|
try:
|
|
|
|
|
password_validation.validate_password(password, self.instance)
|
2020-02-12 21:48:49 +08:00
|
|
|
|
except ValidationError as error:
|
2017-04-26 03:26:58 +08:00
|
|
|
|
self.add_error('password2', error)
|
|
|
|
|
|
2010-01-12 10:29:45 +08:00
|
|
|
|
def save(self, commit=True):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
user = super().save(commit=False)
|
2010-01-12 10:29:45 +08:00
|
|
|
|
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):
|
2016-03-29 06:33:29 +08:00
|
|
|
|
password = ReadOnlyPasswordHashField(
|
|
|
|
|
label=_("Password"),
|
|
|
|
|
help_text=_(
|
2019-06-28 00:39:47 +08:00
|
|
|
|
'Raw passwords are not stored, so there is no way to see this '
|
|
|
|
|
'user’s password, but you can change the password using '
|
|
|
|
|
'<a href="{}">this form</a>.'
|
2016-03-29 06:33:29 +08:00
|
|
|
|
),
|
|
|
|
|
)
|
2011-12-10 06:10:03 +08:00
|
|
|
|
|
2008-08-26 01:10:20 +08:00
|
|
|
|
class Meta:
|
2018-07-03 06:10:36 +08:00
|
|
|
|
model = User
|
2013-02-22 05:56:55 +08:00
|
|
|
|
fields = '__all__'
|
2018-07-03 06:10:36 +08:00
|
|
|
|
field_classes = {'username': UsernameField}
|
2008-08-26 01:10:20 +08:00
|
|
|
|
|
2010-09-04 02:56:12 +08:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-03-29 17:35:53 +08:00
|
|
|
|
password = self.fields.get('password')
|
|
|
|
|
if password:
|
|
|
|
|
password.help_text = password.help_text.format('../password/')
|
|
|
|
|
user_permissions = self.fields.get('user_permissions')
|
|
|
|
|
if user_permissions:
|
|
|
|
|
user_permissions.queryset = user_permissions.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
|
2018-09-28 07:52:01 +08:00
|
|
|
|
return self.initial.get('password')
|
2012-10-20 11:41:54 +08:00
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2019-09-02 16:50:56 +08:00
|
|
|
|
username = UsernameField(widget=forms.TextInput(attrs={'autofocus': True}))
|
2016-03-29 06:33:29 +08:00
|
|
|
|
password = forms.CharField(
|
|
|
|
|
label=_("Password"),
|
|
|
|
|
strip=False,
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'current-password'}),
|
2016-03-29 06:33:29 +08:00
|
|
|
|
)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
error_messages = {
|
2016-03-29 06:33:29 +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
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
|
2017-08-18 05:08:56 +08:00
|
|
|
|
# Set the max length and label for the "username" field.
|
2012-12-15 22:43:44 +08:00
|
|
|
|
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
|
2019-09-17 22:21:50 +08:00
|
|
|
|
username_max_length = self.username_field.max_length or 254
|
|
|
|
|
self.fields['username'].max_length = username_max_length
|
|
|
|
|
self.fields['username'].widget.attrs['maxlength'] = username_max_length
|
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
|
|
|
|
|
2016-07-26 21:50:29 +08:00
|
|
|
|
if username is not None and password:
|
2016-07-11 22:40:39 +08:00
|
|
|
|
self.user_cache = authenticate(self.request, username=username, password=password)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
if self.user_cache is None:
|
2017-10-12 12:39:22 +08:00
|
|
|
|
raise self.get_invalid_login_error()
|
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
|
2020-02-12 21:48:49 +08:00
|
|
|
|
``ValidationError``.
|
2013-07-30 20:24:13 +08:00
|
|
|
|
|
|
|
|
|
If the given user may log in, this method should return None.
|
|
|
|
|
"""
|
|
|
|
|
if not user.is_active:
|
2020-02-12 21:48:49 +08:00
|
|
|
|
raise ValidationError(
|
2013-07-30 20:24:13 +08:00
|
|
|
|
self.error_messages['inactive'],
|
|
|
|
|
code='inactive',
|
|
|
|
|
)
|
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
def get_user(self):
|
|
|
|
|
return self.user_cache
|
|
|
|
|
|
2017-10-12 12:39:22 +08:00
|
|
|
|
def get_invalid_login_error(self):
|
2020-02-12 21:48:49 +08:00
|
|
|
|
return ValidationError(
|
2017-10-12 12:39:22 +08:00
|
|
|
|
self.error_messages['invalid_login'],
|
|
|
|
|
code='invalid_login',
|
|
|
|
|
params={'username': self.username_field.verbose_name},
|
|
|
|
|
)
|
|
|
|
|
|
2011-12-16 00:12:46 +08:00
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
|
class PasswordResetForm(forms.Form):
|
2019-03-10 18:03:42 +08:00
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
label=_("Email"),
|
|
|
|
|
max_length=254,
|
|
|
|
|
widget=forms.EmailInput(attrs={'autocomplete': 'email'})
|
|
|
|
|
)
|
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):
|
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
|
Send a django.core.mail.EmailMultiAlternatives to `to_email`.
|
2014-05-09 14:48:41 +08:00
|
|
|
|
"""
|
|
|
|
|
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.
|
|
|
|
|
"""
|
2019-12-17 10:51:57 +08:00
|
|
|
|
email_field_name = UserModel.get_email_field_name()
|
2016-09-19 20:55:18 +08:00
|
|
|
|
active_users = UserModel._default_manager.filter(**{
|
2019-12-17 10:51:57 +08:00
|
|
|
|
'%s__iexact' % email_field_name: email,
|
2016-09-19 20:55:18 +08:00
|
|
|
|
'is_active': True,
|
|
|
|
|
})
|
2019-12-17 10:51:57 +08:00
|
|
|
|
return (
|
|
|
|
|
u for u in active_users
|
|
|
|
|
if u.has_usable_password() and
|
|
|
|
|
_unicode_ci_compare(email, getattr(u, email_field_name))
|
|
|
|
|
)
|
2014-09-04 01:49:56 +08:00
|
|
|
|
|
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,
|
2015-06-18 20:49:35 +08:00
|
|
|
|
from_email=None, request=None, html_email_template_name=None,
|
|
|
|
|
extra_email_context=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
|
Generate a one-use only link for resetting password and send it to the
|
2012-01-02 22:51:25 +08:00
|
|
|
|
user.
|
2008-07-19 07:54:34 +08:00
|
|
|
|
"""
|
2013-02-23 20:39:21 +08:00
|
|
|
|
email = self.cleaned_data["email"]
|
2019-11-11 17:40:04 +08:00
|
|
|
|
if not domain_override:
|
|
|
|
|
current_site = get_current_site(request)
|
|
|
|
|
site_name = current_site.name
|
|
|
|
|
domain = current_site.domain
|
|
|
|
|
else:
|
|
|
|
|
site_name = domain = domain_override
|
2019-12-17 10:51:57 +08:00
|
|
|
|
email_field_name = UserModel.get_email_field_name()
|
2014-09-04 01:49:56 +08:00
|
|
|
|
for user in self.get_users(email):
|
2019-12-17 10:51:57 +08:00
|
|
|
|
user_email = getattr(user, email_field_name)
|
2014-05-09 14:48:41 +08:00
|
|
|
|
context = {
|
2019-12-17 10:51:57 +08:00
|
|
|
|
'email': user_email,
|
2007-06-20 04:04:54 +08:00
|
|
|
|
'domain': domain,
|
|
|
|
|
'site_name': site_name,
|
2018-10-02 07:18:04 +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',
|
2017-12-11 20:08:45 +08:00
|
|
|
|
**(extra_email_context or {}),
|
2008-07-19 07:54:34 +08:00
|
|
|
|
}
|
2016-03-29 06:33:29 +08:00
|
|
|
|
self.send_mail(
|
|
|
|
|
subject_template_name, email_template_name, context, from_email,
|
2019-12-17 10:51:57 +08:00
|
|
|
|
user_email, html_email_template_name=html_email_template_name,
|
2016-03-29 06:33:29 +08:00
|
|
|
|
)
|
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 = {
|
2019-06-28 00:39:47 +08:00
|
|
|
|
'password_mismatch': _('The two password fields didn’t match.'),
|
2011-12-16 00:12:46 +08:00
|
|
|
|
}
|
2016-03-29 06:33:29 +08:00
|
|
|
|
new_password1 = forms.CharField(
|
|
|
|
|
label=_("New password"),
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
2016-03-29 06:33:29 +08:00
|
|
|
|
strip=False,
|
|
|
|
|
help_text=password_validation.password_validators_help_text_html(),
|
|
|
|
|
)
|
|
|
|
|
new_password2 = forms.CharField(
|
|
|
|
|
label=_("New password confirmation"),
|
|
|
|
|
strip=False,
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
2016-03-29 06:33:29 +08:00
|
|
|
|
)
|
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
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2008-08-01 04:47:53 +08:00
|
|
|
|
|
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:
|
2020-02-12 21:48:49 +08:00
|
|
|
|
raise ValidationError(
|
2013-06-06 02:55:05 +08:00
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
|
code='password_mismatch',
|
|
|
|
|
)
|
2015-03-08 22:07:57 +08:00
|
|
|
|
password_validation.validate_password(password2, self.user)
|
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):
|
2015-03-08 22:07:57 +08:00
|
|
|
|
password = self.cleaned_data["new_password1"]
|
|
|
|
|
self.user.set_password(password)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
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
|
|
|
|
"""
|
2017-12-11 20:08:45 +08:00
|
|
|
|
error_messages = {
|
|
|
|
|
**SetPasswordForm.error_messages,
|
2016-03-29 06:33:29 +08:00
|
|
|
|
'password_incorrect': _("Your old password was entered incorrectly. Please enter it again."),
|
2017-12-11 20:08:45 +08:00
|
|
|
|
}
|
2015-11-23 18:46:19 +08:00
|
|
|
|
old_password = forms.CharField(
|
|
|
|
|
label=_("Old password"),
|
2016-03-15 04:21:05 +08:00
|
|
|
|
strip=False,
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'current-password', 'autofocus': True}),
|
2015-11-23 18:46:19 +08:00
|
|
|
|
)
|
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):
|
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
|
Validate that the old_password field is correct.
|
2008-08-01 04:47:53 +08:00
|
|
|
|
"""
|
|
|
|
|
old_password = self.cleaned_data["old_password"]
|
|
|
|
|
if not self.user.check_password(old_password):
|
2020-02-12 21:48:49 +08:00
|
|
|
|
raise 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 = {
|
2019-06-28 00:39:47 +08:00
|
|
|
|
'password_mismatch': _('The two password fields didn’t match.'),
|
2011-12-16 00:12:46 +08:00
|
|
|
|
}
|
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"),
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password', 'autofocus': True}),
|
2016-03-15 04:21:05 +08:00
|
|
|
|
strip=False,
|
2015-03-08 22:07:57 +08:00
|
|
|
|
help_text=password_validation.password_validators_help_text_html(),
|
2014-12-26 21:06:41 +08:00
|
|
|
|
)
|
|
|
|
|
password2 = forms.CharField(
|
|
|
|
|
label=_("Password (again)"),
|
2019-03-10 18:03:42 +08:00
|
|
|
|
widget=forms.PasswordInput(attrs={'autocomplete': 'new-password'}),
|
2016-03-15 04:21:05 +08:00
|
|
|
|
strip=False,
|
2015-07-20 19:52:29 +08:00
|
|
|
|
help_text=_("Enter the same password as before, for verification."),
|
2014-12-26 21:06:41 +08:00
|
|
|
|
)
|
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
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__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')
|
2020-09-25 00:37:55 +08:00
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
|
raise ValidationError(
|
|
|
|
|
self.error_messages['password_mismatch'],
|
|
|
|
|
code='password_mismatch',
|
|
|
|
|
)
|
2015-03-08 22:07:57 +08:00
|
|
|
|
password_validation.validate_password(password2, self.user)
|
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):
|
2017-01-25 04:31:57 +08:00
|
|
|
|
"""Save the new password."""
|
2015-03-08 22:07:57 +08:00
|
|
|
|
password = self.cleaned_data["password1"]
|
|
|
|
|
self.user.set_password(password)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
if commit:
|
|
|
|
|
self.user.save()
|
|
|
|
|
return self.user
|
2012-10-24 23:11:41 +08:00
|
|
|
|
|
2016-08-26 08:06:22 +08:00
|
|
|
|
@property
|
|
|
|
|
def changed_data(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
data = super().changed_data
|
2017-05-28 07:08:46 +08:00
|
|
|
|
for name in self.fields:
|
2012-10-24 23:11:41 +08:00
|
|
|
|
if name not in data:
|
|
|
|
|
return []
|
|
|
|
|
return ['password']
|