2018-01-31 21:05:24 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-02-05 00:29:16 +08:00
|
|
|
"fmt"
|
2018-01-31 21:05:24 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-01-27 15:50:36 +08:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2018-03-14 22:27:18 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-31 21:05:24 +08:00
|
|
|
)
|
|
|
|
|
2020-06-17 18:51:41 +08:00
|
|
|
// In Grafana v7.0 we changed panel edit & view query parameters.
|
|
|
|
// This middleware tries to detect those old url parameters and direct to the new url query params
|
2023-01-27 15:50:36 +08:00
|
|
|
func RedirectFromLegacyPanelEditURL(cfg *setting.Cfg) func(c *contextmodel.ReqContext) {
|
|
|
|
return func(c *contextmodel.ReqContext) {
|
2020-06-17 18:51:41 +08:00
|
|
|
queryParams := c.Req.URL.Query()
|
|
|
|
|
2020-12-16 02:09:04 +08:00
|
|
|
panelID, hasPanelID := queryParams["panelId"]
|
2020-06-17 18:51:41 +08:00
|
|
|
_, hasFullscreen := queryParams["fullscreen"]
|
|
|
|
_, hasEdit := queryParams["edit"]
|
|
|
|
|
2020-12-16 02:09:04 +08:00
|
|
|
if hasPanelID && hasFullscreen {
|
2020-06-17 18:51:41 +08:00
|
|
|
delete(queryParams, "panelId")
|
|
|
|
delete(queryParams, "fullscreen")
|
|
|
|
delete(queryParams, "edit")
|
|
|
|
|
|
|
|
if hasEdit {
|
2020-12-16 02:09:04 +08:00
|
|
|
queryParams["editPanel"] = panelID
|
2020-06-17 18:51:41 +08:00
|
|
|
} else {
|
2020-12-16 02:09:04 +08:00
|
|
|
queryParams["viewPanel"] = panelID
|
2020-06-17 18:51:41 +08:00
|
|
|
}
|
|
|
|
|
2020-12-16 02:09:04 +08:00
|
|
|
newURL := fmt.Sprintf("%s%s?%s", cfg.AppURL, strings.TrimPrefix(c.Req.URL.Path, "/"), queryParams.Encode())
|
2020-06-17 18:51:41 +08:00
|
|
|
c.Redirect(newURL, 301)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|