module_index_act_first_user.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 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. fave_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. wrap.R.Context(),
  49. `INSERT INTO fave_users SET
  50. id = 1,
  51. first_name = ?,
  52. last_name = ?,
  53. email = ?,
  54. password = MD5(?),
  55. admin = 1,
  56. active = 1
  57. ;`,
  58. pf_first_name,
  59. pf_last_name,
  60. pf_email,
  61. pf_password,
  62. )
  63. if err != nil {
  64. wrap.MsgError(err.Error())
  65. return
  66. }
  67. wrap.ResetCacheBlocks()
  68. // Reload current page
  69. wrap.Write(`window.location.reload(false);`)
  70. })
  71. }