SimpleMachines forum authentication backend

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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

More like this

  1. MoinMoin auth backend by yourcelf 2 years, 8 months ago
  2. Support for permissions for anonymous users in django ModelBackend by jb 1 year, 7 months ago
  3. Active Directory Authentication Backend (with User object updating) by mroose 1 year, 7 months ago
  4. Use email addresses for user name for django 1.3 by kidzik 2 years ago
  5. Use email addresses for user name by chris 6 years, 3 months ago

Comments

(Forgotten your password?)