Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Edit profile unborked, please don't bork it anymore :'(

Cette révision appartient à :
akuma06 2017-05-10 20:24:37 +02:00
Parent 0a1bd085ae
révision 45e6fc109f
5 fichiers modifiés avec 18 ajouts et 16 suppressions

Voir le fichier

@ -10,6 +10,7 @@ import (
"github.com/ewhal/nyaa/service/user"
"github.com/ewhal/nyaa/service/user/form"
"github.com/ewhal/nyaa/service/user/permission"
"github.com/ewhal/nyaa/util/log"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/util/modelHelper"
"github.com/gorilla/mux"
@ -141,9 +142,14 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) {
}
if len(err) == 0 {
modelHelper.BindValueForm(&b, r)
if (!userPermission.HasAdmin(currentUser)) {
b.Username = currentUser.Username
}
err = modelHelper.ValidateForm(&b, err)
log.Info("lol")
if len(err) == 0 {
userProfile, _, errorUser = userService.UpdateUser(w, &b, currentUser, id)
log.Infof("xD2")
if errorUser != nil {
err["errors"] = append(err["errors"], errorUser.Error())
}

Voir le fichier

@ -64,13 +64,13 @@ type LoginForm struct {
// UserForm is used when updating a user.
type UserForm struct {
Username string `form:"username" needed:"true" len_min:"3" len_max:"20"`
Email string `form:"email" needed:"true"`
Language string `form:"language" default:"en-us"`
CurrentPassword string `form:"password" len_min:"6" len_max:"25" omit:"true"`
Password string `form:"password" len_min:"6" len_max:"25" equalInput:"ConfirmPassword"`
ConfirmPassword string `form:"password_confirmation" omit:"true"`
Status int `form:"language" default:"0"`
Username string `form:"username" needed:"true" len_min:"3" len_max:"20"`
Email string `form:"email" needed:"true"`
Language string `form:"language" default:"en-us"`
CurrentPassword string `form:"current_password" len_min:"6" len_max:"25" omit:"true"`
Password string `form:"password" len_min:"6" len_max:"25" equalInput:"Confirm_Password"`
Confirm_Password string `form:"password_confirmation" omit:"true"`
Status int `form:"status" default:"0"`
}
// PasswordForm is used when updating a user password.

Voir le fichier

@ -20,7 +20,7 @@ func CurrentOrAdmin(user *model.User, userID uint) bool {
// CurrentUserIdentical check that userID is same as current user's ID.
// TODO: Inline this
func CurrentUserIdentical(user *model.User, userID uint) bool {
return user.ID != userID
return user.ID == userID
}
func GetRole(user *model.User) string {

Voir le fichier

@ -173,7 +173,7 @@ func UpdateUser(w http.ResponseWriter, form *formStruct.UserForm, currentUser *m
if db.ORM.First(&user, id).RecordNotFound() {
return user, http.StatusNotFound, errors.New("user not found")
}
log.Infof("updateUser")
if form.Password != "" {
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(form.CurrentPassword))
if err != nil && !userPermission.HasAdmin(currentUser) {

Voir le fichier

@ -92,7 +92,7 @@ func ValidateForm(form interface{}, errorForm map[string][]string) map[string][]
if tag.Get("needed") != "" && formElem.Field(i).String() == "" {
errorForm[tag.Get("form")] = append(errorForm[tag.Get("form")], fmt.Sprintf("Field needed: %s", inputName))
}
if tag.Get("default") != "" {
if formElem.Field(i).String() == "" && tag.Get("default") != "" {
formElem.Field(i).SetString(tag.Get("default"))
}
case "int":
@ -105,7 +105,7 @@ func ValidateForm(form interface{}, errorForm map[string][]string) map[string][]
if tag.Get("needed") != "" && formElem.Field(i).Int() == 0 {
errorForm[tag.Get("form")] = append(errorForm[tag.Get("form")], fmt.Sprintf("Field needed: %s", inputName))
}
if tag.Get("default") != "" {
if formElem.Field(i).Interface == nil && tag.Get("default") != "" {
defaultValue, _ := strconv.Atoi(tag.Get("default"))
formElem.Field(i).SetInt(int64(defaultValue))
}
@ -119,7 +119,7 @@ func ValidateForm(form interface{}, errorForm map[string][]string) map[string][]
if tag.Get("needed") != "" && formElem.Field(i).Float() == 0 {
errorForm[tag.Get("form")] = append(errorForm[tag.Get("form")], fmt.Sprintf("Field needed: %s", inputName))
}
if tag.Get("default") != "" {
if formElem.Field(i).Interface == nil && tag.Get("default") != "" {
defaultValue, _ := strconv.Atoi(tag.Get("default"))
formElem.Field(i).SetFloat(float64(defaultValue))
}
@ -130,10 +130,6 @@ func ValidateForm(form interface{}, errorForm map[string][]string) map[string][]
errorForm[tag.Get("form")] = append(errorForm[tag.Get("form")], fmt.Sprintf("Wrong value for the input: %s", inputName))
}
}
if tag.Get("default") != "" {
defaultValue, _ := strconv.ParseBool(tag.Get("default"))
formElem.Field(i).SetBool(defaultValue)
}
}
}
return errorForm