index.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package api
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/plugins"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
  13. settings, err := getFrontendSettingsMap(c)
  14. if err != nil {
  15. return nil, err
  16. }
  17. prefsQuery := m.GetPreferencesWithDefaultsQuery{OrgId: c.OrgId, UserId: c.UserId}
  18. if err := bus.Dispatch(&prefsQuery); err != nil {
  19. return nil, err
  20. }
  21. prefs := prefsQuery.Result
  22. // Read locale from acccept-language
  23. acceptLang := c.Req.Header.Get("Accept-Language")
  24. locale := "en-US"
  25. if len(acceptLang) > 0 {
  26. parts := strings.Split(acceptLang, ",")
  27. locale = parts[0]
  28. }
  29. appUrl := setting.AppUrl
  30. appSubUrl := setting.AppSubUrl
  31. // special case when doing localhost call from phantomjs
  32. if c.IsRenderCall {
  33. appUrl = fmt.Sprintf("%s://localhost:%s", setting.Protocol, setting.HttpPort)
  34. appSubUrl = ""
  35. settings["appSubUrl"] = ""
  36. }
  37. var data = dtos.IndexViewData{
  38. User: &dtos.CurrentUser{
  39. Id: c.UserId,
  40. IsSignedIn: c.IsSignedIn,
  41. Login: c.Login,
  42. Email: c.Email,
  43. Name: c.Name,
  44. OrgId: c.OrgId,
  45. OrgName: c.OrgName,
  46. OrgRole: c.OrgRole,
  47. GravatarUrl: dtos.GetGravatarUrl(c.Email),
  48. IsGrafanaAdmin: c.IsGrafanaAdmin,
  49. LightTheme: prefs.Theme == "light",
  50. Timezone: prefs.Timezone,
  51. Locale: locale,
  52. HelpFlags1: c.HelpFlags1,
  53. },
  54. Settings: settings,
  55. AppUrl: appUrl,
  56. AppSubUrl: appSubUrl,
  57. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  58. GoogleTagManagerId: setting.GoogleTagManagerId,
  59. BuildVersion: setting.BuildVersion,
  60. BuildCommit: setting.BuildCommit,
  61. NewGrafanaVersion: plugins.GrafanaLatestVersion,
  62. NewGrafanaVersionExists: plugins.GrafanaHasUpdate,
  63. }
  64. if setting.DisableGravatar {
  65. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png"
  66. }
  67. if len(data.User.Name) == 0 {
  68. data.User.Name = data.User.Login
  69. }
  70. themeUrlParam := c.Query("theme")
  71. if themeUrlParam == "light" {
  72. data.User.LightTheme = true
  73. }
  74. dashboardChildNavs := []*dtos.NavLink{
  75. {Text: "Home", Url: setting.AppSubUrl + "/"},
  76. {Text: "Playlists", Url: setting.AppSubUrl + "/playlists"},
  77. {Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots"},
  78. }
  79. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  80. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Divider: true})
  81. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "New", Icon: "fa fa-plus", Url: setting.AppSubUrl + "/dashboard/new"})
  82. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "Import", Icon: "fa fa-download", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"})
  83. }
  84. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  85. Text: "Dashboards",
  86. Icon: "icon-gf icon-gf-dashboard",
  87. Url: setting.AppSubUrl + "/",
  88. Children: dashboardChildNavs,
  89. })
  90. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  91. alertChildNavs := []*dtos.NavLink{
  92. {Text: "Alert List", Url: setting.AppSubUrl + "/alerting/list"},
  93. {Text: "Notification channels", Url: setting.AppSubUrl + "/alerting/notifications"},
  94. }
  95. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  96. Text: "Alerting",
  97. Icon: "icon-gf icon-gf-alert",
  98. Url: setting.AppSubUrl + "/alerting/list",
  99. Children: alertChildNavs,
  100. })
  101. }
  102. if c.OrgRole == m.ROLE_ADMIN {
  103. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  104. Text: "Data Sources",
  105. Icon: "icon-gf icon-gf-datasources",
  106. Url: setting.AppSubUrl + "/datasources",
  107. })
  108. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  109. Text: "Plugins",
  110. Icon: "icon-gf icon-gf-apps",
  111. Url: setting.AppSubUrl + "/plugins",
  112. })
  113. }
  114. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  115. if err != nil {
  116. return nil, err
  117. }
  118. for _, plugin := range enabledPlugins.Apps {
  119. if plugin.Pinned {
  120. appLink := &dtos.NavLink{
  121. Text: plugin.Name,
  122. Url: plugin.DefaultNavUrl,
  123. Img: plugin.Info.Logos.Small,
  124. }
  125. for _, include := range plugin.Includes {
  126. if !c.HasUserRole(include.Role) {
  127. continue
  128. }
  129. if include.Type == "page" && include.AddToNav {
  130. link := &dtos.NavLink{
  131. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  132. Text: include.Name,
  133. }
  134. appLink.Children = append(appLink.Children, link)
  135. }
  136. if include.Type == "dashboard" && include.AddToNav {
  137. link := &dtos.NavLink{
  138. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  139. Text: include.Name,
  140. }
  141. appLink.Children = append(appLink.Children, link)
  142. }
  143. }
  144. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  145. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  146. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  147. }
  148. if len(appLink.Children) > 0 {
  149. data.MainNavLinks = append(data.MainNavLinks, appLink)
  150. }
  151. }
  152. }
  153. if c.IsGrafanaAdmin {
  154. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  155. Text: "Admin",
  156. Icon: "fa fa-fw fa-cogs",
  157. Url: setting.AppSubUrl + "/admin",
  158. Children: []*dtos.NavLink{
  159. {Text: "Global Users", Url: setting.AppSubUrl + "/admin/users"},
  160. {Text: "Global Orgs", Url: setting.AppSubUrl + "/admin/orgs"},
  161. {Text: "Server Settings", Url: setting.AppSubUrl + "/admin/settings"},
  162. {Text: "Server Stats", Url: setting.AppSubUrl + "/admin/stats"},
  163. },
  164. })
  165. }
  166. return &data, nil
  167. }
  168. func Index(c *middleware.Context) {
  169. if data, err := setIndexViewData(c); err != nil {
  170. c.Handle(500, "Failed to get settings", err)
  171. return
  172. } else {
  173. c.HTML(200, "index", data)
  174. }
  175. }
  176. func NotFoundHandler(c *middleware.Context) {
  177. if c.IsApiRequest() {
  178. c.JsonApiErr(404, "Not found", nil)
  179. return
  180. }
  181. if data, err := setIndexViewData(c); err != nil {
  182. c.Handle(500, "Failed to get settings", err)
  183. return
  184. } else {
  185. c.HTML(404, "index", data)
  186. }
  187. }