First off, I'm using Django 1.1.

I've been having problems getting my custom authentication backend to
work, specifically in the "admin" site.

I've been using this page as guide to put together an auth backend
that uses my subclass of the User model to authenticate.

http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/

I will post all code below, but for the moment here is the problem.
I'm trying to log into the django admin site and I'm getting an
exception on "check_password()" that shows me that the admin login
system is using the User.check_password() rather than my subclass'
check_password(). I'm working with an existing database here and I
don't want to change my password hashing scheme if I don't have to.
The thing that's driving me crazy is that the admin site doesn't seem
to be using my User subclass.

Is there something I'm forgetting/missing here?

Thanks for any help anyone has to offer.

Here is all relevant code (if there is something I'm forgetting, do
let me know):

settings.py
------------------------------

...
...
AUTHENTICATION_BACKENDS = (
    'mf_pyweb.auth_backends.EmUserModelBackend' ,
)
CUSTOM_USER_MODEL = 'mfmain.EmUser'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'mf_pyweb.mfmain',
    'mf_pyweb.mfadmin',
)


------------------------------

auth_backends.py
------------------------------

from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model

class EmUserModelBackend(ModelBackend):
    def authenticate(self , username=None , password=None ,
email=None):
        try:
            user = None
            if email or '@' in username:
                user = self.user_class.objects.get(email=email)
            elif username:
                user = self.user_class.objects.get(username=username)
            else:
                return None
            if user.check_password(password):
                return user
        except:
            return None

    def get_user(self , user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except:
            return None

    def get_user(self , user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except:
            return None

    @property
    def user_class(self):
        if not hasattr(self , '_user_class'):
            self._user_class = get_model(
                    *settings.CUSTOM_USER_MODEL.split('.' , 2))
            if not self._user_class:
                raise ImproperlyConfigured('Could not get custom user
model')
        return self._user_class


------------------------------

mfmain/models.py:
------------------------------

...
...
class EmUser(User):
    domain = models.ForeignKey(Domain , db_index=True ,
db_column='dom_id')
    report_freq = models.PositiveIntegerField()
    report_time = models.DateTimeField()
    userdir = models.CharField(max_length=1024)

    objects = UserManager()

    def set_password(self , raw_pass):
        self.password = getPHash(raw_pass)

    def check_password(self , raw_pass):
        salt , hash = self.password.split('$')
        return (getPHash(raw_pass , salt , False) == hash)

    def __str__(self):
        return self.email

    class Meta:
        db_table = 'users'
        ordering = ['email' , 'username' , 'date_joined']
        verbose_name_plural = 'users'
...
...
------------------------------


--
Jay Deiman

\033:wq!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to