mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #24910 -- Added createsuperuser support for non-unique USERNAME_FIELDs
Clarified docs to say that a non-unique USERNAME_FIELD is permissable as long as the custom auth backend can support it.
This commit is contained in:
parent
a391b17ad2
commit
1ea87c8c79
4 changed files with 58 additions and 17 deletions
|
@ -1,12 +1,23 @@
|
|||
from django.contrib.auth.models import AbstractBaseUser
|
||||
from django.contrib.auth.models import AbstractBaseUser, UserManager
|
||||
from django.db import models
|
||||
|
||||
|
||||
class CustomUserNonUniqueUsername(AbstractBaseUser):
|
||||
"A user with a non-unique username"
|
||||
"""
|
||||
A user with a non-unique username.
|
||||
|
||||
This model is not invalid if it is used with a custom authentication
|
||||
backend which supports non-unique usernames.
|
||||
"""
|
||||
username = models.CharField(max_length=30)
|
||||
email = models.EmailField(blank=True)
|
||||
is_staff = models.BooleanField(default=False)
|
||||
is_superuser = models.BooleanField(default=False)
|
||||
|
||||
USERNAME_FIELD = 'username'
|
||||
REQUIRED_FIELDS = ['email']
|
||||
|
||||
objects = UserManager()
|
||||
|
||||
class Meta:
|
||||
app_label = 'auth'
|
||||
|
|
|
@ -305,6 +305,33 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
|
|||
|
||||
self.assertEqual(CustomUser._default_manager.count(), 0)
|
||||
|
||||
@override_settings(
|
||||
AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername',
|
||||
AUTHENTICATION_BACKENDS=['my.custom.backend'],
|
||||
)
|
||||
def test_swappable_user_username_non_unique(self):
|
||||
@mock_inputs({
|
||||
'username': 'joe',
|
||||
'password': 'nopasswd',
|
||||
})
|
||||
def createsuperuser():
|
||||
new_io = six.StringIO()
|
||||
call_command(
|
||||
"createsuperuser",
|
||||
interactive=True,
|
||||
email="joe@somewhere.org",
|
||||
stdout=new_io,
|
||||
stdin=MockTTY(),
|
||||
)
|
||||
command_output = new_io.getvalue().strip()
|
||||
self.assertEqual(command_output, 'Superuser created successfully.')
|
||||
|
||||
for i in range(2):
|
||||
createsuperuser()
|
||||
|
||||
users = CustomUserNonUniqueUsername.objects.filter(username="joe")
|
||||
self.assertEqual(users.count(), 2)
|
||||
|
||||
def test_skip_if_not_in_TTY(self):
|
||||
"""
|
||||
If the command is not called from a TTY, it should be skipped and a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue