修复BUG:

1.密码重置;
2.个人中心密码修改
This commit is contained in:
猿小天
2024-05-09 11:11:23 +08:00
parent a03b14a32d
commit 0d9ee1f4d4
4 changed files with 28 additions and 6 deletions

View File

@@ -320,15 +320,17 @@ class UserViewSet(CustomModelViewSet):
"""密码修改"""
data = request.data
old_pwd = data.get("oldPassword")
print(old_pwd)
new_pwd = data.get("newPassword")
new_pwd2 = data.get("newPassword2")
if old_pwd is None or new_pwd is None or new_pwd2 is None:
return ErrorResponse(msg="参数不能为空")
if new_pwd != new_pwd2:
return ErrorResponse(msg="两次密码不匹配")
verify_password = check_password(old_pwd, self.request.user.password)
verify_password = check_password(old_pwd, request.user.password)
if not verify_password:
verify_password = check_password(hashlib.md5(old_pwd.encode(encoding='UTF-8')).hexdigest(), self.request.user.password)
old_pwd_md5 = hashlib.md5(old_pwd.encode(encoding='UTF-8')).hexdigest()
verify_password = check_password(str(old_pwd_md5), request.user.password)
if verify_password:
request.user.password = make_password(hashlib.md5(new_pwd.encode(encoding='UTF-8')).hexdigest())
request.user.save()
@@ -337,11 +339,15 @@ class UserViewSet(CustomModelViewSet):
return ErrorResponse(msg="旧密码不正确")
@action(methods=["PUT"], detail=True, permission_classes=[IsAuthenticated])
def reset_to_default_password(self, request, *args, **kwargs):
def reset_to_default_password(self, request,pk):
"""恢复默认密码"""
instance = Users.objects.filter(id=kwargs.get("pk")).first()
if not self.request.user.is_superuser:
return ErrorResponse(msg="只允许超级管理员对其进行密码重置")
instance = Users.objects.filter(id=pk).first()
if instance:
instance.set_password(dispatch.get_system_config_values("base.default_password"))
default_password = dispatch.get_system_config_values("base.default_password")
md5_pwd = hashlib.md5(default_password.encode(encoding='UTF-8')).hexdigest()
instance.password = make_password(md5_pwd)
instance.save()
return DetailResponse(data=None, msg="密码重置成功")
else: