Skip to content

Commit

Permalink
Fixed #17046 -- Added a check if the username passed to User.objects.…
Browse files Browse the repository at this point in the history
…create_user is empty or not. Thanks, kwadrat.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17628 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Mar 2, 2012
1 parent 40b248a commit fcaf8ea
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/contrib/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def create_user(self, username, email=None, password=None):
Creates and saves a User with the given username, email and password.
"""
now = timezone.now()
if not username:
raise ValueError('The given username must be set')
email = UserManager.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=False, is_active=True, is_superuser=False,
Expand Down
5 changes: 5 additions & 0 deletions django/contrib/auth/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ def test_create_user_email_domain_normalize(self):
def test_create_user_email_domain_normalize_with_whitespace(self):
returned = UserManager.normalize_email('email\ with_whitespace@D.COM')
self.assertEquals(returned, 'email\ with_whitespace@d.com')

def test_empty_username(self):
self.assertRaisesMessage(ValueError,
'The given username must be set',
User.objects.create_user, username='')
4 changes: 3 additions & 1 deletion docs/topics/auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ Manager functions
.. method:: models.UserManager.create_user(username, email=None, password=None)

.. versionchanged:: 1.4
The ``email`` parameter was made optional.
The ``email`` parameter was made optional. The username
parameter is now checked for emptiness and raises a
:exc:`ValueError` in case of a negative result.

Creates, saves and returns a :class:`~django.contrib.auth.models.User`.

Expand Down

0 comments on commit fcaf8ea

Please sign in to comment.