middleware.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.SignInUser
  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.SignInUser = 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.SignInUser = query.Result
  49. // api key role
  50. ctx.SignInUser.UserRole = tokenInfo.Role
  51. ctx.SignInUser.UsingAccountId = ctx.SignInUser.AccountId
  52. ctx.SignInUser.UsingAccountName = ctx.SignInUser.UserName
  53. }
  54. }
  55. c.Map(ctx)
  56. }
  57. }
  58. // Handle handles and logs error by given status.
  59. func (ctx *Context) Handle(status int, title string, err error) {
  60. if err != nil {
  61. log.Error(4, "%s: %v", title, err)
  62. if macaron.Env != macaron.PROD {
  63. ctx.Data["ErrorMsg"] = err
  64. }
  65. }
  66. switch status {
  67. case 404:
  68. ctx.Data["Title"] = "Page Not Found"
  69. case 500:
  70. ctx.Data["Title"] = "Internal Server Error"
  71. }
  72. ctx.HTML(status, strconv.Itoa(status))
  73. }
  74. func (ctx *Context) JsonOK(message string) {
  75. resp := make(map[string]interface{})
  76. resp["message"] = message
  77. ctx.JSON(200, resp)
  78. }
  79. func (ctx *Context) IsApiRequest() bool {
  80. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  81. }
  82. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  83. resp := make(map[string]interface{})
  84. if err != nil {
  85. log.Error(4, "%s: %v", message, err)
  86. if setting.Env != setting.PROD {
  87. resp["error"] = err.Error()
  88. }
  89. }
  90. switch status {
  91. case 404:
  92. resp["message"] = "Not Found"
  93. case 500:
  94. resp["message"] = "Internal Server Error"
  95. }
  96. if message != "" {
  97. resp["message"] = message
  98. }
  99. ctx.JSON(status, resp)
  100. }
  101. func (ctx *Context) JsonBody(model interface{}) bool {
  102. b, _ := ctx.Req.Body().Bytes()
  103. err := json.Unmarshal(b, &model)
  104. return err == nil
  105. }