Code

Ticket #578: auto_admin_md5_fixed.diff

File auto_admin_md5_fixed.diff, 2.0 KB (added by Hein-Pieter van Braam <hp@…>, 9 years ago)
Line 
1Index: django/models/auth.py
2===================================================================
3--- django/models/auth.py       (revision 728)
4+++ django/models/auth.py       (working copy)
5@@ -29,7 +29,7 @@
6     first_name = meta.CharField(maxlength=30, blank=True)
7     last_name = meta.CharField(maxlength=30, blank=True)
8     email = meta.EmailField('e-mail address', blank=True)
9-    password_md5 = meta.CharField('password', maxlength=32, help_text="Use an MD5 hash -- not the raw password.")
10+    password_md5 = meta.CharField('password', maxlength=32, help_text="Password will be automatically encrypted, and won't be visible once the user is added.")
11     is_staff = meta.BooleanField('staff status', help_text="Designates whether the user can log into this admin site.")
12     is_active = meta.BooleanField('active', default=True)
13     is_superuser = meta.BooleanField('superuser status')
14@@ -154,13 +154,10 @@
15                     raise SiteProfileNotAvailable
16         return self._profile_cache
17 
18-    def _module_create_user(username, email, password):
19+    def _module_create_user(username, email, password_md5):
20         "Creates and saves a User with the given username, e-mail and password."
21-        import md5
22-        password_md5 = md5.new(password).hexdigest()
23         now = datetime.datetime.now()
24         user = User(None, username, '', '', email.strip().lower(), password_md5, False, True, False, now, now)
25-        user.save()
26         return user
27 
28     def _module_make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
29@@ -170,6 +167,11 @@
30         from random import choice
31         return ''.join([choice(allowed_chars) for i in range(length)])
32 
33+    def _pre_save(self):
34+        import md5
35+        if (self.password_md5 != md5.new(self.password_md5).hexdigest()):
36+            self.password_md5 = md5.new(self.password_md5).hexdigest()
37+
38 class Message(meta.Model):
39     user = meta.ForeignKey(User)
40     message = meta.TextField()