2015-01-29 05:10:34 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-05-15 11:38:54 -05:00
|
|
|
"context"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
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"
|
2023-04-27 11:20:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
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-05-15 11:38:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/login"
|
2022-04-21 08:03:17 -05:00
|
|
|
pref "github.com/grafana/grafana/pkg/services/preference"
|
2023-05-15 11:38:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-01-29 05:10:34 -06:00
|
|
|
)
|
|
|
|
|
2023-05-24 09:33:35 -05:00
|
|
|
// TODO this will be removed when we remove legacy AC fallback from HasAccess method
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) editorInAnyFolder(c *contextmodel.ReqContext) bool {
|
2023-05-24 09:33:35 -05:00
|
|
|
return false
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-10 08:37:04 -05:00
|
|
|
theme := hs.getThemeForIndexData(prefs.Theme, c.Query("theme"))
|
|
|
|
|
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,
|
|
|
|
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-05-10 08:37:04 -05:00
|
|
|
Theme: theme.ID,
|
|
|
|
LightTheme: theme.Type == "light",
|
2020-09-07 15:10:06 -05:00
|
|
|
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-05-15 11:38:54 -05:00
|
|
|
Analytics: hs.buildUserAnalyticsSettings(c.Req.Context(), c.SignedInUser),
|
2020-09-07 15:10:06 -05:00
|
|
|
},
|
2022-11-09 08:09:19 -06:00
|
|
|
Settings: settings,
|
2023-05-10 08:37:04 -05:00
|
|
|
ThemeType: theme.Type,
|
2022-11-09 08:09:19 -06:00
|
|
|
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,
|
|
|
|
Nonce: c.RequestNonce,
|
|
|
|
ContentDeliveryURL: hs.Cfg.GetContentDeliveryURL(hs.License.ContentDeliveryPrefix()),
|
|
|
|
LoadingLogo: "public/img/grafana_icon.svg",
|
2023-04-27 11:20:37 -05:00
|
|
|
IsDevelopmentEnv: hs.Cfg.Env == setting.Dev,
|
|
|
|
}
|
|
|
|
|
|
|
|
if hs.Cfg.CSPEnabled {
|
|
|
|
data.CSPEnabled = true
|
|
|
|
data.CSPContent = middleware.ReplacePolicyVariables(hs.Cfg.CSPTemplate, appURL, c.RequestNonce)
|
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
|
|
|
|
}
|
|
|
|
|
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-05-15 11:38:54 -05:00
|
|
|
func (hs *HTTPServer) buildUserAnalyticsSettings(ctx context.Context, signedInUser *user.SignedInUser) dtos.AnalyticsSettings {
|
|
|
|
identifier := signedInUser.Email + "@" + setting.AppUrl
|
|
|
|
|
|
|
|
authInfo, err := hs.authInfoService.GetAuthInfo(ctx, &login.GetAuthInfoQuery{UserId: signedInUser.UserID})
|
|
|
|
if err != nil && !errors.Is(err, user.ErrUserNotFound) {
|
|
|
|
hs.log.Error("Failed to get auth info for analytics", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if authInfo != nil && authInfo.AuthModule == login.GrafanaComAuthModule {
|
|
|
|
identifier = authInfo.AuthId
|
|
|
|
}
|
|
|
|
|
|
|
|
return dtos.AnalyticsSettings{
|
|
|
|
Identifier: identifier,
|
|
|
|
IntercomIdentifier: hashUserIdentifier(identifier, hs.Cfg.IntercomSecret),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashUserIdentifier(identifier string, secret string) string {
|
|
|
|
if secret == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
key := []byte(secret)
|
|
|
|
h := hmac.New(sha256.New, key)
|
|
|
|
h.Write([]byte(identifier))
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-05-10 08:37:04 -05:00
|
|
|
|
|
|
|
func (hs *HTTPServer) getThemeForIndexData(themePrefId string, themeURLParam string) *pref.ThemeDTO {
|
|
|
|
if themeURLParam != "" && pref.IsValidThemeID(themeURLParam) {
|
|
|
|
return pref.GetThemeByID(themeURLParam)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pref.IsValidThemeID(themePrefId) {
|
|
|
|
theme := pref.GetThemeByID(themePrefId)
|
|
|
|
if !theme.IsExtra || hs.Features.IsEnabled(featuremgmt.FlagExtraThemes) {
|
|
|
|
return theme
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pref.GetThemeByID(hs.Cfg.DefaultTheme)
|
|
|
|
}
|