change_passwd.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package user
  2. import (
  3. "context"
  4. "github.com/jaryhe/gopkgs/database"
  5. "github.com/jinzhu/gorm"
  6. "smart-enterprise-management/errors"
  7. dbmodel "smart-enterprise-management/model"
  8. "smart-enterprise-management/pb/v1"
  9. "github.com/jaryhe/gopkgs/logger"
  10. "go.uber.org/zap"
  11. )
  12. func ChangePasswd(ctx context.Context, req *v1.ChangePasswdRequest) (reply *v1.ChangePasswdReply, err error) {
  13. p := &dbmodel.TCompany{}
  14. and := map[string]interface{}{
  15. "id": req.Uid,
  16. }
  17. err = p.Find(database.DB(), and, nil)
  18. if err != nil && err != gorm.ErrRecordNotFound {
  19. logger.Error("ChangePasswd",
  20. zap.String("err", err.Error()))
  21. return nil, errors.DataBaseError
  22. }
  23. if p.Id == 0 {
  24. return nil, errors.UserNotExistError
  25. }
  26. old, err := getMd5Pass(req.Old)
  27. if err != nil {
  28. logger.Error("ChangePasswd",
  29. zap.String("err", err.Error()))
  30. return nil, errors.SystemError
  31. }
  32. if p.Passwd != old {
  33. return nil, errors.UserPassError
  34. }
  35. where := map[string]interface{}{
  36. "id": req.Uid,
  37. }
  38. p.Passwd, err = getMd5Pass(req.New)
  39. if err != nil {
  40. logger.Error("ChangePasswd",
  41. zap.String("err", err.Error()))
  42. return nil, errors.SystemError
  43. }
  44. err = p.Update(database.DB(), where, map[string]interface{}{"passwd": p.Passwd})
  45. if err != nil {
  46. logger.Error("ChangePasswd",
  47. zap.String("err", err.Error()))
  48. return nil, errors.DataBaseError
  49. }
  50. return &v1.ChangePasswdReply{}, nil
  51. }