module_users_act_delete.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package modules
  2. import (
  3. "golang-fave/engine/wrapper"
  4. "golang-fave/utils"
  5. )
  6. func (this *Modules) RegisterAction_UsersDelete() *Action {
  7. return this.newAction(AInfo{
  8. WantDB: true,
  9. Mount: "users-delete",
  10. WantAdmin: true,
  11. }, func(wrap *wrapper.Wrapper) {
  12. pf_id := wrap.R.FormValue("id")
  13. if !utils.IsNumeric(pf_id) {
  14. wrap.MsgError(`Inner system error`)
  15. return
  16. }
  17. err := wrap.DB.Transaction(func(tx *wrapper.Tx) error {
  18. // Block rows
  19. if _, err := tx.Exec("SELECT id FROM blog_cats WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec("SELECT id FROM blog_posts WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec("SELECT id FROM pages WHERE user = ? FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  26. return err
  27. }
  28. if _, err := tx.Exec("SELECT id FROM users WHERE id = ? and id > 1 FOR UPDATE;", utils.StrToInt(pf_id)); err != nil {
  29. return err
  30. }
  31. // Process
  32. if _, err := tx.Exec("UPDATE blog_cats SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  33. return err
  34. }
  35. if _, err := tx.Exec("UPDATE blog_posts SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  36. return err
  37. }
  38. if _, err := tx.Exec("UPDATE pages SET user = 1 WHERE user = ?;", utils.StrToInt(pf_id)); err != nil {
  39. return err
  40. }
  41. if _, err := tx.Exec("DELETE FROM users WHERE id = ? and id > 1;", utils.StrToInt(pf_id)); err != nil {
  42. return err
  43. }
  44. return nil
  45. })
  46. if err != nil {
  47. wrap.MsgError(err.Error())
  48. return
  49. }
  50. wrap.ResetCacheBlocks()
  51. // Reload current page
  52. wrap.Write(`window.location.reload(false);`)
  53. })
  54. }