required.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package routes
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strconv"
  8. "github.com/gorilla/mux"
  9. "github.com/toolkits/str"
  10. "github.com/710leo/urlooker/modules/web/g"
  11. "github.com/710leo/urlooker/modules/web/http/cookie"
  12. "github.com/710leo/urlooker/modules/web/http/errors"
  13. "github.com/710leo/urlooker/modules/web/model"
  14. )
  15. func StraRequired(r *http.Request) *model.Strategy {
  16. vars := mux.Vars(r)
  17. id, err := strconv.ParseInt(vars["id"], 10, 64)
  18. errors.MaybePanic(err)
  19. obj, err := model.GetStrategyById(id)
  20. errors.MaybePanic(err)
  21. if obj == nil {
  22. panic(errors.BadRequestError("no such item"))
  23. }
  24. return obj
  25. }
  26. func BindJson(r *http.Request, obj interface{}) error {
  27. if r.Body == nil {
  28. return fmt.Errorf("Empty request body")
  29. }
  30. defer r.Body.Close()
  31. body, _ := ioutil.ReadAll(r.Body)
  32. err := json.Unmarshal(body, obj)
  33. if err != nil {
  34. return fmt.Errorf("unmarshal body %s err:%v", string(body), err)
  35. }
  36. return err
  37. }
  38. func IdcRequired(r *http.Request) string {
  39. vars := mux.Vars(r)
  40. idc := vars["idc"]
  41. if str.HasDangerousCharacters(idc) {
  42. errors.Panic("idc不合法")
  43. }
  44. return idc
  45. }
  46. func LoginRequired(w http.ResponseWriter, r *http.Request) (int64, string) {
  47. userid, username, found := cookie.ReadUser(r)
  48. if !found {
  49. panic(errors.NotLoginError())
  50. }
  51. return userid, username
  52. }
  53. func AdminRequired(id int64, name string) {
  54. user, err := model.GetUserById(id)
  55. if err != nil {
  56. panic(errors.InternalServerError(err.Error()))
  57. }
  58. if user == nil {
  59. panic(errors.NotLoginError())
  60. }
  61. for _, admin := range g.Config.Admins {
  62. if user.Name == admin {
  63. return
  64. }
  65. }
  66. panic(errors.NotLoginError())
  67. return
  68. }
  69. func MeRequired(id int64, name string) *model.User {
  70. user, err := model.GetUserById(id)
  71. if err != nil {
  72. panic(errors.InternalServerError(err.Error()))
  73. }
  74. if user == nil {
  75. panic(errors.NotLoginError())
  76. }
  77. return user
  78. }
  79. func TeamRequired(r *http.Request) *model.Team {
  80. vars := mux.Vars(r)
  81. tid, err := strconv.ParseInt(vars["tid"], 10, 64)
  82. errors.MaybePanic(err)
  83. team, err := model.GetTeamById(tid)
  84. errors.MaybePanic(err)
  85. if team == nil {
  86. panic(errors.BadRequestError("no such team"))
  87. }
  88. return team
  89. }
  90. func UserMustBeMemberOfTeam(uid, tid int64) {
  91. is, err := model.IsMemberOfTeam(uid, tid)
  92. errors.MaybePanic(err)
  93. if is {
  94. return
  95. }
  96. team, err := model.GetTeamById(tid)
  97. errors.MaybePanic(err)
  98. if team != nil && team.Creator == uid {
  99. return
  100. }
  101. panic(errors.BadRequestError("用户不是团队的成员"))
  102. }
  103. func IsAdmin(username string) bool {
  104. for _, admin := range g.Config.Admins {
  105. if username == admin {
  106. return true
  107. }
  108. }
  109. return false
  110. }