Hi,

2009/4/10 Amos Jeffries <[email protected]>:
>
> Is there a way to (optinally) cleanly erase the obsolete info from the prefs
> file as it goes? That would help migrating admin know where things are
> during this process which might take many weeks/months.
>
> AYJ
>
>

Amos, the new attached patch adds that ability.
I yanked the file saving from users.py from Pootle 1.2.1.
>From my tests it seems to work great.

Tell me what you think.

Flávio Martins
Index: settings.py
===================================================================
--- settings.py	(revision 10847)
+++ settings.py	(working copy)
@@ -141,6 +141,8 @@
 
 PREFSFILE = pootle_home('pootle.prefs')
 
+USERSPREFSFILE = pootle_home('users.prefs')
+
 PODIRECTORY = pootle_home('po')
 
 # Use the commented definition to authenticate first with Mozilla's LDAP system and then to fall back
@@ -148,6 +150,12 @@
 #AUTHENTICATION_BACKENDS = ('auth.ldap_backend.LdapBackend', 'django.contrib.auth.backends.ModelBackend',)
 AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
 
+# Use this commented definition if you want to accept the login information from a previous version of Pootle.
+# The user information will be migrated from the old users.prefs file to the database.
+#AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend', 'auth.prefs_backend.PrefsBackend',)
+# If you set the following True, the users migrated using the above backend will be removed from the users.prefs.
+#USERSPREFS_CLEANUP = False
+
 # LDAP Setup
 # The LDAP server.  Format:  protocol://hostname:port
 AUTH_LDAP_SERVER = ''
Index: auth/prefs_backend.py
===================================================================
--- auth/prefs_backend.py	(revision 0)
+++ auth/prefs_backend.py	(revision 0)
@@ -0,0 +1,67 @@
+from django.conf import settings
+from django.contrib.auth.models import User
+from django.forms.fields import email_re
+from jToolkit import prefs
+from jToolkit.web import session
+
+class PrefsBackend(object):
+    """
+    Authenticate against a Pootle installation users.prefs file.
+
+    Uses the login name and a hash of the password.
+
+    This authentication backend can be used for migration purposes
+    since it adds the authenticated user to the database.
+
+    """
+
+    def saveprefs(self, prefsparser):
+        """saves changed preferences back to disk"""
+        # TODO: this is a hack, fix it up nicely :-)
+        # Yanked from users.py with some changes
+        prefsfile = prefsparser.__root__.__dict__["_setvalue"].im_self
+        prefsfile.savefile()
+
+    def check_password(self, raw_password, enc_password):
+        return session.md5hexdigest(raw_password) == enc_password
+
+    def is_valid_email(self, email):
+        return True if email_re.match(email) else False
+
+    def authenticate(self, username=None, password=None):
+        p = prefs.PrefsParser()
+        p.parsefile(settings.USERSPREFSFILE)
+        login_valid = p.__hasattr__(username)
+        if login_valid:
+            if p.__getattr__(username).__hasattr__('passwdhash'):
+                pwd_valid = self.check_password(password,
+                                p.__getattr__(username).__getattr__('passwdhash'))
+                if pwd_valid:
+                    # User/Password combo matches. We will check if
+                    # the user exists, and if it does not we will add him.
+                    try:
+                        user = User.objects.get(username=username)
+                    except User.DoesNotExist:
+                        # Create a new user. Note that we can set password
+                        # to anything. We will set the password to be the
+                        # same as the one in the prefs since the hash matches.
+                        email = ''
+                        if p.__getattr__(username).__hasattr__('email'):
+                            email = p.__getattr__(username).__getattr__('email')
+                        if not self.is_valid_email(email):
+                            email = '[email protected]'
+                        user = User.objects.create_user(username, email, password)
+                        # TODO: We can migrate other attributes here
+                        user.save()
+                        # We can remove the user from users.prefs now.
+                        if settings.USERSPREFS_CLEANUP:
+                            p.removekey(username)
+                            self.saveprefs(p)
+                    return user
+        return None
+
+    def get_user(self, user_id):
+        try:
+            return User.objects.get(pk=user_id)
+        except User.DoesNotExist:
+            return None
------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Translate-pootle mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/translate-pootle

Reply via email to