module_index_act_first_user.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 := utils.Trim(wrap.R.FormValue("first_name"))
  12. pf_last_name := utils.Trim(wrap.R.FormValue("last_name"))
  13. pf_email := utils.Trim(wrap.R.FormValue("email"))
  14. pf_password := utils.Trim(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. wrap.R.Context(),
  31. `SELECT
  32. COUNT(*)
  33. FROM
  34. users
  35. ;`,
  36. ).Scan(
  37. &count,
  38. )
  39. if *wrap.LogCpError(&err) != nil {
  40. wrap.MsgError(err.Error())
  41. return
  42. }
  43. if count > 0 {
  44. wrap.MsgError(`CMS is already configured`)
  45. return
  46. }
  47. _, err = wrap.DB.Exec(
  48. `INSERT INTO users SET
  49. id = 1,
  50. first_name = ?,
  51. last_name = ?,
  52. email = ?,
  53. password = MD5(?),
  54. admin = 1,
  55. active = 1
  56. ;`,
  57. pf_first_name,
  58. pf_last_name,
  59. pf_email,
  60. pf_password,
  61. )
  62. if err != nil {
  63. wrap.MsgError(err.Error())
  64. return
  65. }
  66. wrap.ResetCacheBlocks()
  67. // Reload current page
  68. wrap.Write(`window.location.reload(false);`)
  69. })
  70. }