Hi,
I found the time to test my previous patch and improve it.
This email has the new resulting patch attached.
How does it work?
1. Pootle tries to authenticate the user using the database and if it fails,
2. Pootle tries to authenticate the user using the users.prefs file
and if it succeeds
3. A new user is added to the database with the same login data
4. Next time this same user tries to login he will be authenticated
using the database
Regards,
Flávio Martins
Index: settings.py
===================================================================
--- settings.py (revisão 10848)
+++ settings.py (cópia de trabalho)
@@ -141,6 +145,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 +154,10 @@
#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',)
+
# LDAP Setup
# The LDAP server. Format: protocol://hostname:port
AUTH_LDAP_SERVER = ''
Index: auth/prefs_backend.py
===================================================================
--- auth/prefs_backend.py (revisão 0)
+++ auth/prefs_backend.py (revisão 0)
@@ -0,0 +1,57 @@
+import logging
+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 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()
+ 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