middleware.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 accountId := getRequestAccountId(ctx); accountId != 0 {
  27. query := m.GetSignedInUserQuery{AccountId: accountId}
  28. if err := bus.Dispatch(&query); err != nil {
  29. log.Error(3, "Failed to get user by id, %v, %v", accountId, err)
  30. } else {
  31. ctx.IsSignedIn = true
  32. ctx.SignedInUser = query.Result
  33. }
  34. } else if token := getApiToken(ctx); token != "" {
  35. // Try API Key auth
  36. tokenQuery := m.GetTokenByTokenQuery{Token: token}
  37. if err := bus.Dispatch(&tokenQuery); err != nil {
  38. ctx.JsonApiErr(401, "Invalid token", err)
  39. return
  40. } else {
  41. tokenInfo := tokenQuery.Result
  42. query := m.GetSignedInUserQuery{AccountId: tokenInfo.AccountId}
  43. if err := bus.Dispatch(&query); err != nil {
  44. ctx.JsonApiErr(401, "Invalid token", err)
  45. return
  46. }
  47. ctx.IsSignedIn = true
  48. ctx.SignedInUser = query.Result
  49. // api key role
  50. ctx.UserRole = tokenInfo.Role
  51. ctx.ApiKeyId = tokenInfo.Id
  52. ctx.UsingAccountId = ctx.AccountId
  53. ctx.UsingAccountName = ctx.UserName
  54. }
  55. }
  56. c.Map(ctx)
  57. }
  58. }
  59. // Handle handles and logs error by given status.
  60. func (ctx *Context) Handle(status int, title string, err error) {
  61. if err != nil {
  62. log.Error(4, "%s: %v", title, err)
  63. if macaron.Env != macaron.PROD {
  64. ctx.Data["ErrorMsg"] = err
  65. }
  66. }
  67. switch status {
  68. case 404:
  69. ctx.Data["Title"] = "Page Not Found"
  70. case 500:
  71. ctx.Data["Title"] = "Internal Server Error"
  72. }
  73. ctx.HTML(status, strconv.Itoa(status))
  74. }
  75. func (ctx *Context) JsonOK(message string) {
  76. resp := make(map[string]interface{})
  77. resp["message"] = message
  78. ctx.JSON(200, resp)
  79. }
  80. func (ctx *Context) IsApiRequest() bool {
  81. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  82. }
  83. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  84. resp := make(map[string]interface{})
  85. if err != nil {
  86. log.Error(4, "%s: %v", message, err)
  87. if setting.Env != setting.PROD {
  88. resp["error"] = err.Error()
  89. }
  90. }
  91. switch status {
  92. case 404:
  93. resp["message"] = "Not Found"
  94. case 500:
  95. resp["message"] = "Internal Server Error"
  96. }
  97. if message != "" {
  98. resp["message"] = message
  99. }
  100. ctx.JSON(status, resp)
  101. }
  102. func (ctx *Context) JsonBody(model interface{}) bool {
  103. b, _ := ctx.Req.Body().Bytes()
  104. err := json.Unmarshal(b, &model)
  105. return err == nil
  106. }