middleware.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package middleware
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "github.com/Unknwon/macaron"
  7. "github.com/macaron-contrib/session"
  8. "github.com/torkelo/grafana-pro/pkg/bus"
  9. "github.com/torkelo/grafana-pro/pkg/log"
  10. m "github.com/torkelo/grafana-pro/pkg/models"
  11. "github.com/torkelo/grafana-pro/pkg/setting"
  12. )
  13. type Context struct {
  14. *macaron.Context
  15. Session session.Store
  16. IsSignedIn bool
  17. IsAdmin bool
  18. Account *m.Account
  19. UserAccount *m.Account
  20. }
  21. func (c *Context) GetAccountId() int64 {
  22. return c.Account.Id
  23. }
  24. func GetContextHandler() macaron.Handler {
  25. return func(c *macaron.Context, sess session.Store) {
  26. ctx := &Context{
  27. Context: c,
  28. Session: sess,
  29. }
  30. // try get account id from request
  31. if accountId, err := getRequestAccountId(ctx); err == nil {
  32. // fetch user
  33. userQuery := m.GetAccountByIdQuery{Id: accountId}
  34. if err := bus.Dispatch(&userQuery); err != nil {
  35. log.Error(3, "Failed to get user by id, %v, %v", accountId, err)
  36. } else {
  37. // fetch using account
  38. ctx.UserAccount = userQuery.Result
  39. usingQuery := m.GetAccountByIdQuery{Id: ctx.UserAccount.UsingAccountId}
  40. if err := bus.Dispatch(&usingQuery); err != nil {
  41. log.Error(3, "Faild to get account's using account, account: %v, usingAccountId: %v, err:%v", accountId, ctx.UserAccount.Id, err)
  42. } else {
  43. ctx.Account = usingQuery.Result
  44. }
  45. }
  46. }
  47. if ctx.Account != nil {
  48. ctx.IsSignedIn = true
  49. ctx.IsAdmin = ctx.Account.IsAdmin
  50. }
  51. c.Map(ctx)
  52. }
  53. }
  54. // Handle handles and logs error by given status.
  55. func (ctx *Context) Handle(status int, title string, err error) {
  56. if err != nil {
  57. log.Error(4, "%s: %v", title, err)
  58. if macaron.Env != macaron.PROD {
  59. ctx.Data["ErrorMsg"] = err
  60. }
  61. }
  62. switch status {
  63. case 404:
  64. ctx.Data["Title"] = "Page Not Found"
  65. case 500:
  66. ctx.Data["Title"] = "Internal Server Error"
  67. }
  68. ctx.HTML(status, strconv.Itoa(status))
  69. }
  70. func (ctx *Context) JsonOK(message string) {
  71. resp := make(map[string]interface{})
  72. resp["message"] = message
  73. ctx.JSON(200, resp)
  74. }
  75. func (ctx *Context) IsApiRequest() bool {
  76. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  77. }
  78. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  79. resp := make(map[string]interface{})
  80. if err != nil {
  81. log.Error(4, "%s: %v", message, err)
  82. if setting.Env != setting.PROD {
  83. resp["error"] = err.Error()
  84. }
  85. }
  86. switch status {
  87. case 404:
  88. resp["message"] = "Not Found"
  89. case 500:
  90. resp["message"] = "Internal Server Error"
  91. }
  92. if message != "" {
  93. resp["message"] = message
  94. }
  95. ctx.JSON(status, resp)
  96. }
  97. func (ctx *Context) JsonBody(model interface{}) bool {
  98. b, _ := ctx.Req.Body().Bytes()
  99. err := json.Unmarshal(b, &model)
  100. return err == nil
  101. }