Profile edit page works now
Cette révision appartient à :
Parent
b2e00dce1f
révision
5feec8748b
1 fichiers modifiés avec 13 ajouts et 18 suppressions
|
@ -14,36 +14,31 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateFromRequest updates a user.
|
// UpdateFromRequest updates a user.
|
||||||
func UpdateFromRequest(c *gin.Context, form *userValidator.UserForm, formSet *userValidator.UserSettingsForm, currentUser *models.User, id uint) (user *models.User, status int, err error) {
|
func UpdateFromRequest(c *gin.Context, form *userValidator.UserForm, formSet *userValidator.UserSettingsForm, currentUser *models.User, id uint) (*models.User, int, error) {
|
||||||
if models.ORM.First(&user, id).RecordNotFound() {
|
var user = &models.User{}
|
||||||
err = errors.New("user_not_found")
|
if models.ORM.First(user, id).RecordNotFound() {
|
||||||
status = http.StatusNotFound
|
return user, http.StatusNotFound, errors.New("user_not_found")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !currentUser.IsModerator() { // We don't want users to be able to modify some fields
|
||||||
|
form.Status = user.Status
|
||||||
|
form.Username = user.Username
|
||||||
|
}
|
||||||
if form.Password != "" {
|
if form.Password != "" {
|
||||||
errBcrypt := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(form.CurrentPassword))
|
errBcrypt := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(form.CurrentPassword))
|
||||||
if errBcrypt != nil && !currentUser.IsModerator() {
|
if errBcrypt != nil && !currentUser.IsModerator() {
|
||||||
err = errors.New("incorrect_password")
|
return user, http.StatusInternalServerError, errors.New("incorrect_password")
|
||||||
status = http.StatusInternalServerError
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
newPassword, errBcrypt := bcrypt.GenerateFromPassword([]byte(form.Password), 10)
|
newPassword, errBcrypt := bcrypt.GenerateFromPassword([]byte(form.Password), 10)
|
||||||
if errBcrypt != nil {
|
if errBcrypt != nil {
|
||||||
err = errors.New("error_password_generating")
|
return user, http.StatusInternalServerError, errors.New("error_password_generating")
|
||||||
status = http.StatusInternalServerError
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
form.Password = string(newPassword)
|
form.Password = string(newPassword)
|
||||||
} else { // Then no change of password
|
} else { // Then no change of password
|
||||||
form.Password = user.Password
|
form.Password = user.Password
|
||||||
}
|
}
|
||||||
if !currentUser.IsModerator() { // We don't want users to be able to modify some fields
|
|
||||||
form.Status = user.Status
|
|
||||||
form.Username = user.Username
|
|
||||||
}
|
|
||||||
log.Debugf("form %+v\n", form)
|
log.Debugf("form %+v\n", form)
|
||||||
validator.Bind(&user, form)
|
validator.Bind(user, form)
|
||||||
|
|
||||||
// We set settings here
|
// We set settings here
|
||||||
user.ParseSettings()
|
user.ParseSettings()
|
||||||
|
@ -59,6 +54,6 @@ func UpdateFromRequest(c *gin.Context, form *userValidator.UserForm, formSet *us
|
||||||
user.Settings.Set("followed_email", formSet.FollowedEmail)
|
user.Settings.Set("followed_email", formSet.FollowedEmail)
|
||||||
user.SaveSettings()
|
user.SaveSettings()
|
||||||
|
|
||||||
status, err = user.Update()
|
status, err := user.Update()
|
||||||
return
|
return user, status, err
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket