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):
|
||||
try:
|
||||
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):
|
||||
return user
|
||||
except UserModel.DoesNotExist:
|
||||
|
@ -64,7 +64,7 @@ class ModelBackend(object):
|
|||
def get_user(self, user_id):
|
||||
try:
|
||||
UserModel = get_user_model()
|
||||
return UserModel.objects.get(pk=user_id)
|
||||
return UserModel._default_manager.get(pk=user_id)
|
||||
except UserModel.DoesNotExist:
|
||||
return None
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ class UserCreationForm(forms.ModelForm):
|
|||
# but it sets a nicer error message than the ORM. See #13147.
|
||||
username = self.cleaned_data["username"]
|
||||
try:
|
||||
User.objects.get(username=username)
|
||||
User._default_manager.get(username=username)
|
||||
except User.DoesNotExist:
|
||||
return username
|
||||
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
||||
|
@ -217,7 +217,7 @@ class PasswordResetForm(forms.Form):
|
|||
"""
|
||||
UserModel = get_user_model()
|
||||
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):
|
||||
raise forms.ValidationError(self.error_messages['unknown'])
|
||||
if not any(user.is_active for user in self.users_cache):
|
||||
|
|
|
@ -18,7 +18,7 @@ def check_password(environ, username, password):
|
|||
|
||||
try:
|
||||
try:
|
||||
user = UserModel.objects.get_by_natural_key(username)
|
||||
user = UserModel._default_manager.get_by_natural_key(username)
|
||||
except UserModel.DoesNotExist:
|
||||
return None
|
||||
if not user.is_active:
|
||||
|
@ -37,7 +37,7 @@ def groups_for_user(environ, username):
|
|||
|
||||
try:
|
||||
try:
|
||||
user = UserModel.objects.get_by_natural_key(username)
|
||||
user = UserModel._default_manager.get_by_natural_key(username)
|
||||
except UserModel.DoesNotExist:
|
||||
return []
|
||||
try:
|
||||
|
|
|
@ -174,7 +174,7 @@ def get_default_username(check_db=True):
|
|||
# Don't return the default username if it is already taken.
|
||||
if check_db and default_username:
|
||||
try:
|
||||
auth_app.User.objects.get(username=default_username)
|
||||
auth_app.User._default_manager.get(username=default_username)
|
||||
except auth_app.User.DoesNotExist:
|
||||
pass
|
||||
else:
|
||||
|
|
|
@ -33,7 +33,7 @@ class Command(BaseCommand):
|
|||
UserModel = get_user_model()
|
||||
|
||||
try:
|
||||
u = UserModel.objects.using(options.get('database')).get(**{
|
||||
u = UserModel._default_manager.using(options.get('database')).get(**{
|
||||
UserModel.USERNAME_FIELD: username
|
||||
})
|
||||
except UserModel.DoesNotExist:
|
||||
|
|
|
@ -95,7 +95,7 @@ class Command(BaseCommand):
|
|||
username = None
|
||||
continue
|
||||
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:
|
||||
pass
|
||||
else:
|
||||
|
@ -134,6 +134,6 @@ class Command(BaseCommand):
|
|||
|
||||
user_data[self.UserModel.USERNAME_FIELD] = username
|
||||
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:
|
||||
self.stdout.write("Superuser created successfully.")
|
||||
|
|
|
@ -34,7 +34,7 @@ class BaseModelBackendTest(object):
|
|||
ContentType.objects.clear_cache()
|
||||
|
||||
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)
|
||||
user.is_staff = True
|
||||
user.save()
|
||||
|
@ -53,14 +53,14 @@ class BaseModelBackendTest(object):
|
|||
self.assertEqual(user.has_perm('auth.test'), False)
|
||||
|
||||
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)
|
||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||
user.user_permissions.add(perm)
|
||||
user.save()
|
||||
|
||||
# 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_group_permissions(), set([]))
|
||||
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')
|
||||
user.user_permissions.add(perm)
|
||||
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.has_perm('test'), False)
|
||||
self.assertEqual(user.has_perm('auth.test'), True)
|
||||
|
@ -81,7 +81,7 @@ class BaseModelBackendTest(object):
|
|||
group.permissions.add(perm)
|
||||
group.save()
|
||||
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'])
|
||||
self.assertEqual(user.get_all_permissions(), exp)
|
||||
self.assertEqual(user.get_group_permissions(), set(['auth.test_group']))
|
||||
|
@ -93,7 +93,7 @@ class BaseModelBackendTest(object):
|
|||
|
||||
def test_has_no_object_perm(self):
|
||||
"""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)
|
||||
perm = Permission.objects.create(name='test', content_type=content_type, codename='test')
|
||||
user.user_permissions.add(perm)
|
||||
|
@ -106,7 +106,7 @@ class BaseModelBackendTest(object):
|
|||
|
||||
def test_get_all_superuser_permissions(self):
|
||||
"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()))
|
||||
|
||||
|
||||
|
@ -151,13 +151,13 @@ class ExtensionUserModelBackendTest(BaseModelBackendTest, TestCase):
|
|||
UserModel = ExtensionUser
|
||||
|
||||
def create_users(self):
|
||||
self.user = ExtensionUser.objects.create_user(
|
||||
self.user = ExtensionUser._default_manager.create_user(
|
||||
username='test',
|
||||
email='test@example.com',
|
||||
password='test',
|
||||
date_of_birth=date(2006, 4, 25)
|
||||
)
|
||||
self.superuser = ExtensionUser.objects.create_superuser(
|
||||
self.superuser = ExtensionUser._default_manager.create_superuser(
|
||||
username='test2',
|
||||
email='test2@example.com',
|
||||
password='test',
|
||||
|
@ -178,12 +178,12 @@ class CustomPermissionsUserModelBackendTest(BaseModelBackendTest, TestCase):
|
|||
UserModel = CustomPermissionsUser
|
||||
|
||||
def create_users(self):
|
||||
self.user = CustomPermissionsUser.objects.create_user(
|
||||
self.user = CustomPermissionsUser._default_manager.create_user(
|
||||
email='test@example.com',
|
||||
password='test',
|
||||
date_of_birth=date(2006, 4, 25)
|
||||
)
|
||||
self.superuser = CustomPermissionsUser.objects.create_superuser(
|
||||
self.superuser = CustomPermissionsUser._default_manager.create_superuser(
|
||||
email='test2@example.com',
|
||||
password='test',
|
||||
date_of_birth=date(1976, 11, 8)
|
||||
|
|
|
@ -42,7 +42,7 @@ class CustomUser(AbstractBaseUser):
|
|||
is_admin = models.BooleanField(default=False)
|
||||
date_of_birth = models.DateField()
|
||||
|
||||
objects = CustomUserManager()
|
||||
custom_objects = CustomUserManager()
|
||||
|
||||
USERNAME_FIELD = 'email'
|
||||
REQUIRED_FIELDS = ['date_of_birth']
|
||||
|
@ -88,7 +88,7 @@ class CustomUser(AbstractBaseUser):
|
|||
class ExtensionUser(AbstractUser):
|
||||
date_of_birth = models.DateField()
|
||||
|
||||
objects = UserManager()
|
||||
custom_objects = UserManager()
|
||||
|
||||
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)
|
||||
date_of_birth = models.DateField()
|
||||
|
||||
objects = CustomPermissionsUserManager()
|
||||
custom_objects = CustomPermissionsUserManager()
|
||||
|
||||
USERNAME_FIELD = 'email'
|
||||
REQUIRED_FIELDS = ['date_of_birth']
|
||||
|
@ -136,7 +136,7 @@ class IsActiveTestUser1(AbstractBaseUser):
|
|||
"""
|
||||
username = models.CharField(max_length=30, unique=True)
|
||||
|
||||
objects = BaseUserManager()
|
||||
custom_objects = BaseUserManager()
|
||||
|
||||
USERNAME_FIELD = 'username'
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ class ModWsgiHandlerTestCase(TransactionTestCase):
|
|||
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
|
||||
self.assertTrue(check_password({}, 'unknown', '') is None)
|
||||
|
|
|
@ -125,7 +125,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
|||
email="joe@somewhere.org",
|
||||
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.assertFalse(u.has_usable_password())
|
||||
|
||||
|
@ -145,7 +145,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
|||
)
|
||||
command_output = new_io.getvalue().strip()
|
||||
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))
|
||||
|
||||
# created password should be unusable
|
||||
|
@ -167,7 +167,7 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
|||
skip_validation=True
|
||||
)
|
||||
|
||||
self.assertEqual(CustomUser.objects.count(), 0)
|
||||
self.assertEqual(CustomUser._default_manager.count(), 0)
|
||||
|
||||
|
||||
class PermissionDuplicationTestCase(TestCase):
|
||||
|
|
|
@ -137,6 +137,6 @@ class IsActiveTestCase(TestCase):
|
|||
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)
|
||||
user_fetched = UserModel._default_manager.get(pk=user.pk)
|
||||
# the attribute is always true for newly retrieved instance
|
||||
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')
|
||||
try:
|
||||
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):
|
||||
user = None
|
||||
|
||||
|
|
Loading…
Reference in New Issue