Django:我如何使用md5密码列实现低级别登录,因为我从旧站点移植了我的用户表?

med*_*iev 2 django django-authentication

基本上,我现在有login/urls.py重定向到django.contrib.auth.views.login这似乎迎刃而解.

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

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

Ada*_*dam 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
Run Code Online (Sandbox Code Playgroud)

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