2

基本上,我目前已经重定向到login/并且似乎效果很好。urls.pydjango.contrib.auth.views.login

但是我正在从旧的 mysql/php 站点移植密码,我相信我应该根据http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information创建一个新的模型配置文件-关于用户。这个新模型/表将具有旧的 md5 密码列,我会将用户名移植到主用户表。

如何更改登录方法,以便我首先检查用户auth_user表中是否有密码,如果没有,则 md5 POSTpassword字段并尝试将其与我的新配置文件密码列匹配,如果有,将密码保存在新的auth_user通过 SHA1 加密的表就像管理员是如何做的一样?

4

2 回答 2

5

我将创建一个新视图,该视图执行以下操作:

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

这是未经测试的,因此需要进行一些清理。它还需要在不同点进行错误检查以处理失败情况(此示例假定成功)。

于 2010-03-09T14:30:46.370 回答
2

编写自定义身份验证后端:

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

于 2010-03-09T18:17:03.950 回答