middleware.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. *m.SignedInUser
  16. Session session.Store
  17. IsSignedIn bool
  18. }
  19. func GetContextHandler() macaron.Handler {
  20. return func(c *macaron.Context, sess session.Store) {
  21. ctx := &Context{
  22. Context: c,
  23. Session: sess,
  24. }
  25. // try get account id from request
  26. if userId := getRequestUserId(ctx); userId != 0 {
  27. query := m.GetSignedInUserQuery{UserId: userId}
  28. if err := bus.Dispatch(&query); err != nil {
  29. log.Error(3, "Failed to get user by id, %v, %v", userId, err)
  30. } else {
  31. ctx.IsSignedIn = true
  32. ctx.SignedInUser = query.Result
  33. }
  34. } else if key := getApiKey(ctx); key != "" {
  35. // Try API Key auth
  36. keyQuery := m.GetApiKeyByKeyQuery{Key: key}
  37. if err := bus.Dispatch(&keyQuery); err != nil {
  38. ctx.JsonApiErr(401, "Invalid API key", err)
  39. return
  40. } else {
  41. keyInfo := keyQuery.Result
  42. ctx.IsSignedIn = true
  43. ctx.SignedInUser = &m.SignedInUser{}
  44. // TODO: fix this
  45. ctx.AccountRole = keyInfo.Role
  46. ctx.ApiKeyId = keyInfo.Id
  47. ctx.AccountId = keyInfo.AccountId
  48. }
  49. }
  50. c.Map(ctx)
  51. }
  52. }
  53. // Handle handles and logs error by given status.
  54. func (ctx *Context) Handle(status int, title string, err error) {
  55. if err != nil {
  56. log.Error(4, "%s: %v", title, err)
  57. if macaron.Env != macaron.PROD {
  58. ctx.Data["ErrorMsg"] = err
  59. }
  60. }
  61. switch status {
  62. case 404:
  63. ctx.Data["Title"] = "Page Not Found"
  64. case 500:
  65. ctx.Data["Title"] = "Internal Server Error"
  66. }
  67. ctx.HTML(status, strconv.Itoa(status))
  68. }
  69. func (ctx *Context) JsonOK(message string) {
  70. resp := make(map[string]interface{})
  71. resp["message"] = message
  72. ctx.JSON(200, resp)
  73. }
  74. func (ctx *Context) IsApiRequest() bool {
  75. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  76. }
  77. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  78. resp := make(map[string]interface{})
  79. if err != nil {
  80. log.Error(4, "%s: %v", message, err)
  81. if setting.Env != setting.PROD {
  82. resp["error"] = err.Error()
  83. }
  84. }
  85. switch status {
  86. case 404:
  87. resp["message"] = "Not Found"
  88. case 500:
  89. resp["message"] = "Internal Server Error"
  90. }
  91. if message != "" {
  92. resp["message"] = message
  93. }
  94. ctx.JSON(status, resp)
  95. }
  96. func (ctx *Context) JsonBody(model interface{}) bool {
  97. b, _ := ctx.Req.Body().Bytes()
  98. err := json.Unmarshal(b, &model)
  99. return err == nil
  100. }