MCPcopy Index your code
hub / github.com/dnote/dnote / ProfileUpdate

Method ProfileUpdate

pkg/server/controllers/users.go:513–555  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

511}
512
513func (u *Users) ProfileUpdate(w http.ResponseWriter, r *http.Request) {
514 vd := views.Data{}
515
516 user := context.User(r.Context())
517 if user == nil {
518 handleHTMLError(w, r, app.ErrLoginRequired, "No authenticated user found", u.SettingView, vd)
519 return
520 }
521
522 var form updateProfileForm
523 if err := parseRequestData(r, &form); err != nil {
524 handleHTMLError(w, r, err, "parsing payload", u.SettingView, vd)
525 return
526 }
527
528 password := []byte(form.Password)
529 if err := bcrypt.CompareHashAndPassword([]byte(user.Password.String), password); err != nil {
530 log.WithFields(log.Fields{
531 "user_id": user.ID,
532 }).Warn("invalid email update attempt")
533 handleHTMLError(w, r, app.ErrInvalidPassword, "Wrong password", u.SettingView, vd)
534 return
535 }
536
537 // Validate
538 if len(form.Email) > 60 {
539 handleHTMLError(w, r, app.ErrEmailTooLong, "Email is too long", u.SettingView, vd)
540 return
541 }
542
543 user.Email.String = form.Email
544
545 if err := u.app.DB.Save(&user).Error; err != nil {
546 handleHTMLError(w, r, err, "saving user", u.SettingView, vd)
547 return
548 }
549
550 alert := views.Alert{
551 Level: views.AlertLvlSuccess,
552 Message: "Email change successful",
553 }
554 views.RedirectAlert(w, r, "/", http.StatusFound, alert)
555}
556

Callers

nothing calls this directly

Calls 6

UserFunction · 0.92
WithFieldsFunction · 0.92
RedirectAlertFunction · 0.92
handleHTMLErrorFunction · 0.85
parseRequestDataFunction · 0.85
WarnMethod · 0.80

Tested by

no test coverage detected