module_index_act_first_user.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package modules
  2. import (
  3. "golang-fave/engine/utils"
  4. "golang-fave/engine/wrapper"
  5. )
  6. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  7. return this.newAction(AInfo{
  8. Mount: "index-first-user",
  9. }, func(wrap *wrapper.Wrapper) {
  10. pf_first_name := utils.Trim(wrap.R.FormValue("first_name"))
  11. pf_last_name := utils.Trim(wrap.R.FormValue("last_name"))
  12. pf_email := utils.Trim(wrap.R.FormValue("email"))
  13. pf_password := utils.Trim(wrap.R.FormValue("password"))
  14. if pf_email == "" {
  15. wrap.MsgError(`Please specify user email`)
  16. return
  17. }
  18. if !utils.IsValidEmail(pf_email) {
  19. wrap.MsgError(`Please specify correct user email`)
  20. return
  21. }
  22. if pf_password == "" {
  23. wrap.MsgError(`Please specify user password`)
  24. return
  25. }
  26. // Security, check if still need to run this action
  27. var count int
  28. err := wrap.DB.QueryRow(
  29. wrap.R.Context(),
  30. `SELECT
  31. COUNT(*)
  32. FROM
  33. fave_users
  34. ;`,
  35. ).Scan(
  36. &count,
  37. )
  38. if *wrap.LogCpError(&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. wrap.R.Context(),
  48. `INSERT INTO fave_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. }