1 from django.conf import settings
2 from django.contrib.auth.models import User, check_password
8 This auth backend allows django to authenticate against an external phpbb
9 database. If authentication is successful, the corresponding User from the
10 normal django database is returned (linked on the username field). If no such
11 User exists, it is created automatically.
13 This class uses the following variables from you django settings:
17 PHPBB_DATABASE_PASSWORD
20 If any of these settings are missing, the corresponding setting from Django's
21 own database settings are used. This means, that, usually, you only have to
22 specify the database name where phpbb lives.
26 host = getattr(settings, 'PHPBB_DATABASE_HOST', settings.DATABASE_HOST)
27 port = getattr(settings, 'PHPBB_DATABASE_PORT', settings.DATABASE_PORT)
28 user = getattr(settings, 'PHPBB_DATABASE_USER', settings.DATABASE_USER)
29 password = getattr(settings, 'PHPBB_DATABASE_PASSWORD', settings.DATABASE_PASSWORD)
30 name = getattr(settings, 'PHPBB_DATABASE_NAME', settings.DATABASE_NAME)
32 # This code was shamelessly stolen from
33 # django.db.backends.mysql.base.cursor
35 #'conv': django_conversions,
44 kwargs['passwd'] = password
45 if host.startswith('/'):
46 kwargs['unix_socket'] = host
50 kwargs['port'] = int(port)
52 conn = MySQLdb.connect (**kwargs)
57 def check_login(self, username, password):
61 cursor = conn.cursor ()
62 cursor.execute ("SELECT user_password,user_email FROM users WHERE username=%s", username)
65 if (cursor.rowcount == 0):
70 row = cursor.fetchone()
73 if (md5.new(password).hexdigest() == row[0]):
80 Authenticate against a PhpBB database.
82 Most of this code has been taken from Django's user auth tutorial.
84 def authenticate(self, username=None, password=None):
85 email = self.check_login(username, password)
88 user = User.objects.get(username=username)
89 except User.DoesNotExist:
90 # Create a new user. Note that we can set password
91 # to anything, because it won't be checked; the password
92 # from settings.py will.
93 user = User(username=username, password='get from settings.py')
95 user.set_unusable_password()
101 def get_user(self, user_id):
103 return User.objects.get(pk=user_id)
104 except User.DoesNotExist:
107 # vim: set sts=4 sw=4 expandtab: