index.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  75. data.NavTree = append(data.NavTree, &dtos.NavLink{
  76. Text: "New",
  77. Icon: "fa fa-fw fa-plus",
  78. Url: "",
  79. Children: []*dtos.NavLink{
  80. {Text: "Dashboard", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new"},
  81. {Text: "Folder", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new/?editview=new-folder"},
  82. {Text: "Import", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"},
  83. },
  84. })
  85. }
  86. dashboardChildNavs := []*dtos.NavLink{
  87. {Text: "Home", Url: setting.AppSubUrl + "/", Icon: "fa fa-fw fa-home"},
  88. {Text: "Playlists", Url: setting.AppSubUrl + "/playlists", Icon: "fa fa-fw fa-film"},
  89. {Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "icon-gf icon-gf-snapshot"},
  90. }
  91. data.NavTree = append(data.NavTree, &dtos.NavLink{
  92. Text: "Dashboards",
  93. Id: "dashboards",
  94. Icon: "icon-gf icon-gf-dashboard",
  95. Url: setting.AppSubUrl + "/",
  96. Children: dashboardChildNavs,
  97. })
  98. if c.IsSignedIn {
  99. data.NavTree = append(data.NavTree, &dtos.NavLink{
  100. Text: "Your Profile",
  101. Id: "profile",
  102. Icon: "fa fa-fw fa-user",
  103. Url: setting.AppSubUrl + "/profile",
  104. Children: []*dtos.NavLink{
  105. {Text: "Signout", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self"},
  106. {Text: "Your profile", Url: setting.AppSubUrl + "/profile", Icon: "fa fa-fw fa-sliders"},
  107. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  108. },
  109. })
  110. }
  111. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  112. alertChildNavs := []*dtos.NavLink{
  113. {Text: "Alert List", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "fa fa-fw fa-list-ul"},
  114. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "fa fa-fw fa-bell-o"},
  115. }
  116. data.NavTree = append(data.NavTree, &dtos.NavLink{
  117. Text: "Alerting",
  118. Id: "alerting",
  119. Icon: "icon-gf icon-gf-alert",
  120. Url: setting.AppSubUrl + "/alerting/list",
  121. Children: alertChildNavs,
  122. })
  123. }
  124. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  125. if err != nil {
  126. return nil, err
  127. }
  128. for _, plugin := range enabledPlugins.Apps {
  129. if plugin.Pinned {
  130. appLink := &dtos.NavLink{
  131. Text: plugin.Name,
  132. Id: "plugin-page-" + plugin.Id,
  133. Url: plugin.DefaultNavUrl,
  134. Img: plugin.Info.Logos.Small,
  135. }
  136. for _, include := range plugin.Includes {
  137. if !c.HasUserRole(include.Role) {
  138. continue
  139. }
  140. if include.Type == "page" && include.AddToNav {
  141. link := &dtos.NavLink{
  142. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  143. Text: include.Name,
  144. }
  145. appLink.Children = append(appLink.Children, link)
  146. }
  147. if include.Type == "dashboard" && include.AddToNav {
  148. link := &dtos.NavLink{
  149. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  150. Text: include.Name,
  151. }
  152. appLink.Children = append(appLink.Children, link)
  153. }
  154. }
  155. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  156. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  157. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  158. }
  159. if len(appLink.Children) > 0 {
  160. data.NavTree = append(data.NavTree, appLink)
  161. }
  162. }
  163. }
  164. if c.OrgRole == m.ROLE_ADMIN {
  165. cfgNode := &dtos.NavLink{
  166. Id: "cfg",
  167. Text: "Configuration",
  168. Icon: "fa fa-fw fa-cogs",
  169. Url: setting.AppSubUrl + "/configuration",
  170. Children: []*dtos.NavLink{
  171. {
  172. Text: "Data Sources",
  173. Icon: "icon-gf icon-gf-datasources",
  174. Description: "Add and configure data sources",
  175. Id: "datasources",
  176. Url: setting.AppSubUrl + "/datasources",
  177. Children: []*dtos.NavLink{
  178. {Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-datasources"},
  179. {Text: "New", Url: setting.AppSubUrl + "/datasources", Icon: "fa fa-fw fa-plus"},
  180. },
  181. },
  182. {
  183. Text: "Preferences",
  184. Id: "org",
  185. Description: "Organization preferences",
  186. Icon: "fa fa-fw fa-sliders",
  187. Url: setting.AppSubUrl + "/org",
  188. },
  189. {
  190. Text: "Plugins",
  191. Id: "plugins",
  192. Description: "View and configure plugins",
  193. Icon: "icon-gf icon-gf-apps",
  194. Url: setting.AppSubUrl + "/plugins",
  195. Children: []*dtos.NavLink{
  196. {Text: "Panels", Url: setting.AppSubUrl + "/plugins?type=panel", Icon: "fa fa-fw fa-stop"},
  197. {Text: "Data sources", Url: setting.AppSubUrl + "/plugins?type=datasource", Icon: "icon-gf icon-gf-datasources"},
  198. {Text: "Apps", Url: setting.AppSubUrl + "/plugins?type=app", Icon: "icon-gf icon-gf-apps"},
  199. },
  200. },
  201. {
  202. Text: "User Management",
  203. Id: "users",
  204. Description: "Manage users & user groups",
  205. Icon: "fa fa-fw fa-users",
  206. Url: setting.AppSubUrl + "/org/users",
  207. },
  208. {
  209. Text: "API Keys",
  210. Id: "apikeys",
  211. Description: "Create & manage API keys",
  212. Icon: "fa fa-fw fa-key",
  213. Url: setting.AppSubUrl + "/org/apikeys",
  214. },
  215. },
  216. }
  217. if c.IsGrafanaAdmin {
  218. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  219. Text: "Server Admin",
  220. Id: "admin",
  221. Icon: "fa fa-fw fa-shield",
  222. Url: setting.AppSubUrl + "/admin",
  223. Children: []*dtos.NavLink{
  224. {Text: "Global Users", Url: setting.AppSubUrl + "/admin/users"},
  225. {Text: "Global Orgs", Url: setting.AppSubUrl + "/admin/orgs"},
  226. {Text: "Server Settings", Url: setting.AppSubUrl + "/admin/settings"},
  227. {Text: "Server Stats", Url: setting.AppSubUrl + "/admin/stats"},
  228. },
  229. })
  230. }
  231. data.NavTree = append(data.NavTree, cfgNode)
  232. }
  233. return &data, nil
  234. }
  235. func Index(c *middleware.Context) {
  236. if data, err := setIndexViewData(c); err != nil {
  237. c.Handle(500, "Failed to get settings", err)
  238. return
  239. } else {
  240. c.HTML(200, "index", data)
  241. }
  242. }
  243. func NotFoundHandler(c *middleware.Context) {
  244. if c.IsApiRequest() {
  245. c.JsonApiErr(404, "Not found", nil)
  246. return
  247. }
  248. if data, err := setIndexViewData(c); err != nil {
  249. c.Handle(500, "Failed to get settings", err)
  250. return
  251. } else {
  252. c.HTML(404, "index", data)
  253. }
  254. }