Skip to content

Commit

Permalink
feat(user): reset password by wechat (#351)
Browse files Browse the repository at this point in the history
* feat(user): add reset password by wechat

微信登录者,凭借自身token直接修改密码

* style(user): reformat

* style(user): reformat

* style(test): reformat

* style(test): reformat

* feat(user): add reset password by wechat

微信登录者,根据jscode和token的双重校验,在无需输入旧密码的情况下,修改密码

* test(user): delete useless module

删除微信联动测试
  • Loading branch information
Norton-Lin authored Dec 20, 2023
1 parent b909e0e commit cffbab5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hinghwa-dict-backend/user/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from .view.wechat import WechatLogin, WechatRegister, BindWechat
from .view.wechat import WechatLogin, WechatRegister, BindWechat, WechatManage
from .views import *
from .view.manage import *
from .view.forget import *
Expand All @@ -17,6 +17,7 @@
urlpatterns += [
path("<int:id>", csrf_exempt(Manage.as_view())), # get US0201 put US0301
path("<int:id>/password", csrf_exempt(ManagePassword.as_view())), # put US0302
path("<int:id>/password/reset", csrf_exempt(WechatManage.as_view())), # post US0307
path("<int:id>/email", csrf_exempt(ManageEmail.as_view())), # put US0303
path("<int:id>/points", csrf_exempt(ManagePoints.as_view())),
]
Expand Down
28 changes: 28 additions & 0 deletions hinghwa-dict-backend/user/view/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
NotBoundWechat,
NotFoundException,
)
from utils.exception.types.forbidden import ForbiddenException
from utils.token import generate_token, check_request_user
from user.dto.user_all import user_all


class OpenId:
Expand Down Expand Up @@ -106,3 +108,29 @@ def delete(self, request, id) -> JsonResponse:
user.user_info.wechat = ""
user.user_info.save()
return JsonResponse({}, status=200)


class WechatManage(View):
# US0307 微信更新用户密码
def post(self, request, id) -> JsonResponse:
# 基于token获取的用户
user = check_request_user(request, id)
if user.id != id:
raise ForbiddenException
body = demjson.decode(request.body)
jscode = body["jscode"]
openid = OpenId(jscode).get_openid()
# 基于jscode获取的用户
user_info = UserInfo.objects.filter(wechat__contains=openid)
if user_info[0].user != user:
raise ForbiddenException
password_validator(body["newpassword"])
user.set_password(body["newpassword"])
user.save()
return JsonResponse(
{
"user": user_all(user),
"token": generate_token(user),
},
status=200,
)

0 comments on commit cffbab5

Please sign in to comment.