reset_password.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "git.getensh.com/common/gopkgs/util"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. "time"
  15. "xingjia-management-gateway/apis"
  16. "xingjia-management-gateway/consts"
  17. "xingjia-management-gateway/errors"
  18. "xingjia-management-gateway/impl/v1/operation_log"
  19. dbmodel "xingjia-management-gateway/model"
  20. )
  21. func checkUserResetPasswordParam(req *apis.UserResetPasswordRequest) error {
  22. switch {
  23. case req.Password == "":
  24. return status.Error(10003, "未输入目标")
  25. case req.SelfId == 0:
  26. return status.Error(10003, "未知操作者")
  27. case req.ResetId == 0:
  28. return status.Error(10003, "目标账户不能为空")
  29. }
  30. return nil
  31. }
  32. //
  33. func UserResetPassword(ctx context.Context, req *apis.UserResetPasswordRequest) (reply *apis.UserResetPasswordReply, err error) {
  34. reply = &apis.UserResetPasswordReply{}
  35. // 捕获各个task中的异常并返回给调用者
  36. defer func() {
  37. if r := recover(); r != nil {
  38. err = fmt.Errorf("%+v", r)
  39. e := &status.Status{}
  40. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  41. logger.Error("err",
  42. zap.String("system_err", err.Error()),
  43. zap.Stack("stacktrace"))
  44. }
  45. }
  46. }()
  47. if err = checkUserResetPasswordParam(req); err != nil {
  48. return nil, err
  49. }
  50. if err = checkAdmin(req.SelfId); err != nil {
  51. return nil, err
  52. }
  53. old := &dbmodel.TUser{}
  54. where := map[string]interface{}{
  55. "id": req.ResetId,
  56. }
  57. err = old.Find(database.DB(), where)
  58. if err != nil && err != gorm.ErrRecordNotFound {
  59. return nil, errors.DataBaseError
  60. }
  61. if old.ID == 0 {
  62. return nil, errors.ErrRecordNotFound
  63. }
  64. values := map[string]interface{}{}
  65. now := time.Now()
  66. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  67. values["password"] = passwd
  68. values["updated_at"] = now
  69. p := dbmodel.TUser{}
  70. db := database.DB().Begin()
  71. defer func() {
  72. if err != nil {
  73. db.Rollback()
  74. }
  75. }()
  76. err = p.Update(db, where, values)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. // 操作日志
  81. content := getUserContent(*old)
  82. logReq := &apis.OperationLogAddRequest{
  83. OriginContent: "",
  84. TargetContent: content,
  85. Uid: req.SelfId,
  86. User: req.SelfName,
  87. Module: consts.OperationModuleAccount,
  88. OperationTime: time.Now().Unix(),
  89. Operation: consts.OperationActionUserResetPassword,
  90. }
  91. err = operation_log.OperationLogAddHandle(logReq, db)
  92. if err != nil {
  93. return nil, err
  94. }
  95. db.Commit()
  96. return reply, nil
  97. }