2018-01-31 07:05:24 -06:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-02-04 10:29:16 -06:00
|
|
|
"fmt"
|
2018-01-31 07:05:24 -06:00
|
|
|
"strings"
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2018-03-14 09:27:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2018-01-31 07:05:24 -06:00
|
|
|
)
|
|
|
|
|
2020-06-17 05:51:41 -05: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 01:50:36 -06:00
|
|
|
func RedirectFromLegacyPanelEditURL(cfg *setting.Cfg) func(c *contextmodel.ReqContext) {
|
|
|
|
return func(c *contextmodel.ReqContext) {
|
2020-06-17 05:51:41 -05:00
|
|
|
queryParams := c.Req.URL.Query()
|
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
panelID, hasPanelID := queryParams["panelId"]
|
2020-06-17 05:51:41 -05:00
|
|
|
_, hasFullscreen := queryParams["fullscreen"]
|
|
|
|
_, hasEdit := queryParams["edit"]
|
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
if hasPanelID && hasFullscreen {
|
2020-06-17 05:51:41 -05:00
|
|
|
delete(queryParams, "panelId")
|
|
|
|
delete(queryParams, "fullscreen")
|
|
|
|
delete(queryParams, "edit")
|
|
|
|
|
|
|
|
if hasEdit {
|
2020-12-15 12:09:04 -06:00
|
|
|
queryParams["editPanel"] = panelID
|
2020-06-17 05:51:41 -05:00
|
|
|
} else {
|
2020-12-15 12:09:04 -06:00
|
|
|
queryParams["viewPanel"] = panelID
|
2020-06-17 05:51:41 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 12:09:04 -06:00
|
|
|
newURL := fmt.Sprintf("%s%s?%s", cfg.AppURL, strings.TrimPrefix(c.Req.URL.Path, "/"), queryParams.Encode())
|
2020-06-17 05:51:41 -05:00
|
|
|
c.Redirect(newURL, 301)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|