action_usersettings.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package actions
  2. import (
  3. utils "golang-fave/engine/wrapper/utils"
  4. )
  5. func (this *Action) Action_usersettings() {
  6. if err := this.use_database(); err != nil {
  7. this.msg_error(err.Error())
  8. return
  9. } else {
  10. defer this.db.Close()
  11. }
  12. if err := this.load_session_user(); err != nil {
  13. this.msg_error(err.Error())
  14. return
  15. }
  16. pf_first_name := this.wrapper.R.FormValue("first_name")
  17. pf_last_name := this.wrapper.R.FormValue("last_name")
  18. pf_email := this.wrapper.R.FormValue("email")
  19. pf_password := this.wrapper.R.FormValue("password")
  20. if pf_email == "" {
  21. this.msg_error(`Please specify user email`)
  22. return
  23. }
  24. if !utils.EmailIsValid(pf_email) {
  25. this.msg_error(`Please specify correct user email`)
  26. return
  27. }
  28. if pf_password != "" {
  29. // Update with password if set
  30. _, err := this.db.Query(
  31. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?) WHERE `id` = ?;",
  32. pf_first_name, pf_last_name, pf_email, pf_password, this.user.A_id)
  33. if err != nil {
  34. this.msg_error(err.Error())
  35. return
  36. }
  37. } else {
  38. // Update without password if not set
  39. _, err := this.db.Query(
  40. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ? WHERE `id` = ?;",
  41. pf_first_name, pf_last_name, pf_email, this.user.A_id)
  42. if err != nil {
  43. this.msg_error(err.Error())
  44. return
  45. }
  46. }
  47. // Reload current page
  48. this.write(`window.location.reload(false);`)
  49. }