diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index cb071cde..d2959e5a 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -150,7 +150,7 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa Password: password, } - _, _, err = l.users.UpdateUser(c, userNew, false) + err = l.users.UpdateUserPassword(c, userNew) if err != nil { log.CliErrorf(c, "[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error()) diff --git a/pkg/services/users.go b/pkg/services/users.go index afb753d8..1a455a18 100644 --- a/pkg/services/users.go +++ b/pkg/services/users.go @@ -379,6 +379,32 @@ func (s *UserService) UpdateUser(c core.Context, user *models.User, modifyUserLa return keyProfileUpdated, emailSetToUnverified, nil } +// UpdateUserPassword updates the password of specified user +func (s *UserService) UpdateUserPassword(c core.Context, user *models.User) error { + if user.Uid <= 0 { + return errs.ErrUserIdInvalid + } + + if user.Password == "" { + return errs.ErrPasswordIsEmpty + } + + user.Password = utils.EncodePassword(user.Password, user.Salt) + user.UpdatedUnixTime = time.Now().Unix() + + return s.UserDB().DoTransaction(c, func(sess *xorm.Session) error { + updatedRows, err := sess.ID(user.Uid).Cols("password", "updated_unix_time").Where("deleted=?", false).Update(user) + + if err != nil { + return err + } else if updatedRows < 1 { + return errs.ErrUserNotFound + } + + return nil + }) +} + // UpdateUserAvatar updates the custom avatar type of specified user func (s *UserService) UpdateUserAvatar(c core.Context, uid int64, avatarFile multipart.File, fileExtension string, oldFileExtension string) error { if uid <= 0 {