2015-01-29 05:10:34 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-09-23 05:29:53 -05:00
|
|
|
"fmt"
|
2022-04-15 07:01:58 -05:00
|
|
|
"net/http"
|
2016-07-05 10:59:43 -05:00
|
|
|
"strings"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-04-19 04:23:29 -05:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-03-09 10:57:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-01-26 11:44:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2023-01-25 02:14:32 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2022-04-21 08:03:17 -05:00
|
|
|
pref "github.com/grafana/grafana/pkg/services/preference"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-01-29 05:10:34 -06:00
|
|
|
)
|
|
|
|
|
2018-09-22 03:50:00 -05:00
|
|
|
const (
|
|
|
|
// Themes
|
2023-01-30 03:51:51 -06:00
|
|
|
lightName = "light"
|
|
|
|
darkName = "dark"
|
|
|
|
systemName = "system"
|
2018-09-22 03:50:00 -05:00
|
|
|
)
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) editorInAnyFolder(c *contextmodel.ReqContext) bool {
|
2023-01-25 02:14:32 -06:00
|
|
|
hasEditPermissionInFoldersQuery := folder.HasEditPermissionInFoldersQuery{SignedInUser: c.SignedInUser}
|
|
|
|
hasEditPermissionInFoldersQueryResult, err := hs.DashboardService.HasEditPermissionInFolders(c.Req.Context(), &hasEditPermissionInFoldersQuery)
|
|
|
|
if err != nil {
|
2022-04-20 10:49:20 -05:00
|
|
|
return false
|
|
|
|
}
|
2023-01-25 02:14:32 -06:00
|
|
|
return hasEditPermissionInFoldersQueryResult
|
2022-04-20 10:49:20 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) setIndexViewData(c *contextmodel.ReqContext) (*dtos.IndexViewData, error) {
|
2022-03-03 08:05:47 -06:00
|
|
|
hasAccess := ac.HasAccess(hs.AccessControl, c)
|
2022-05-04 09:12:09 -05:00
|
|
|
hasEditPerm := hasAccess(hs.editorInAnyFolder, ac.EvalAny(ac.EvalPermission(dashboards.ActionDashboardsCreate), ac.EvalPermission(dashboards.ActionFoldersCreate)))
|
2020-09-07 15:10:06 -05:00
|
|
|
|
2023-01-31 13:14:15 -06:00
|
|
|
settings, err := hs.getFrontendSettings(c)
|
2020-09-07 15:10:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-31 13:14:15 -06:00
|
|
|
settings.IsPublicDashboardView = c.IsPublicDashboardView
|
2020-09-07 15:10:06 -05:00
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
prefsQuery := pref.GetPreferenceWithDefaultsQuery{UserID: c.UserID, OrgID: c.OrgID, Teams: c.Teams}
|
2022-04-21 08:03:17 -05:00
|
|
|
prefs, err := hs.preferenceService.GetWithDefaults(c.Req.Context(), &prefsQuery)
|
|
|
|
if err != nil {
|
2020-09-07 15:10:06 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-21 04:19:07 -06:00
|
|
|
if hs.Features.IsEnabled(featuremgmt.FlagIndividualCookiePreferences) {
|
|
|
|
if !prefs.Cookies("analytics") {
|
|
|
|
settings.GoogleAnalytics4Id = ""
|
|
|
|
settings.GoogleAnalyticsId = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:18:34 -06:00
|
|
|
// Locale is used for some number and date/time formatting, whereas language is used just for
|
|
|
|
// translating words in the interface
|
2022-06-21 05:12:49 -05:00
|
|
|
acceptLangHeader := c.Req.Header.Get("Accept-Language")
|
2020-09-07 15:10:06 -05:00
|
|
|
locale := "en-US"
|
2022-11-22 06:18:34 -06:00
|
|
|
language := "" // frontend will set the default language
|
2020-09-07 15:10:06 -05:00
|
|
|
|
2022-11-22 06:18:34 -06:00
|
|
|
if hs.Features.IsEnabled(featuremgmt.FlagInternationalization) && prefs.JSONData.Language != "" {
|
|
|
|
language = prefs.JSONData.Language
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(acceptLangHeader) > 0 {
|
2022-06-21 05:12:49 -05:00
|
|
|
parts := strings.Split(acceptLangHeader, ",")
|
2020-09-07 15:10:06 -05:00
|
|
|
locale = parts[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
appURL := setting.AppUrl
|
2021-03-10 05:41:29 -06:00
|
|
|
appSubURL := hs.Cfg.AppSubURL
|
2020-09-07 15:10:06 -05:00
|
|
|
|
|
|
|
// special case when doing localhost call from image renderer
|
|
|
|
if c.IsRenderCall && !hs.Cfg.ServeFromSubPath {
|
2021-03-10 05:41:29 -06:00
|
|
|
appURL = fmt.Sprintf("%s://localhost:%s", hs.Cfg.Protocol, hs.Cfg.HTTPPort)
|
2020-09-07 15:10:06 -05:00
|
|
|
appSubURL = ""
|
2023-01-31 13:14:15 -06:00
|
|
|
settings.AppSubUrl = ""
|
2020-09-07 15:10:06 -05:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:04:48 -05:00
|
|
|
navTree, err := hs.navTreeService.GetNavTree(c, hasEditPerm, prefs)
|
2020-09-07 15:10:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-21 08:53:56 -06:00
|
|
|
weekStart := ""
|
|
|
|
if prefs.WeekStart != nil {
|
|
|
|
weekStart = *prefs.WeekStart
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
data := dtos.IndexViewData{
|
|
|
|
User: &dtos.CurrentUser{
|
2022-08-11 06:28:55 -05:00
|
|
|
Id: c.UserID,
|
2020-09-07 15:10:06 -05:00
|
|
|
IsSignedIn: c.IsSignedIn,
|
|
|
|
Login: c.Login,
|
|
|
|
Email: c.Email,
|
2022-08-11 06:28:55 -05:00
|
|
|
ExternalUserId: c.SignedInUser.ExternalAuthID,
|
2020-09-07 15:10:06 -05:00
|
|
|
Name: c.Name,
|
|
|
|
OrgCount: c.OrgCount,
|
2022-08-11 06:28:55 -05:00
|
|
|
OrgId: c.OrgID,
|
2020-09-07 15:10:06 -05:00
|
|
|
OrgName: c.OrgName,
|
|
|
|
OrgRole: c.OrgRole,
|
|
|
|
GravatarUrl: dtos.GetGravatarUrl(c.Email),
|
|
|
|
IsGrafanaAdmin: c.IsGrafanaAdmin,
|
2023-01-30 03:51:51 -06:00
|
|
|
Theme: prefs.Theme,
|
2020-09-07 15:10:06 -05:00
|
|
|
LightTheme: prefs.Theme == lightName,
|
|
|
|
Timezone: prefs.Timezone,
|
2022-11-21 08:53:56 -06:00
|
|
|
WeekStart: weekStart,
|
2020-09-07 15:10:06 -05:00
|
|
|
Locale: locale,
|
2022-11-22 06:18:34 -06:00
|
|
|
Language: language,
|
2020-09-07 15:10:06 -05:00
|
|
|
HelpFlags1: c.HelpFlags1,
|
|
|
|
HasEditPermissionInFolders: hasEditPerm,
|
2023-03-03 08:39:53 -06:00
|
|
|
Analytics: dtos.AnalyticsSettings{
|
|
|
|
Identifier: c.SignedInUser.Analytics.Identifier,
|
|
|
|
IntercomIdentifier: c.SignedInUser.Analytics.IntercomIdentifier,
|
|
|
|
},
|
2020-09-07 15:10:06 -05:00
|
|
|
},
|
2022-11-09 08:09:19 -06:00
|
|
|
Settings: settings,
|
|
|
|
Theme: prefs.Theme,
|
|
|
|
AppUrl: appURL,
|
|
|
|
AppSubUrl: appSubURL,
|
2023-02-21 04:19:07 -06:00
|
|
|
GoogleAnalyticsId: settings.GoogleAnalyticsId,
|
|
|
|
GoogleAnalytics4Id: settings.GoogleAnalytics4Id,
|
|
|
|
GoogleAnalytics4SendManualPageViews: hs.Cfg.GoogleAnalytics4SendManualPageViews,
|
|
|
|
GoogleTagManagerId: hs.Cfg.GoogleTagManagerID,
|
2022-11-09 08:09:19 -06:00
|
|
|
BuildVersion: setting.BuildVersion,
|
|
|
|
BuildCommit: setting.BuildCommit,
|
|
|
|
NewGrafanaVersion: hs.grafanaUpdateChecker.LatestVersion(),
|
|
|
|
NewGrafanaVersionExists: hs.grafanaUpdateChecker.UpdateAvailable(),
|
|
|
|
AppName: setting.ApplicationName,
|
|
|
|
AppNameBodyClass: "app-grafana",
|
|
|
|
FavIcon: "public/img/fav32.png",
|
|
|
|
AppleTouchIcon: "public/img/apple-touch-icon.png",
|
|
|
|
AppTitle: "Grafana",
|
|
|
|
NavTree: navTree,
|
|
|
|
Sentry: &hs.Cfg.Sentry,
|
|
|
|
Nonce: c.RequestNonce,
|
|
|
|
ContentDeliveryURL: hs.Cfg.GetContentDeliveryURL(hs.License.ContentDeliveryPrefix()),
|
|
|
|
LoadingLogo: "public/img/grafana_icon.svg",
|
2020-09-07 15:10:06 -05:00
|
|
|
}
|
|
|
|
|
2022-04-25 03:42:09 -05:00
|
|
|
if !hs.AccessControl.IsDisabled() {
|
2022-08-24 06:29:17 -05:00
|
|
|
userPermissions, err := hs.accesscontrolService.GetUserPermissions(c.Req.Context(), c.SignedInUser, ac.Options{ReloadCache: false})
|
2021-04-16 08:02:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-19 04:23:29 -05:00
|
|
|
data.User.Permissions = ac.BuildPermissionsMap(userPermissions)
|
2021-04-16 08:02:16 -05:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:10:06 -05:00
|
|
|
if setting.DisableGravatar {
|
2021-03-10 05:41:29 -06:00
|
|
|
data.User.GravatarUrl = hs.Cfg.AppSubURL + "/public/img/user_profile.png"
|
2020-09-07 15:10:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(data.User.Name) == 0 {
|
|
|
|
data.User.Name = data.User.Login
|
|
|
|
}
|
|
|
|
|
|
|
|
themeURLParam := c.Query("theme")
|
2023-01-30 03:51:51 -06:00
|
|
|
if themeURLParam == lightName || themeURLParam == darkName || themeURLParam == systemName {
|
|
|
|
data.User.Theme = themeURLParam
|
|
|
|
data.Theme = themeURLParam
|
2020-09-07 15:10:06 -05:00
|
|
|
}
|
|
|
|
|
2020-01-27 02:24:44 -06:00
|
|
|
hs.HooksService.RunIndexDataHooks(&data, c)
|
2019-11-15 02:28:55 -06:00
|
|
|
|
2023-04-20 05:10:12 -05:00
|
|
|
data.NavTree.ApplyAdminIA()
|
2022-09-28 01:29:35 -05:00
|
|
|
data.NavTree.Sort()
|
2020-09-11 08:17:41 -05:00
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
return &data, nil
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) Index(c *contextmodel.ReqContext) {
|
2018-10-09 10:47:43 -05:00
|
|
|
data, err := hs.setIndexViewData(c)
|
2018-03-22 06:37:35 -05:00
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 500, "Failed to get settings", err)
|
2015-01-29 05:10:34 -06:00
|
|
|
return
|
|
|
|
}
|
2022-04-15 07:01:58 -05:00
|
|
|
c.HTML(http.StatusOK, "index", data)
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) NotFoundHandler(c *contextmodel.ReqContext) {
|
2015-01-29 05:10:34 -06:00
|
|
|
if c.IsApiRequest() {
|
2015-03-22 14:14:00 -05:00
|
|
|
c.JsonApiErr(404, "Not found", nil)
|
2015-01-29 05:10:34 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:47:43 -05:00
|
|
|
data, err := hs.setIndexViewData(c)
|
2018-03-22 06:37:35 -05:00
|
|
|
if err != nil {
|
2020-12-15 12:09:04 -06:00
|
|
|
c.Handle(hs.Cfg, 500, "Failed to get settings", err)
|
2015-01-29 05:10:34 -06:00
|
|
|
return
|
|
|
|
}
|
2018-03-22 06:37:35 -05:00
|
|
|
|
|
|
|
c.HTML(404, "index", data)
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|