module_users_act_modify.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/utils"
  5. "golang-fave/engine/wrapper"
  6. )
  7. func (this *Modules) RegisterAction_UsersModify() *Action {
  8. return this.newAction(AInfo{
  9. Mount: "users-modify",
  10. WantAdmin: true,
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_id := utils.Trim(wrap.R.FormValue("id"))
  13. pf_first_name := utils.Trim(wrap.R.FormValue("first_name"))
  14. pf_last_name := utils.Trim(wrap.R.FormValue("last_name"))
  15. pf_email := utils.Trim(wrap.R.FormValue("email"))
  16. pf_password := utils.Trim(wrap.R.FormValue("password"))
  17. pf_admin := utils.Trim(wrap.R.FormValue("admin"))
  18. pf_active := utils.Trim(wrap.R.FormValue("active"))
  19. if pf_admin == "" {
  20. pf_admin = "0"
  21. }
  22. if pf_active == "" {
  23. pf_active = "0"
  24. }
  25. if !utils.IsNumeric(pf_id) {
  26. wrap.MsgError(`Inner system error`)
  27. return
  28. }
  29. if pf_email == "" {
  30. wrap.MsgError(`Please specify user email`)
  31. return
  32. }
  33. if !utils.IsValidEmail(pf_email) {
  34. wrap.MsgError(`Please specify correct user email`)
  35. return
  36. }
  37. // First user always super admin
  38. // Rewrite active and admin status
  39. if pf_id == "1" {
  40. pf_admin = "1"
  41. pf_active = "1"
  42. }
  43. if pf_id == "0" {
  44. // Add new user
  45. if pf_password == "" {
  46. wrap.MsgError(`Please specify user password`)
  47. return
  48. }
  49. var lastID int64 = 0
  50. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  51. res, err := tx.Exec(
  52. ctx,
  53. `INSERT INTO fave_users SET
  54. first_name = ?,
  55. last_name = ?,
  56. email = ?,
  57. password = MD5(?),
  58. admin = ?,
  59. active = ?
  60. ;`,
  61. pf_first_name,
  62. pf_last_name,
  63. pf_email,
  64. pf_password,
  65. pf_admin,
  66. utils.StrToInt(pf_active),
  67. )
  68. if err != nil {
  69. return err
  70. }
  71. // Get inserted post id
  72. lastID, err = res.LastInsertId()
  73. if err != nil {
  74. return err
  75. }
  76. return nil
  77. }); err != nil {
  78. wrap.MsgError(err.Error())
  79. return
  80. }
  81. wrap.ResetCacheBlocks()
  82. wrap.Write(`window.location='/cp/users/modify/` + utils.Int64ToStr(lastID) + `/';`)
  83. } else {
  84. // Update user
  85. if pf_password == "" {
  86. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  87. _, err := tx.Exec(
  88. ctx,
  89. `UPDATE fave_users SET
  90. first_name = ?,
  91. last_name = ?,
  92. email = ?,
  93. admin = ?,
  94. active = ?
  95. WHERE
  96. id = ?
  97. ;`,
  98. pf_first_name,
  99. pf_last_name,
  100. pf_email,
  101. pf_admin,
  102. utils.StrToInt(pf_active),
  103. utils.StrToInt(pf_id),
  104. )
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }); err != nil {
  110. wrap.MsgError(err.Error())
  111. return
  112. }
  113. } else {
  114. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  115. _, err := tx.Exec(
  116. ctx,
  117. `UPDATE fave_users SET
  118. first_name = ?,
  119. last_name = ?,
  120. email = ?,
  121. password = MD5(?)
  122. WHERE
  123. id = ?
  124. ;`,
  125. pf_first_name,
  126. pf_last_name,
  127. pf_email,
  128. pf_password,
  129. utils.StrToInt(pf_id),
  130. )
  131. if err != nil {
  132. return err
  133. }
  134. return nil
  135. }); err != nil {
  136. wrap.MsgError(err.Error())
  137. return
  138. }
  139. }
  140. wrap.ResetCacheBlocks()
  141. wrap.Write(`window.location='/cp/users/modify/` + pf_id + `/';`)
  142. }
  143. })
  144. }