Code

Ticket #6273: django_auth_passwd_command.diff

File django_auth_passwd_command.diff, 6.7 KB (added by toxik, 6 years ago)
Line 
1Index: django/contrib/auth/management.py
2===================================================================
3--- django/contrib/auth/management.py   (revision 6977)
4+++ django/contrib/auth/management.py   (working copy)
5@@ -1,49 +0,0 @@
6-"""
7-Creates permissions for all installed apps that need permissions.
8-"""
9-
10-from django.dispatch import dispatcher
11-from django.db.models import get_models, signals
12-from django.contrib.auth import models as auth_app
13-
14-def _get_permission_codename(action, opts):
15-    return u'%s_%s' % (action, opts.object_name.lower())
16-
17-def _get_all_permissions(opts):
18-    "Returns (codename, name) for all permissions in the given opts."
19-    perms = []
20-    for action in ('add', 'change', 'delete'):
21-        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
22-    return perms + list(opts.permissions)
23-
24-def create_permissions(app, created_models, verbosity):
25-    from django.contrib.contenttypes.models import ContentType
26-    from django.contrib.auth.models import Permission
27-    app_models = get_models(app)
28-    if not app_models:
29-        return
30-    for klass in app_models:
31-        ctype = ContentType.objects.get_for_model(klass)
32-        for codename, name in _get_all_permissions(klass._meta):
33-            p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
34-                defaults={'name': name, 'content_type': ctype})
35-            if created and verbosity >= 2:
36-                print "Adding permission '%s'" % p
37-
38-def create_superuser(app, created_models, verbosity, **kwargs):
39-    from django.contrib.auth.models import User
40-    from django.contrib.auth.create_superuser import createsuperuser as do_create
41-    if User in created_models and kwargs.get('interactive', True):
42-        msg = "\nYou just installed Django's auth system, which means you don't have " \
43-                "any superusers defined.\nWould you like to create one now? (yes/no): "
44-        confirm = raw_input(msg)
45-        while 1:
46-            if confirm not in ('yes', 'no'):
47-                confirm = raw_input('Please enter either "yes" or "no": ')
48-                continue
49-            if confirm == 'yes':
50-                do_create()
51-            break
52-
53-dispatcher.connect(create_permissions, signal=signals.post_syncdb)
54-dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
55Index: django/contrib/auth/management/commands/__init__.py
56===================================================================
57--- django/contrib/auth/management/commands/__init__.py (revision 0)
58+++ django/contrib/auth/management/commands/__init__.py (revision 0)
59@@ -0,0 +1 @@
60+"""Placeholder file to make directory a package."""
61Index: django/contrib/auth/management/commands/passwd.py
62===================================================================
63--- django/contrib/auth/management/commands/passwd.py   (revision 0)
64+++ django/contrib/auth/management/commands/passwd.py   (revision 0)
65@@ -0,0 +1,41 @@
66+from django.core.management.base import BaseCommand, CommandError
67+from django.contrib.auth.models import User
68+import getpass
69+
70+class Command(BaseCommand):
71+    help = "Clone of the UNIX program ``passwd'', for django.contrib.auth."
72+
73+    requires_model_validation = False
74+
75+    def _get_pass(self, prompt="Password: "):
76+        p = getpass.getpass(prompt=prompt)
77+        if not p:
78+            raise CommandError("aborted")
79+        return p
80+
81+    def handle(self, *args, **options):
82+        if len(args) > 1:
83+            raise CommandError("need exactly one or zero arguments for username")
84+
85+        if args:
86+            username, = args
87+        else:
88+            username = getpass.getuser()
89+
90+        try:
91+            u = User.objects.get(username=username)
92+        except User.DoesNotExist:
93+            raise CommandError("user %s does not exist" % username)
94+
95+        print "Changing password for user", u.username
96+        p1, p2 = 1, 2  # To make them mismatch.
97+        while p1 != p2:
98+            p1 = self._get_pass()
99+            p2 = self._get_pass("Password (again): ")
100+            if p1 != p2:
101+                print "Passwords do not match, try again"
102+
103+        u.set_password(p1)
104+        u.save()
105+
106+        return "Password changed successfully for user " + u.username
107Index: django/contrib/auth/management/__init__.py
108===================================================================
109--- django/contrib/auth/management/__init__.py  (revision 0)
110+++ django/contrib/auth/management/__init__.py  (revision 0)
111@@ -0,0 +1,49 @@
112+"""
113+Creates permissions for all installed apps that need permissions.
114+"""
115+
116+from django.dispatch import dispatcher
117+from django.db.models import get_models, signals
118+from django.contrib.auth import models as auth_app
119+
120+def _get_permission_codename(action, opts):
121+    return u'%s_%s' % (action, opts.object_name.lower())
122+
123+def _get_all_permissions(opts):
124+    "Returns (codename, name) for all permissions in the given opts."
125+    perms = []
126+    for action in ('add', 'change', 'delete'):
127+        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
128+    return perms + list(opts.permissions)
129+
130+def create_permissions(app, created_models, verbosity):
131+    from django.contrib.contenttypes.models import ContentType
132+    from django.contrib.auth.models import Permission
133+    app_models = get_models(app)
134+    if not app_models:
135+        return
136+    for klass in app_models:
137+        ctype = ContentType.objects.get_for_model(klass)
138+        for codename, name in _get_all_permissions(klass._meta):
139+            p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
140+                defaults={'name': name, 'content_type': ctype})
141+            if created and verbosity >= 2:
142+                print "Adding permission '%s'" % p
143+
144+def create_superuser(app, created_models, verbosity, **kwargs):
145+    from django.contrib.auth.models import User
146+    from django.contrib.auth.create_superuser import createsuperuser as do_create
147+    if User in created_models and kwargs.get('interactive', True):
148+        msg = "\nYou just installed Django's auth system, which means you don't have " \
149+                "any superusers defined.\nWould you like to create one now? (yes/no): "
150+        confirm = raw_input(msg)
151+        while 1:
152+            if confirm not in ('yes', 'no'):
153+                confirm = raw_input('Please enter either "yes" or "no": ')
154+                continue
155+            if confirm == 'yes':
156+                do_create()
157+            break
158+
159+dispatcher.connect(create_permissions, signal=signals.post_syncdb)
160+dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)