fixed no need to provide old password when initialization.

This commit is contained in:
wknet123 2016-02-24 18:58:28 +08:00
parent fc2fda0efe
commit ea063c13df
2 changed files with 12 additions and 4 deletions

View File

@ -135,9 +135,17 @@ func ToggleUserAdminRole(u models.User) error {
return err
}
func ChangeUserPassword(u models.User, oldPassword string) error {
func ChangeUserPassword(u models.User, oldPassword ...string) error {
o := orm.NewOrm()
_, err := o.Raw(`update user set password=?, salt=? where user_id=? and password = ?`, utils.Encrypt(u.Password, u.Salt), u.Salt, u.UserId, utils.Encrypt(oldPassword, u.Salt)).Exec()
//In some cases, it may no need to check old password, just as Linux change password polies.
var err error
if len(oldPassword) == 0 {
_, err = o.Raw(`update user set password=?, salt=? where user_id=?`, utils.Encrypt(u.Password, u.Salt), u.Salt, u.UserId).Exec()
} else if len(oldPassword) == 1 {
_, err = o.Raw(`update user set password=?, salt=? where user_id=? and password = ?`, utils.Encrypt(u.Password, u.Salt), u.Salt, u.UserId, utils.Encrypt(oldPassword[0], u.Salt)).Exec()
} else {
err = errors.New("Wrong numbers of params.")
}
return err
}

View File

@ -38,7 +38,7 @@ func updateInitPassword(userId int, password string) error {
queryUser := models.User{UserId: userId}
user, err := dao.GetUser(queryUser)
if err != nil {
log.Println("Failed to get user in initial password, userId:", userId)
log.Println("Failed to get user's initial password, userId:", userId)
return err
}
if user == nil {
@ -52,7 +52,7 @@ func updateInitPassword(userId int, password string) error {
}
user.Salt = salt
user.Password = password
err = dao.ChangeUserPassword(*user, password)
err = dao.ChangeUserPassword(*user)
if err != nil {
log.Printf("Failed to update user encrypted password, userId: %d, err: %v", userId, err)
return err