import hashlib from string import lower from django.contrib.auth.models import User from yourproject import settings class SMFBackend: """ Django authentication backend for SimpleMachines Forum user database. (For more information on SMF go to http://simplemachines.org/) Also it fetches e-mail address from SMF user profile and updates Django user profile on every login. """ def authenticate(self, username=None, password=None): valid = False email = None if not (username is None) and not (password is None): hash = hashlib.sha1(lower(username) + password).hexdigest() from django.db import connection cursor = connection.cursor() cursor.execute("select passwd, ID_GROUP, emailAddress from %s_members where memberName = '%s' and is_activated = '1'" % (settings.SMF_PREFIX, username)) row = cursor.fetchone() email = row[2] if row[0] == hash: valid = True if hasattr(settings, 'SMF_GROUP') and (settings.SMF_GROUP != row[1]): valid = False if valid: try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username) user.is_staff = True user.is_superuser = False user.set_unusable_password() # disable login through Model backend user.save() if not email is None: user.email = email return user return None def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None