change_passwd.go 1.3 KB

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