moto/internal/organize/passport/password.go

23 lines
621 B
Go

package passport
import (
"context"
"fmt"
"git.nobla.cn/golang/moto/common/db"
"git.nobla.cn/golang/moto/models"
)
func ResetPassword(ctx context.Context, userid string, oldPassword, newPassword string) (err error) {
tx := db.WithContext(ctx)
userModel := &models.User{}
if err = tx.Where("uid=?", userid).First(userModel).Error; err != nil {
return
}
if userModel.Password == oldPassword || md5Hash(oldPassword) == userModel.Password {
err = tx.Where("uid=?", userid).Model(&models.User{}).UpdateColumn("password", md5Hash(newPassword)).Error
} else {
err = fmt.Errorf("invalid password")
}
return
}