views:

133

answers:

2

Basically, I currently have login/ in urls.py redirect to the django.contrib.auth.views.login and that seems to work out fine.

However I'm porting over passwords from a legacy mysql/php site and I believe I should just create a new model profile per http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users. This new model/table will have the old md5 password column, and I'll port over the usernames to the main users table.

How could I change the login method such that I first check if the user has a password in the auth_user table, and if not then md5 the POST password field and attempt to match it to my new profile password column, if so save the password in the new auth_user table by SHA1 encrypting like how the admin does it?

+2  A: 

I would create a new view that does something along the following:

from django.contrib.auth.models import User, check_password
import hashlib

def login_with_lookup(request):
    if request.POST: # If you want to restrict to POST
        username = request.POST['username']
        password = request.POST['password']
        user = User.objects.get(username=username)
        profile = user.get_profile()
        if profile.old_password != '' and profile.old_password == hashlib.md5(password).hexdigest():
            user.set_password(password)
            profile.old_password = ''
            user.save() # Might need to save profile as well, not sure how it works
        if check_password(password, user.password):
            login(request, user)
    #Whatever else you want to do, followed by a render to template or redirect

This is un-tested, so it will need a bit of clean-up. It will also need error checking at different points to handle failure cases (this example assumes success).

Adam
A: 

Write a custom authentication backend:

http://docs.djangoproject.com/en/1.1/topics/auth/#writing-an-authentication-backend

Igor Sobreira