module_users_act_modify.go 3.3 KB

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