perf: 🐛 用户登录后,每次都会变化密码加密值

This commit is contained in:
李强
2023-05-28 13:28:36 +08:00
parent 31f20a766f
commit 8c0a04ce61
2 changed files with 18 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
import hashlib
from django.contrib.auth.hashers import make_password
from django.contrib.auth.hashers import make_password, check_password
from django_restql.fields import DynamicSerializerMethodField
from rest_framework import serializers
from rest_framework.decorators import action, permission_classes
@@ -324,11 +324,10 @@ class UserViewSet(CustomModelViewSet):
return ErrorResponse(msg="参数不能为空")
if new_pwd != new_pwd2:
return ErrorResponse(msg="两次密码不匹配")
check_password = request.user.check_password(old_pwd)
if not check_password:
check_password = request.user.check_password(hashlib.md5(old_pwd.encode(encoding='UTF-8')).hexdigest())
if check_password:
new_pwd = hashlib.md5(new_pwd.encode(encoding='UTF-8')).hexdigest()
verify_password = check_password(old_pwd, self.request.user.password)
if not verify_password:
verify_password = check_password(hashlib.md5(old_pwd.encode(encoding='UTF-8')).hexdigest(), self.request.user.password)
if verify_password:
request.user.password = make_password(new_pwd)
request.user.save()
return DetailResponse(data=None, msg="修改成功")