added checking affected count in changing password, added ut for changing password with old password.

This commit is contained in:
wknet123 2016-02-24 20:44:46 +08:00
parent 297474c10e
commit be07274e33
4 changed files with 32 additions and 7 deletions

View File

@ -217,7 +217,11 @@ func (rpc *CommonController) ResetPassword() {
if password != "" {
user.Password = password
dao.ResetUserPassword(*user)
err = dao.ResetUserPassword(*user)
if err != nil {
beego.Error("Error occurred in ResetUserPassword:", err)
rpc.CustomAbort(500, "Internal error.")
}
} else {
rpc.CustomAbort(400, "password_is_required")
}

View File

@ -15,6 +15,7 @@
package dao
import (
"database/sql"
"errors"
"github.com/vmware/harbor/models"
@ -137,14 +138,19 @@ func ToggleUserAdminRole(u models.User) error {
func ChangeUserPassword(u models.User, oldPassword ...string) error {
o := orm.NewOrm()
//In some cases, it may no need to check old password, just as Linux change password polies.
var err error
var r sql.Result
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()
//In some cases, it may no need to check old password, just as Linux change password policies.
r, 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()
r, 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 errors.New("Wrong numbers of params.")
}
count, err := r.RowsAffected()
if count == 0 {
return errors.New("No record be changed, change password failed.")
}
return err
}
@ -157,7 +163,7 @@ func ResetUserPassword(u models.User) error {
}
count, err := r.RowsAffected()
if count == 0 {
return errors.New("No record be changed.")
return errors.New("No record be changed, reset password failed.")
}
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's initial password, userId:", userId)
log.Println("Failed to get user, userId:", userId)
return err
}
if user == nil {

View File

@ -300,6 +300,21 @@ func TestChangeUserPassword(t *testing.T) {
}
}
func TestChangeUserPasswordWithOldPassword(t *testing.T) {
err := dao.ChangeUserPassword(models.User{UserId: currentUser.UserId, Password: "NewerHarborTester12345", Salt: currentUser.Salt}, "NewHarborTester12345")
if err != nil {
t.Errorf("Error occurred in ChangeUserPassword: %v", err)
}
loginedUser, err := dao.LoginByDb(models.AuthModel{Principal: currentUser.Username, Password: "NewerHarborTester12345"})
if err != nil {
t.Errorf("Error occurred in LoginByDb: %v", err)
}
if loginedUser.Username != USERNAME {
t.Errorf("The username returned by Login does not match, expected: %s, acutal: %s", USERNAME, loginedUser.Username)
}
}
func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
projects, err := dao.QueryRelevantProjects(currentUser.UserId)
if err != nil {