action_first_user.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package actions
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var regexpe = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
  7. func (this *Action) Action_first_user() {
  8. if dbe := this.use_database(); dbe != nil {
  9. this.msg_error(dbe.Error())
  10. return
  11. } else {
  12. defer this.db.Close()
  13. }
  14. pf_first_name := this.wrapper.R.FormValue("first_name")
  15. pf_last_name := this.wrapper.R.FormValue("last_name")
  16. pf_email := this.wrapper.R.FormValue("email")
  17. pf_password := this.wrapper.R.FormValue("password")
  18. if pf_email == "" {
  19. this.msg_error(`Please specify user email`)
  20. return
  21. }
  22. if !regexpe.MatchString(pf_email) {
  23. this.msg_error(`Please specify correct user email`)
  24. return
  25. }
  26. if pf_password == "" {
  27. this.msg_error(`Please specify user password`)
  28. return
  29. }
  30. _, err := this.db.Query(
  31. "INSERT INTO `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?);",
  32. pf_first_name, pf_last_name, pf_email, pf_password)
  33. if err != nil {
  34. this.msg_error(err.Error())
  35. return
  36. }
  37. // Reload current page
  38. this.write(fmt.Sprintf(`window.location.reload(false);`))
  39. }