1package middleware
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/grafana/grafana/pkg/models"
8	"github.com/grafana/grafana/pkg/setting"
9)
10
11// In Grafana v7.0 we changed panel edit & view query parameters.
12// This middleware tries to detect those old url parameters and direct to the new url query params
13func RedirectFromLegacyPanelEditURL(cfg *setting.Cfg) func(c *models.ReqContext) {
14	return func(c *models.ReqContext) {
15		queryParams := c.Req.URL.Query()
16
17		panelID, hasPanelID := queryParams["panelId"]
18		_, hasFullscreen := queryParams["fullscreen"]
19		_, hasEdit := queryParams["edit"]
20
21		if hasPanelID && hasFullscreen {
22			delete(queryParams, "panelId")
23			delete(queryParams, "fullscreen")
24			delete(queryParams, "edit")
25
26			if hasEdit {
27				queryParams["editPanel"] = panelID
28			} else {
29				queryParams["viewPanel"] = panelID
30			}
31
32			newURL := fmt.Sprintf("%s%s?%s", cfg.AppURL, strings.TrimPrefix(c.Req.URL.Path, "/"), queryParams.Encode())
33			c.Redirect(newURL, 301)
34		}
35	}
36}
37