Custom user model backend

46 views
Skip to first unread message

Domagoj Kovač

unread,
May 28, 2014, 8:49:01 AM5/28/14
to django...@googlegroups.com
Hi guys,

I extended base user model with certain fields. My code is as follows:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.utils import timezone
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 CustomUserModelBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        try:
            user = self.user_class.objects.get(username=username)
            if user.login_duration is None or user.login_duration >= timezone.now():
                if user.check_password(password):
                    return user
            else:
                return None
        except self.user_class.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except self.user_class.DoesNotExist:
            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

Everything works as it should but if i am logged in system logs me out after certain time even i am using application. Is there something i missed that controls how login persistence is handled?

Best,
Domagoj 

Kelvin Wong

unread,
May 28, 2014, 11:10:11 PM5/28/14
to django...@googlegroups.com
In your settings.py you might have to set the SESSION_COOKIE_AGE


K

Domagoj Kovač

unread,
May 29, 2014, 2:52:58 AM5/29/14
to django...@googlegroups.com
I already know about SESSION_COOKIE_AGE. I set it to be 30 minutes, but the problem is that even if i am doing something system logs me out, and this should not happen. It work properly before i implemented custom auth backend.

Kelvin Wong

unread,
May 29, 2014, 6:25:38 AM5/29/14
to django...@googlegroups.com
Maybe try putting some logging in your CustomUserModelBackend.

Near the top, put in some logging boilerplate:

import logging
logger = logging.getLogger(__name__)

Then in your backend, something like this:

class CustomUserModelBackend(ModelBackend):
    ...
    def get_user(self, user_id):
        try:
            return self.user_class.objects.get(pk=user_id)
        except self.user_class.DoesNotExist:
            logger.info('User.id={}: User not found, attaching AnonymousUser'.format(user_id))
            return None

This backend method is checked by the AuthenticationMiddleware when it attaches the user to the request



When you make a backend like this it is also a good idea to make a test suite for it.

If these are all fruitless, maybe try looking at your session setup. I'm assuming you are running the db backend for the sessions. If you are using memcached or something else, check the usual settings (TIMEOUT, OPTIONS, etc)

K
Reply all
Reply to author
Forward
0 new messages