1 from django.conf import settings
2 from django.contrib.auth.models import User, check_password
7 This auth backend allows django to authenticate against an external phpbb
8 database. If authentication is successful, the corresponding User from the
9 normal django database is returned (linked on the username field). If no such
10 User exists, it is created automatically.
12 This class uses the following variables from you django settings:
16 PHPBB_DATABASE_PASSWORD
19 If any of these settings are missing, the corresponding setting from Django's
20 own database settings are used. This means, that, usually, you only have to
21 specify the database name where phpbb lives.
25 self.hash = tools.phpass.PasswordHash()
28 host = getattr(settings, 'PHPBB_DATABASE_HOST', settings.DATABASE_HOST)
29 port = getattr(settings, 'PHPBB_DATABASE_PORT', settings.DATABASE_PORT)
30 user = getattr(settings, 'PHPBB_DATABASE_USER', settings.DATABASE_USER)
31 password = getattr(settings, 'PHPBB_DATABASE_PASSWORD', settings.DATABASE_PASSWORD)
32 name = getattr(settings, 'PHPBB_DATABASE_NAME', settings.DATABASE_NAME)
34 # This code was shamelessly stolen from
35 # django.db.backends.mysql.base.cursor
37 #'conv': django_conversions,
46 kwargs['passwd'] = password
47 if host.startswith('/'):
48 kwargs['unix_socket'] = host
52 kwargs['port'] = int(port)
54 conn = MySQLdb.connect (**kwargs)
59 def check_login(self, username, password):
61 prefix = getattr(settings, 'PHPBB_TABLE_PREFIX', '')
64 cursor = conn.cursor ()
65 cursor.execute ("SELECT user_password,user_email FROM %susers WHERE username=%%s" % prefix, username)
68 if (cursor.rowcount == 0):
73 row = cursor.fetchone()
76 if (self.hash.check_password(password, row[0])):
83 Authenticate against a PhpBB database.
85 Most of this code has been taken from Django's user auth tutorial.
87 def authenticate(self, username=None, password=None):
88 email = self.check_login(username, password)
91 user = User.objects.get(username=username)
92 except User.DoesNotExist:
93 # Create a new user. Note that we can set password
94 # to anything, because it won't be checked; the password
95 # from settings.py will.
96 user = User(username=username, password='get from settings.py')
98 user.set_unusable_password()
104 def get_user(self, user_id):
106 return User.objects.get(pk=user_id)
107 except User.DoesNotExist:
110 # vim: set sts=4 sw=4 expandtab: