Fixed #19596 -- Use `_default_manager` instead of `objects` in the auth app.
This is needed to support custom user models which don't define a manager named `objects`.
This commit is contained in:
parent
456f9b9847
commit
cc4de61a2b
|
@ -13,7 +13,7 @@ class ModelBackend(object):
|
||||||
def authenticate(self, username=None, password=None):
|
def authenticate(self, username=None, password=None):
|
||||||
try:
|
try:
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
user = UserModel.objects.get_by_natural_key(username)
|
user = UserModel._default_manager.get_by_natural_key(username)
|
||||||
if user.check_password(password):
|
if user.check_password(password):
|
||||||
return user
|
return user
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
|
@ -64,7 +64,7 @@ class ModelBackend(object):
|
||||||
def get_user(self, user_id):
|
def get_user(self, user_id):
|
||||||
try:
|
try:
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
return UserModel.objects.get(pk=user_id)
|
return UserModel._default_manager.get(pk=user_id)
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ class UserCreationForm(forms.ModelForm):
|
||||||
# but it sets a nicer error message than the ORM. See #13147.
|
# but it sets a nicer error message than the ORM. See #13147.
|
||||||
username = self.cleaned_data["username"]
|
username = self.cleaned_data["username"]
|
||||||
try:
|
try:
|
||||||
User.objects.get(username=username)
|
User._default_manager.get(username=username)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
return username
|
return username
|
||||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
||||||
|
@ -217,7 +217,7 @@ 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._default_manager.filter(email__iexact=email)
|
||||||
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):
|
if not any(user.is_active for user in self.users_cache):
|
||||||
|
|
|
@ -18,7 +18,7 @@ def check_password(environ, username, password):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
user = UserModel.objects.get_by_natural_key(username)
|
user = UserModel._default_manager.get_by_natural_key(username)
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
if not user.is_active:
|
if not user.is_active:
|
||||||
|
@ -37,7 +37,7 @@ def groups_for_user(environ, username):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
user = UserModel.objects.get_by_natural_key(username)
|
user = UserModel._default_manager.get_by_natural_key(username)
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
return []
|
return []
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -174,7 +174,7 @@ def get_default_username(check_db=True):
|
||||||
# Don't return the default username if it is already taken.
|
# Don't return the default username if it is already taken.
|
||||||
if check_db and default_username:
|
if check_db and default_username:
|
||||||
try:
|
try:
|
||||||
auth_app.User.objects.get(username=default_username)
|
auth_app.User._default_manager.get(username=default_username)
|
||||||
except auth_app.User.DoesNotExist:
|
except auth_app.User.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -33,7 +33,7 @@ class Command(BaseCommand):
|
||||||
UserModel = get_user_model()
|
UserModel = get_user_model()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
u = UserModel.objects.using(options.get('database')).get(**{
|
u = UserModel._default_manager.using(options.get('database')).get(**{
|
||||||
UserModel.USERNAME_FIELD: username
|
UserModel.USERNAME_FIELD: username
|
||||||
})
|
})
|
||||||
except UserModel.DoesNotExist:
|
except UserModel.DoesNotExist:
|
||||||
|
|
|
@ -95,7 +95,7 @@ class Command(BaseCommand):
|
||||||
username = None
|
username = None
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
self.UserModel.objects.db_manager(database).get_by_natural_key(username)
|
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
|
||||||
except self.UserModel.DoesNotExist:
|
except self.UserModel.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
@ -134,6 +134,6 @@ class Command(BaseCommand):
|
||||||
|
|
||||||
user_data[self.UserModel.USERNAME_FIELD] = username
|
user_data[self.UserModel.USERNAME_FIELD] = username
|
||||||
user_data['password'] = password
|
user_data['password'] = password
|
||||||
self.UserModel.objects.db_manager(database).create_superuser(**user_data)
|
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
|
||||||
if verbosity >= 1:
|
if verbosity >= 1:
|
||||||
self.stdout.write("Superuser created successfully.")
|
self.stdout.write("Superuser created successfully.")
|
||||||
|
|
|
@ -34,7 +34,7 @@ class BaseModelBackendTest(object):
|
||||||
ContentType.objects.clear_cache()
|
ContentType.objects.clear_cache()
|
||||||
|
|
||||||
def test_has_perm(self):
|
def test_has_perm(self):
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
self.assertEqual(user.has_perm('auth.test'), False)
|
self.assertEqual(user.has_perm('auth.test'), False)
|
||||||
user.is_staff = True
|
user.is_staff = True
|
||||||
user.save()
|
user.save()
|
||||||
|
@ -53,14 +53,14 @@ class BaseModelBackendTest(object):
|
||||||
self.assertEqual(user.has_perm('auth.test'), False)
|
self.assertEqual(user.has_perm('auth.test'), False)
|
||||||
|
|
||||||
def test_custom_perms(self):
|
def test_custom_perms(self):
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
content_type = ContentType.objects.get_for_model(Group)
|
content_type = ContentType.objects.get_for_model(Group)
|
||||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||||
user.user_permissions.add(perm)
|
user.user_permissions.add(perm)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
# reloading user to purge the _perm_cache
|
# reloading user to purge the _perm_cache
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
self.assertEqual(user.get_all_permissions() == set(['auth.test']), True)
|
self.assertEqual(user.get_all_permissions() == set(['auth.test']), True)
|
||||||
self.assertEqual(user.get_group_permissions(), set([]))
|
self.assertEqual(user.get_group_permissions(), set([]))
|
||||||
self.assertEqual(user.has_module_perms('Group'), False)
|
self.assertEqual(user.has_module_perms('Group'), False)
|
||||||
|
@ -71,7 +71,7 @@ class BaseModelBackendTest(object):
|
||||||
perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
|
perm = Permission.objects.create(name='test3', content_type=content_type, codename='test3')
|
||||||
user.user_permissions.add(perm)
|
user.user_permissions.add(perm)
|
||||||
user.save()
|
user.save()
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3']))
|
self.assertEqual(user.get_all_permissions(), set(['auth.test2', 'auth.test', 'auth.test3']))
|
||||||
self.assertEqual(user.has_perm('test'), False)
|
self.assertEqual(user.has_perm('test'), False)
|
||||||
self.assertEqual(user.has_perm('auth.test'), True)
|
self.assertEqual(user.has_perm('auth.test'), True)
|
||||||
|
@ -81,7 +81,7 @@ class BaseModelBackendTest(object):
|
||||||
group.permissions.add(perm)
|
group.permissions.add(perm)
|
||||||
group.save()
|
group.save()
|
||||||
user.groups.add(group)
|
user.groups.add(group)
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group'])
|
exp = set(['auth.test2', 'auth.test', 'auth.test3', 'auth.test_group'])
|
||||||
self.assertEqual(user.get_all_permissions(), exp)
|
self.assertEqual(user.get_all_permissions(), exp)
|
||||||
self.assertEqual(user.get_group_permissions(), set(['auth.test_group']))
|
self.assertEqual(user.get_group_permissions(), set(['auth.test_group']))
|
||||||
|
@ -93,7 +93,7 @@ class BaseModelBackendTest(object):
|
||||||
|
|
||||||
def test_has_no_object_perm(self):
|
def test_has_no_object_perm(self):
|
||||||
"""Regressiontest for #12462"""
|
"""Regressiontest for #12462"""
|
||||||
user = self.UserModel.objects.get(pk=self.user.pk)
|
user = self.UserModel._default_manager.get(pk=self.user.pk)
|
||||||
content_type = ContentType.objects.get_for_model(Group)
|
content_type = ContentType.objects.get_for_model(Group)
|
||||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||||
user.user_permissions.add(perm)
|
user.user_permissions.add(perm)
|
||||||
|
@ -106,7 +106,7 @@ class BaseModelBackendTest(object):
|
||||||
|
|
||||||
def test_get_all_superuser_permissions(self):
|
def test_get_all_superuser_permissions(self):
|
||||||
"A superuser has all permissions. Refs #14795"
|
"A superuser has all permissions. Refs #14795"
|
||||||
user = self.UserModel.objects.get(pk=self.superuser.pk)
|
user = self.UserModel._default_manager.get(pk=self.superuser.pk)
|
||||||
self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all()))
|
self.assertEqual(len(user.get_all_permissions()), len(Permission.objects.all()))
|
||||||
|
|
||||||
|
|
||||||
|
@ -151,13 +151,13 @@ class ExtensionUserModelBackendTest(BaseModelBackendTest, TestCase):
|
||||||
UserModel = ExtensionUser
|
UserModel = ExtensionUser
|
||||||
|
|
||||||
def create_users(self):
|
def create_users(self):
|
||||||
self.user = ExtensionUser.objects.create_user(
|
self.user = ExtensionUser._default_manager.create_user(
|
||||||
username='test',
|
username='test',
|
||||||
email='test@example.com',
|
email='test@example.com',
|
||||||
password='test',
|
password='test',
|
||||||
date_of_birth=date(2006, 4, 25)
|
date_of_birth=date(2006, 4, 25)
|
||||||
)
|
)
|
||||||
self.superuser = ExtensionUser.objects.create_superuser(
|
self.superuser = ExtensionUser._default_manager.create_superuser(
|
||||||
username='test2',
|
username='test2',
|
||||||
email='test2@example.com',
|
email='test2@example.com',
|
||||||
password='test',
|
password='test',
|
||||||
|
@ -178,12 +178,12 @@ class CustomPermissionsUserModelBackendTest(BaseModelBackendTest, TestCase):
|
||||||
UserModel = CustomPermissionsUser
|
UserModel = CustomPermissionsUser
|
||||||
|
|
||||||
def create_users(self):
|
def create_users(self):
|
||||||
self.user = CustomPermissionsUser.objects.create_user(
|
self.user = CustomPermissionsUser._default_manager.create_user(
|
||||||
email='test@example.com',
|
email='test@example.com',
|
||||||
password='test',
|
password='test',
|
||||||
date_of_birth=date(2006, 4, 25)
|
date_of_birth=date(2006, 4, 25)
|
||||||
)
|
)
|
||||||
self.superuser = CustomPermissionsUser.objects.create_superuser(
|
self.superuser = CustomPermissionsUser._default_manager.create_superuser(
|
||||||
email='test2@example.com',
|
email='test2@example.com',
|
||||||
password='test',
|
password='test',
|
||||||
date_of_birth=date(1976, 11, 8)
|
date_of_birth=date(1976, 11, 8)
|
||||||
|
|
|
@ -42,7 +42,7 @@ class CustomUser(AbstractBaseUser):
|
||||||
is_admin = models.BooleanField(default=False)
|
is_admin = models.BooleanField(default=False)
|
||||||
date_of_birth = models.DateField()
|
date_of_birth = models.DateField()
|
||||||
|
|
||||||
objects = CustomUserManager()
|
custom_objects = CustomUserManager()
|
||||||
|
|
||||||
USERNAME_FIELD = 'email'
|
USERNAME_FIELD = 'email'
|
||||||
REQUIRED_FIELDS = ['date_of_birth']
|
REQUIRED_FIELDS = ['date_of_birth']
|
||||||
|
@ -88,7 +88,7 @@ class CustomUser(AbstractBaseUser):
|
||||||
class ExtensionUser(AbstractUser):
|
class ExtensionUser(AbstractUser):
|
||||||
date_of_birth = models.DateField()
|
date_of_birth = models.DateField()
|
||||||
|
|
||||||
objects = UserManager()
|
custom_objects = UserManager()
|
||||||
|
|
||||||
REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
|
REQUIRED_FIELDS = AbstractUser.REQUIRED_FIELDS + ['date_of_birth']
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ class CustomPermissionsUser(AbstractBaseUser, PermissionsMixin):
|
||||||
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
|
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
|
||||||
date_of_birth = models.DateField()
|
date_of_birth = models.DateField()
|
||||||
|
|
||||||
objects = CustomPermissionsUserManager()
|
custom_objects = CustomPermissionsUserManager()
|
||||||
|
|
||||||
USERNAME_FIELD = 'email'
|
USERNAME_FIELD = 'email'
|
||||||
REQUIRED_FIELDS = ['date_of_birth']
|
REQUIRED_FIELDS = ['date_of_birth']
|
||||||
|
@ -136,7 +136,7 @@ class IsActiveTestUser1(AbstractBaseUser):
|
||||||
"""
|
"""
|
||||||
username = models.CharField(max_length=30, unique=True)
|
username = models.CharField(max_length=30, unique=True)
|
||||||
|
|
||||||
objects = BaseUserManager()
|
custom_objects = BaseUserManager()
|
||||||
|
|
||||||
USERNAME_FIELD = 'username'
|
USERNAME_FIELD = 'username'
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ class ModWsgiHandlerTestCase(TransactionTestCase):
|
||||||
with custom user installed
|
with custom user installed
|
||||||
"""
|
"""
|
||||||
|
|
||||||
CustomUser.objects.create_user('test@example.com', '1990-01-01', 'test')
|
CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
|
||||||
|
|
||||||
# User not in database
|
# User not in database
|
||||||
self.assertTrue(check_password({}, 'unknown', '') is None)
|
self.assertTrue(check_password({}, 'unknown', '') is None)
|
||||||
|
|
|
@ -125,7 +125,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
email="joe@somewhere.org",
|
email="joe@somewhere.org",
|
||||||
stdout=new_io
|
stdout=new_io
|
||||||
)
|
)
|
||||||
u = User.objects.get(username="joe+admin@somewhere.org")
|
u = User._default_manager.get(username="joe+admin@somewhere.org")
|
||||||
self.assertEqual(u.email, 'joe@somewhere.org')
|
self.assertEqual(u.email, 'joe@somewhere.org')
|
||||||
self.assertFalse(u.has_usable_password())
|
self.assertFalse(u.has_usable_password())
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
)
|
)
|
||||||
command_output = new_io.getvalue().strip()
|
command_output = new_io.getvalue().strip()
|
||||||
self.assertEqual(command_output, 'Superuser created successfully.')
|
self.assertEqual(command_output, 'Superuser created successfully.')
|
||||||
u = CustomUser.objects.get(email="joe@somewhere.org")
|
u = CustomUser._default_manager.get(email="joe@somewhere.org")
|
||||||
self.assertEqual(u.date_of_birth, date(1976, 4, 1))
|
self.assertEqual(u.date_of_birth, date(1976, 4, 1))
|
||||||
|
|
||||||
# created password should be unusable
|
# created password should be unusable
|
||||||
|
@ -167,7 +167,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
||||||
skip_validation=True
|
skip_validation=True
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(CustomUser.objects.count(), 0)
|
self.assertEqual(CustomUser._default_manager.count(), 0)
|
||||||
|
|
||||||
|
|
||||||
class PermissionDuplicationTestCase(TestCase):
|
class PermissionDuplicationTestCase(TestCase):
|
||||||
|
|
|
@ -137,6 +137,6 @@ class IsActiveTestCase(TestCase):
|
||||||
user.is_active = False
|
user.is_active = False
|
||||||
# there should be no problem saving - but the attribute is not saved
|
# there should be no problem saving - but the attribute is not saved
|
||||||
user.save()
|
user.save()
|
||||||
user_fetched = UserModel.objects.get(pk=user.pk)
|
user_fetched = UserModel._default_manager.get(pk=user.pk)
|
||||||
# the attribute is always true for newly retrieved instance
|
# the attribute is always true for newly retrieved instance
|
||||||
self.assertEqual(user_fetched.is_active, True)
|
self.assertEqual(user_fetched.is_active, True)
|
||||||
|
|
|
@ -200,7 +200,7 @@ def password_reset_confirm(request, uidb36=None, token=None,
|
||||||
post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
|
post_reset_redirect = reverse('django.contrib.auth.views.password_reset_complete')
|
||||||
try:
|
try:
|
||||||
uid_int = base36_to_int(uidb36)
|
uid_int = base36_to_int(uidb36)
|
||||||
user = UserModel.objects.get(pk=uid_int)
|
user = UserModel._default_manager.get(pk=uid_int)
|
||||||
except (ValueError, OverflowError, UserModel.DoesNotExist):
|
except (ValueError, OverflowError, UserModel.DoesNotExist):
|
||||||
user = None
|
user = None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue