Implement an authentication backend that will look up the user in the old
authorMagnus Hagander <magnus@hagander.net>
Wed, 16 Sep 2009 14:43:46 +0000 (16:43 +0200)
committerMagnus Hagander <magnus@hagander.net>
Wed, 16 Sep 2009 14:43:46 +0000 (16:43 +0200)
community login system in case it's not present in the django auth one,
and if necessary create the object in the django one.

pgweb/settings.py
pgweb/util/auth.py [new file with mode: 0644]

index d7c88b84494c7521f54863555643895f25db3396..0bf62a987e109c0f18085ece5bc5f5c38cccd22c 100644 (file)
@@ -80,6 +80,10 @@ LOGIN_URL='/account/login/'
 LOGIN_REDIRECT_URL='/account/'
 LOGOUT_URL='/account/logout/'
 
+AUTHENTICATION_BACKENDS = (
+    'util.auth.AuthBackend',
+)
+
 INSTALLED_APPS = [
     'django.contrib.auth',
     'django.contrib.contenttypes',
diff --git a/pgweb/util/auth.py b/pgweb/util/auth.py
new file mode 100644 (file)
index 0000000..e140ef6
--- /dev/null
@@ -0,0 +1,40 @@
+from django.contrib.auth.models import User
+from django.contrib.auth.backends import ModelBackend
+from django.db import connection
+
+# Special version of the authentication backend, so we can deal with migration
+# of accounts from the old community login system. Once we consider all accounts
+# migrated, we can remove this one and use the default backend.
+class AuthBackend(ModelBackend):
+       def authenticate(self, username=None, password=None):
+               try:
+                       user = User.objects.get(username=username)
+
+                       # If user is found, check the password using the django
+                       # methods alone.
+                       if user.check_password(password):
+                               return user
+
+                       # User found but password wrong --> tell django it is wrong
+                       return None
+               except User.DoesNotExist:
+                       # User does not exist. See if it exists in the old system,
+                       # and if it does, migrate it to the new one.
+                       curs = connection.cursor()
+                       curs.execute('SELECT * FROM community_login_old(%s,%s)', (username, password))
+                       rows = curs.fetchall()
+                       if len(rows) != 1:
+                               # No rows returned, something clearly went wrong
+                               return None
+                       if rows[0][1] == 1:
+                               # Value 1 in field 1 means the login succeeded. In this case,
+                               # create a user in the django system, and migrate all settings
+                               # we can think of.
+                               user = User(username=username, password=password, email=rows[0][3], first_name=rows[0][2])
+                               user.save()
+                               return user
+                       # Any other value in field 1 means login failed, so tell django we did
+                       return None
+
+               return None # Should never get here, but just in case...
+