org_redirect.go 836 B

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