module_index_act_first_user.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package modules
  2. import (
  3. "golang-fave/engine/wrapper"
  4. "golang-fave/utils"
  5. )
  6. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  7. return this.newAction(AInfo{
  8. WantDB: true,
  9. Mount: "index-first-user",
  10. }, func(wrap *wrapper.Wrapper) {
  11. pf_first_name := wrap.R.FormValue("first_name")
  12. pf_last_name := wrap.R.FormValue("last_name")
  13. pf_email := wrap.R.FormValue("email")
  14. pf_password := wrap.R.FormValue("password")
  15. if pf_email == "" {
  16. wrap.MsgError(`Please specify user email`)
  17. return
  18. }
  19. if !utils.IsValidEmail(pf_email) {
  20. wrap.MsgError(`Please specify correct user email`)
  21. return
  22. }
  23. if pf_password == "" {
  24. wrap.MsgError(`Please specify user password`)
  25. return
  26. }
  27. // Security, check if still need to run this action
  28. var count int
  29. err := wrap.DB.QueryRow(`
  30. SELECT
  31. COUNT(*)
  32. FROM
  33. users
  34. ;`,
  35. ).Scan(
  36. &count,
  37. )
  38. if err != nil {
  39. wrap.MsgError(err.Error())
  40. return
  41. }
  42. if count > 0 {
  43. wrap.MsgError(`CMS is already configured`)
  44. return
  45. }
  46. _, err = wrap.DB.Exec(
  47. `INSERT INTO users SET
  48. id = 1,
  49. first_name = ?,
  50. last_name = ?,
  51. email = ?,
  52. password = MD5(?),
  53. admin = 1,
  54. active = 1
  55. ;`,
  56. pf_first_name,
  57. pf_last_name,
  58. pf_email,
  59. pf_password,
  60. )
  61. if err != nil {
  62. wrap.MsgError(err.Error())
  63. return
  64. }
  65. // Reload current page
  66. wrap.Write(`window.location.reload(false);`)
  67. })
  68. }