mirror of
				https://github.com/django/django.git
				synced 2025-10-26 07:06:08 +00:00 
			
		
		
		
	Fixed #19061 -- added is_active attribute to AbstractBaseUser
This commit is contained in:
		| @@ -209,10 +209,12 @@ class PasswordResetForm(forms.Form): | |||||||
|         """ |         """ | ||||||
|         UserModel = get_user_model() |         UserModel = get_user_model() | ||||||
|         email = self.cleaned_data["email"] |         email = self.cleaned_data["email"] | ||||||
|         self.users_cache = UserModel.objects.filter(email__iexact=email, |         self.users_cache = UserModel.objects.filter(email__iexact=email) | ||||||
|                                                     is_active=True) |  | ||||||
|         if not len(self.users_cache): |         if not len(self.users_cache): | ||||||
|             raise forms.ValidationError(self.error_messages['unknown']) |             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) |         if any((user.password == UNUSABLE_PASSWORD) | ||||||
|                for user in self.users_cache): |                for user in self.users_cache): | ||||||
|             raise forms.ValidationError(self.error_messages['unusable']) |             raise forms.ValidationError(self.error_messages['unusable']) | ||||||
|   | |||||||
| @@ -232,6 +232,8 @@ class AbstractBaseUser(models.Model): | |||||||
|     password = models.CharField(_('password'), max_length=128) |     password = models.CharField(_('password'), max_length=128) | ||||||
|     last_login = models.DateTimeField(_('last login'), default=timezone.now) |     last_login = models.DateTimeField(_('last login'), default=timezone.now) | ||||||
|  |  | ||||||
|  |     is_active = True | ||||||
|  |  | ||||||
|     REQUIRED_FIELDS = [] |     REQUIRED_FIELDS = [] | ||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|   | |||||||
| @@ -88,3 +88,20 @@ class ExtensionUser(AbstractUser): | |||||||
|  |  | ||||||
|     class Meta: |     class Meta: | ||||||
|         app_label = 'auth' |         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 | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| from django.conf import settings | from django.conf import settings | ||||||
|  | from django.contrib.auth import get_user_model | ||||||
| from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable, | from django.contrib.auth.models import (Group, User, SiteProfileNotAvailable, | ||||||
|     UserManager) |     UserManager) | ||||||
| from django.contrib.auth.tests.utils import skipIfCustomUser | from django.contrib.auth.tests.utils import skipIfCustomUser | ||||||
| @@ -98,3 +99,34 @@ class UserManagerTestCase(TestCase): | |||||||
|         self.assertRaisesMessage(ValueError, |         self.assertRaisesMessage(ValueError, | ||||||
|                                  'The given username must be set', |                                  'The given username must be set', | ||||||
|                                   User.objects.create_user, username='') |                                   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) | ||||||
|   | |||||||
| @@ -1911,6 +1911,15 @@ password resets. You must then provide some key implementation details: | |||||||
|             ``REQUIRED_FIELDS`` must contain all required fields on your User |             ``REQUIRED_FIELDS`` must contain all required fields on your User | ||||||
|             model, but should *not* contain the ``USERNAME_FIELD``. |             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(): |     .. method:: User.get_full_name(): | ||||||
|  |  | ||||||
|         A longer formal identifier for the user. A common interpretation |         A longer formal identifier for the user. A common interpretation | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user