changePasswordLogic.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package auth
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "perms-system-server/internal/consts"
  7. "perms-system-server/internal/middleware"
  8. userModel "perms-system-server/internal/model/user"
  9. "perms-system-server/internal/response"
  10. "perms-system-server/internal/svc"
  11. "perms-system-server/internal/types"
  12. "perms-system-server/internal/util"
  13. "github.com/zeromicro/go-zero/core/limit"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. "golang.org/x/crypto/bcrypt"
  16. )
  17. type ChangePasswordLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangePasswordLogic {
  23. return &ChangePasswordLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. // ChangePassword 修改密码。已登录用户验证原密码后设置新密码,同时递增 tokenVersion 使所有已签发令牌失效,强制重新登录。
  30. func (l *ChangePasswordLogic) ChangePassword(req *types.ChangePasswordReq) error {
  31. if msg := util.ValidatePassword(req.NewPassword); msg != "" {
  32. return response.ErrBadRequest(msg)
  33. }
  34. userId := middleware.GetUserId(l.ctx)
  35. if l.svcCtx.TokenOpLimiter != nil {
  36. code, _ := l.svcCtx.TokenOpLimiter.Take(fmt.Sprintf("chpwd:%d", userId))
  37. if code == limit.OverQuota {
  38. return response.ErrTooManyRequests("操作过于频繁,请稍后再试")
  39. }
  40. }
  41. user, err := l.svcCtx.SysUserModel.FindOne(l.ctx, userId)
  42. if err != nil {
  43. return response.ErrNotFound("用户不存在")
  44. }
  45. if user.Status != consts.StatusEnabled {
  46. return response.ErrForbidden("账号已被冻结")
  47. }
  48. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.OldPassword)); err != nil {
  49. logx.WithContext(l.ctx).Infof("change-password old-password mismatch userId=%d", userId)
  50. return response.ErrBadRequest("原密码错误")
  51. }
  52. if req.OldPassword == req.NewPassword {
  53. return response.ErrBadRequest("新密码不能与原密码相同")
  54. }
  55. hashed, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
  56. if err != nil {
  57. return err
  58. }
  59. // 审计 H-R11-1:把上面已经读到的 user.UpdateTime / user.Username 作为乐观锁 expected 透传;
  60. // UpdatePassword 内部不再 FindOne 自对齐,CAS 的 expected 与"外层校验旧密码所依赖的那一份快照"
  61. // 严格绑定——任何并发 UpdatePassword / UpdateProfile / UpdateStatus 都会让 DB 的 updateTime
  62. // 变化,WHERE 不再命中,ErrUpdateConflict 上抛 409,迫使会话刷新后重试。
  63. if err := l.svcCtx.SysUserModel.UpdatePassword(l.ctx, userId, user.Username, string(hashed), consts.MustChangePasswordNo, user.UpdateTime); err != nil {
  64. // 审计 M-R10-4:与 UpdateUserLogic / UpdateRoleLogic / UpdateUserStatusLogic 口径对齐,
  65. // 把乐观锁失败显式映射成 409,避免 raw error 被 rest 框架兜成 500、前端错把"并发冲突"
  66. // 当作系统故障处理,告警看板也不会把这类事件归到 5xx 噪声池。
  67. if errors.Is(err, userModel.ErrUpdateConflict) {
  68. return response.ErrConflict("密码已被其他会话修改,请刷新后重试")
  69. }
  70. return err
  71. }
  72. l.svcCtx.UserDetailsLoader.Clean(l.ctx, userId)
  73. return nil
  74. }