middleware.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.OrgRole = keyInfo.Role
  50. ctx.ApiKeyId = keyInfo.Id
  51. ctx.OrgId = keyInfo.OrgId
  52. }
  53. } else if setting.AnonymousEnabled {
  54. orgQuery := m.GetOrgByNameQuery{Name: setting.AnonymousOrgName}
  55. if err := bus.Dispatch(&orgQuery); err != nil {
  56. if err == m.ErrOrgNotFound {
  57. log.Error(3, "Anonymous access organization name does not exist", nil)
  58. }
  59. } else {
  60. ctx.IsSignedIn = false
  61. ctx.HasAnonymousAccess = true
  62. ctx.SignedInUser = &m.SignedInUser{}
  63. ctx.OrgRole = m.RoleType(setting.AnonymousOrgRole)
  64. ctx.OrgId = orgQuery.Result.Id
  65. ctx.OrgName = orgQuery.Result.Name
  66. }
  67. }
  68. c.Map(ctx)
  69. }
  70. }
  71. // Handle handles and logs error by given status.
  72. func (ctx *Context) Handle(status int, title string, err error) {
  73. if err != nil {
  74. log.Error(4, "%s: %v", title, err)
  75. if setting.Env != setting.PROD {
  76. ctx.Data["ErrorMsg"] = err
  77. }
  78. }
  79. ctx.Data["Title"] = title
  80. ctx.HTML(status, strconv.Itoa(status))
  81. }
  82. func (ctx *Context) JsonOK(message string) {
  83. resp := make(map[string]interface{})
  84. resp["message"] = message
  85. ctx.JSON(200, resp)
  86. }
  87. func (ctx *Context) IsApiRequest() bool {
  88. return strings.HasPrefix(ctx.Req.URL.Path, "/api")
  89. }
  90. func (ctx *Context) JsonApiErr(status int, message string, err error) {
  91. resp := make(map[string]interface{})
  92. if err != nil {
  93. log.Error(4, "%s: %v", message, err)
  94. if setting.Env != setting.PROD {
  95. resp["error"] = err.Error()
  96. }
  97. }
  98. switch status {
  99. case 404:
  100. resp["message"] = "Not Found"
  101. case 500:
  102. resp["message"] = "Internal Server Error"
  103. }
  104. if message != "" {
  105. resp["message"] = message
  106. }
  107. ctx.JSON(status, resp)
  108. }
  109. func (ctx *Context) JsonBody(model interface{}) bool {
  110. b, _ := ctx.Req.Body().Bytes()
  111. err := json.Unmarshal(b, &model)
  112. return err == nil
  113. }