index.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/plugins"
  7. "github.com/grafana/grafana/pkg/setting"
  8. )
  9. func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
  10. settings, err := getFrontendSettingsMap(c)
  11. if err != nil {
  12. return nil, err
  13. }
  14. var data = dtos.IndexViewData{
  15. User: &dtos.CurrentUser{
  16. Id: c.UserId,
  17. IsSignedIn: c.IsSignedIn,
  18. Login: c.Login,
  19. Email: c.Email,
  20. Name: c.Name,
  21. LightTheme: c.Theme == "light",
  22. OrgId: c.OrgId,
  23. OrgName: c.OrgName,
  24. OrgRole: c.OrgRole,
  25. GravatarUrl: dtos.GetGravatarUrl(c.Email),
  26. IsGrafanaAdmin: c.IsGrafanaAdmin,
  27. },
  28. Settings: settings,
  29. AppUrl: setting.AppUrl,
  30. AppSubUrl: setting.AppSubUrl,
  31. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  32. GoogleTagManagerId: setting.GoogleTagManagerId,
  33. }
  34. if setting.DisableGravatar {
  35. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png"
  36. }
  37. if len(data.User.Name) == 0 {
  38. data.User.Name = data.User.Login
  39. }
  40. themeUrlParam := c.Query("theme")
  41. if themeUrlParam == "light" {
  42. data.User.LightTheme = true
  43. }
  44. dashboardChildNavs := []*dtos.NavLink{
  45. {Text: "Home", Url: setting.AppSubUrl + "/"},
  46. {Text: "Playlists", Url: setting.AppSubUrl + "/playlists"},
  47. {Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots"},
  48. }
  49. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  50. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Divider: true})
  51. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "New", Icon: "fa fa-plus", Url: setting.AppSubUrl + "/dashboard/new"})
  52. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "Import", Icon: "fa fa-download", Url: setting.AppSubUrl + "/import/dashboard"})
  53. }
  54. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  55. Text: "Dashboards",
  56. Icon: "icon-gf icon-gf-dashboard",
  57. Url: setting.AppSubUrl + "/",
  58. Children: dashboardChildNavs,
  59. })
  60. if c.OrgRole == m.ROLE_ADMIN {
  61. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  62. Text: "Data Sources",
  63. Icon: "icon-gf icon-gf-datasources",
  64. Url: setting.AppSubUrl + "/datasources",
  65. })
  66. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  67. Text: "Plugins",
  68. Icon: "icon-gf icon-gf-apps",
  69. Url: setting.AppSubUrl + "/plugins",
  70. })
  71. }
  72. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. for _, plugin := range enabledPlugins.Apps {
  77. if plugin.Pinned {
  78. appLink := &dtos.NavLink{
  79. Text: plugin.Name,
  80. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit",
  81. Img: plugin.Info.Logos.Small,
  82. }
  83. for _, include := range plugin.Includes {
  84. if include.Type == "page" && include.AddToNav {
  85. link := &dtos.NavLink{
  86. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  87. Text: include.Name,
  88. }
  89. if include.DefaultNav {
  90. appLink.Url = link.Url
  91. }
  92. appLink.Children = append(appLink.Children, link)
  93. }
  94. if include.Type == "dashboard" && include.AddToNav {
  95. link := &dtos.NavLink{
  96. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  97. Text: include.Name,
  98. }
  99. if include.DefaultNav {
  100. appLink.Url = link.Url
  101. }
  102. appLink.Children = append(appLink.Children, link)
  103. }
  104. }
  105. if c.OrgRole == m.ROLE_ADMIN {
  106. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  107. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  108. }
  109. data.MainNavLinks = append(data.MainNavLinks, appLink)
  110. }
  111. }
  112. if c.IsGrafanaAdmin {
  113. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  114. Text: "Admin",
  115. Icon: "fa fa-fw fa-cogs",
  116. Url: setting.AppSubUrl + "/admin",
  117. Children: []*dtos.NavLink{
  118. {Text: "Global Users", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/users"},
  119. {Text: "Global Orgs", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/orgs"},
  120. {Text: "Server Settings", Icon: "fa fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/settings"},
  121. {Text: "Server Stats", Icon: "fa-fw fa-cogs", Url: setting.AppSubUrl + "/admin/stats"},
  122. },
  123. })
  124. }
  125. return &data, nil
  126. }
  127. func Index(c *middleware.Context) {
  128. if data, err := setIndexViewData(c); err != nil {
  129. c.Handle(500, "Failed to get settings", err)
  130. return
  131. } else {
  132. c.HTML(200, "index", data)
  133. }
  134. }
  135. func NotFoundHandler(c *middleware.Context) {
  136. if c.IsApiRequest() {
  137. c.JsonApiErr(404, "Not found", nil)
  138. return
  139. }
  140. if data, err := setIndexViewData(c); err != nil {
  141. c.Handle(500, "Failed to get settings", err)
  142. return
  143. } else {
  144. c.HTML(404, "index", data)
  145. }
  146. }