Login

SimpleMachines forum authentication backend

Author:
kropp
Posted:
February 1, 2008
Language:
Python
Version:
.96
Tags:
authentication backend smf
Score:
0 (after 0 ratings)

Authentication backend for Simple Machines Forum user database.

Needs one setting in settings.py:

SMF_PREFIX = 'smf'

This is prefix of SMF tables. This shippet assumes that they are in the same database.

There is one more optional setting:

SMF_GROUP = 1

If set, will allow only users from group with given id. May be used to allow access to admin page only for moderators.

 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 4 years, 5 months ago
  2. OracleAuthBackend by nosrednakram 5 years, 6 months ago
  3. Case Insensitive Authentication Backend by ericflo 5 years, 11 months ago
  4. a template tag to invoke a method on an object with a variable by Scanner 7 years, 10 months ago
  5. Active Directory Authentication Backend (with User object updating) by mroose 3 years, 3 months ago

Comments

Please login first before commenting.