Ticket #6273: 6273_change_password_auth_command.diff

File 6273_change_password_auth_command.diff, 2.3 KB (added by Justin Lilly, 15 years ago)

Now with docs and renamed to change_password

  • new file django/contrib/auth/management/commands/change_password.py

    diff --git a/django/contrib/auth/management/commands/change_password.py b/django/contrib/auth/management/commands/change_password.py
    new file mode 100644
    index 0000000..34c7b04
    - +  
     1from django.core.management.base import BaseCommand, CommandError
     2from django.contrib.auth.models import User
     3import getpass
     4
     5class Command(BaseCommand):
     6    help = "Change a user's password for django.contrib.auth."
     7
     8    requires_model_validation = False
     9
     10    def _get_pass(self, prompt="Password: "):
     11        p = getpass.getpass(prompt=prompt)
     12        if not p:
     13            raise CommandError("aborted")
     14        return p
     15
     16    def handle(self, *args, **options):
     17        if len(args) > 1:
     18            raise CommandError("need exactly one or zero arguments for username")
     19
     20        if args:
     21            username, = args
     22        else:
     23            username = getpass.getuser()
     24
     25        try:
     26            u = User.objects.get(username=username)
     27        except User.DoesNotExist:
     28            raise CommandError("user %s does not exist" % username)
     29
     30        print "Changing password for user", u.username
     31        p1, p2 = 1, 2  # To make them mismatch.
     32        while p1 != p2:
     33            p1 = self._get_pass()
     34            p2 = self._get_pass("Password (again): ")
     35            if p1 != p2:
     36                print "Passwords do not match, try again"
     37
     38        u.set_password(p1)
     39        u.save()
     40
     41        return "Password changed successfully for user " + u.username
  • docs/topics/auth.txt

    diff --git a/docs/topics/auth.txt b/docs/topics/auth.txt
    index 7858e44..f4c046b 100644
    a b Don't set the :attr:`~django.contrib.auth.models.User.password` attribute  
    330330directly unless you know what you're doing. This is explained in the next
    331331section.
    332332
     333.. versionadded:: 1.2
     334   The ``manage.py change_password`` command is new.
     335
     336:djadmin:`manage.py change_password <username>` offers a method of
     337changing a User's password from the command line. It prompts you to
     338change the password of a given user which you must enter twice. If
     339they both match, the new password will be changed immediately. If you
     340do not supply a user, the command will attempt to change the password
     341whose username matches the current user.
     342
    333343Passwords
    334344---------
    335345
Back to Top