Code

Ticket #6273: 6273_django_auth_passwd_command_r2.diff

File 6273_django_auth_passwd_command_r2.diff, 1.5 KB (added by justinlilly, 6 years ago)

Applies cleanly to r9478 -- Removed all changes except passwd.py which works.

Line 
1Index: django/contrib/auth/management/commands/passwd.py
2===================================================================
3--- django/contrib/auth/management/commands/passwd.py   (revision 0)
4+++ django/contrib/auth/management/commands/passwd.py   (revision 0)
5@@ -0,0 +1,41 @@
6+from django.core.management.base import BaseCommand, CommandError
7+from django.contrib.auth.models import User
8+import getpass
9+
10+class Command(BaseCommand):
11+    help = "Clone of the UNIX program ``passwd'', for django.contrib.auth."
12+
13+    requires_model_validation = False
14+
15+    def _get_pass(self, prompt="Password: "):
16+        p = getpass.getpass(prompt=prompt)
17+        if not p:
18+            raise CommandError("aborted")
19+        return p
20+
21+    def handle(self, *args, **options):
22+        if len(args) > 1:
23+            raise CommandError("need exactly one or zero arguments for username")
24+
25+        if args:
26+            username, = args
27+        else:
28+            username = getpass.getuser()
29+
30+        try:
31+            u = User.objects.get(username=username)
32+        except User.DoesNotExist:
33+            raise CommandError("user %s does not exist" % username)
34+
35+        print "Changing password for user", u.username
36+        p1, p2 = 1, 2  # To make them mismatch.
37+        while p1 != p2:
38+            p1 = self._get_pass()
39+            p2 = self._get_pass("Password (again): ")
40+            if p1 != p2:
41+                print "Passwords do not match, try again"
42+
43+        u.set_password(p1)
44+        u.save()
45+
46+        return "Password changed successfully for user " + u.username