super_user_reset_password.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package user
  4. import (
  5. "context"
  6. "cp-organization-management/errors"
  7. "cp-organization-management/model"
  8. pb_v1 "cp-organization-management/pb/v1"
  9. "cp-organization-management/utils"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/jaryhe/gopkgs/database"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. "github.com/jaryhe/gopkgs/util"
  17. "cp-organization-management/consts"
  18. )
  19. func ManagementSuperUserResetPassword(ctx context.Context, req *pb_v1.ManagementSuperUserResetPasswordRequest) (reply *pb_v1.ManagementSuperUserResetPasswordReply, err error) {
  20. reply = &pb_v1.ManagementSuperUserResetPasswordReply{}
  21. // 捕获各个task中的异常并返回给调用者
  22. defer func() {
  23. if r := recover(); r != nil {
  24. err = fmt.Errorf("%+v", r)
  25. e := &status.Status{}
  26. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  27. logger.Error("err",
  28. zap.String("system_err", err.Error()),
  29. zap.Stack("stacktrace"))
  30. }
  31. }
  32. }()
  33. if req.OrganizationCode == "" || req.Password == "" || req.Id == 0{
  34. return reply, status.Error(10003, "参数错误")
  35. }
  36. dbname := utils.GetDbName(req.OrganizationCode)
  37. p := model.NewRbacUser(dbname)
  38. where := map[string]interface{}{
  39. "id":req.Id,
  40. }
  41. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  42. values := map[string]interface{}{
  43. "password":passwd,
  44. }
  45. rows, err := p.UpdateWithAffectRows(database.DB(), where, values)
  46. if err != nil {
  47. return nil, errors.DataBaseError
  48. }
  49. if rows == 0 {
  50. return nil, errors.ErrRecordNotFound
  51. }
  52. return reply, nil
  53. }