org_redirect.go 746 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/models"
  6. "gopkg.in/macaron.v1"
  7. )
  8. func OrgRedirect() macaron.Handler {
  9. return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
  10. orgId := c.QueryInt64("org-id")
  11. if orgId == 0 {
  12. return
  13. }
  14. ctx, ok := c.Data["ctx"].(*Context)
  15. if !ok || !ctx.IsSignedIn {
  16. return
  17. }
  18. if orgId == ctx.OrgId {
  19. return
  20. }
  21. cmd := models.SetUsingOrgCommand{UserId: ctx.UserId, OrgId: orgId}
  22. if err := bus.Dispatch(&cmd); err != nil {
  23. if ctx.IsApiRequest() {
  24. ctx.JsonApiErr(404, "Not found", nil)
  25. } else {
  26. ctx.Error(404, "Not found")
  27. }
  28. return
  29. }
  30. c.Redirect(c.Req.URL.String(), 302)
  31. }
  32. }