Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #19061 -- added is_active attribute to AbstractBaseUser

  • Loading branch information...
commit 4ea8105120121c7ef0d3dd6eb23f2bf5f55b496a 1 parent f1cc2be
Preston Holmes ptone authored
6 django/contrib/auth/forms.py
View
@@ -209,10 +209,12 @@ def clean_email(self):
"""
UserModel = get_user_model()
email = self.cleaned_data["email"]
- self.users_cache = UserModel.objects.filter(email__iexact=email,
- is_active=True)
+ self.users_cache = UserModel.objects.filter(email__iexact=email)
if not len(self.users_cache):
raise forms.ValidationError(self.error_messages['unknown'])
+ if not any(user.is_active for user in self.users_cache):
+ # none of the filtered users are active
+ raise forms.ValidationError(self.error_messages['unknown'])
if any((user.password == UNUSABLE_PASSWORD)
for user in self.users_cache):
raise forms.ValidationError(self.error_messages['unusable'])
2  django/contrib/auth/models.py
View
@@ -232,6 +232,8 @@ class AbstractBaseUser(models.Model):
password = models.CharField(_('password'), max_length=128)
last_login = models.DateTimeField(_('last login'), default=timezone.now)
+ is_active = True
+
REQUIRED_FIELDS = []
class Meta:
17 django/contrib/auth/tests/custom_user.py
View
@@ -88,3 +88,20 @@ class ExtensionUser(AbstractUser):
class Meta:
app_label = 'auth'
+
+
+class IsActiveTestUser1(AbstractBaseUser):
+ """
+ This test user class and derivatives test the default is_active behavior
+ """
+ username = models.CharField(max_length=30, unique=True)
+
+ objects = BaseUserManager()
+
+ USERNAME_FIELD = 'username'
+
+ class Meta:
+ app_label = 'auth'
+
+ # the is_active attr is provided by AbstractBaseUser
+
32 django/contrib/auth/tests/models.py
View
@@ -1,4 +1,5 @@
from django.conf import settings
+from django.contrib.auth import get_user_model
from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable,
UserManager)
from django.contrib.auth.tests.utils import skipIfCustomUser
@@ -98,3 +99,34 @@ def test_empty_username(self):
self.assertRaisesMessage(ValueError,
'The given username must be set',
User.objects.create_user, username='')
+
+class IsActiveTestCase(TestCase):
+ """
+ Tests the behavior of the guaranteed is_active attribute
+ """
+
+ def test_builtin_user_isactive(self):
+ user = User.objects.create(username='foo', email='foo@bar.com')
+ # is_active is true by default
+ self.assertEqual(user.is_active, True)
+ user.is_active = False
+ user.save()
+ user_fetched = User.objects.get(pk=user.pk)
+ # the is_active flag is saved
+ self.assertFalse(user_fetched.is_active)
+
+ @override_settings(AUTH_USER_MODEL='auth.IsActiveTestUser1')
+ def test_is_active_field_default(self):
+ """
+ tests that the default value for is_active is provided
+ """
+ UserModel = get_user_model()
+ user = UserModel(username='foo')
+ self.assertEqual(user.is_active, True)
+ # you can set the attribute - but it will not save
+ user.is_active = False
+ # there should be no problem saving - but the attribute is not saved
+ user.save()
+ user_fetched = UserModel.objects.get(pk=user.pk)
+ # the attribute is always true for newly retrieved instance
+ self.assertEqual(user_fetched.is_active, True)
9 docs/topics/auth.txt
View
@@ -1911,6 +1911,15 @@ password resets. You must then provide some key implementation details:
``REQUIRED_FIELDS`` must contain all required fields on your User
model, but should *not* contain the ``USERNAME_FIELD``.
+ .. attribute:: User.is_active
+
+ A boolean attribute that indicates whether the user is considered
+ "active". This attribute is provided as an attribute on
+ ``AbstractBaseUser`` defaulting to ``True``. How you choose to
+ implement it will depend on the details of your chosen auth backends.
+ See the documentation of the :attr:`attribute on the builtin user model
+ <django.contrib.auth.models.User.is_active>` for details.
+
.. method:: User.get_full_name():
A longer formal identifier for the user. A common interpretation
Please sign in to comment.
Something went wrong with that request. Please try again.