middleware.go 3.2 KB

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