module_users_act_modify.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package modules
  2. import (
  3. "context"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  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. `INSERT INTO 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. `UPDATE users SET
  89. first_name = ?,
  90. last_name = ?,
  91. email = ?,
  92. admin = ?,
  93. active = ?
  94. WHERE
  95. id = ?
  96. ;`,
  97. pf_first_name,
  98. pf_last_name,
  99. pf_email,
  100. pf_admin,
  101. utils.StrToInt(pf_active),
  102. utils.StrToInt(pf_id),
  103. )
  104. if err != nil {
  105. return err
  106. }
  107. return nil
  108. }); err != nil {
  109. wrap.MsgError(err.Error())
  110. return
  111. }
  112. } else {
  113. if err := wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  114. _, err := tx.Exec(
  115. `UPDATE users SET
  116. first_name = ?,
  117. last_name = ?,
  118. email = ?,
  119. password = MD5(?)
  120. WHERE
  121. id = ?
  122. ;`,
  123. pf_first_name,
  124. pf_last_name,
  125. pf_email,
  126. pf_password,
  127. utils.StrToInt(pf_id),
  128. )
  129. if err != nil {
  130. return err
  131. }
  132. return nil
  133. }); err != nil {
  134. wrap.MsgError(err.Error())
  135. return
  136. }
  137. }
  138. wrap.ResetCacheBlocks()
  139. wrap.Write(`window.location='/cp/users/modify/` + pf_id + `/';`)
  140. }
  141. })
  142. }