index.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/plugins"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. const (
  12. // Themes
  13. lightName = "light"
  14. darkName = "dark"
  15. )
  16. func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, error) {
  17. settings, err := hs.getFrontendSettingsMap(c)
  18. if err != nil {
  19. return nil, err
  20. }
  21. prefsQuery := m.GetPreferencesWithDefaultsQuery{User: c.SignedInUser}
  22. if err := bus.Dispatch(&prefsQuery); err != nil {
  23. return nil, err
  24. }
  25. prefs := prefsQuery.Result
  26. // Read locale from acccept-language
  27. acceptLang := c.Req.Header.Get("Accept-Language")
  28. locale := "en-US"
  29. if len(acceptLang) > 0 {
  30. parts := strings.Split(acceptLang, ",")
  31. locale = parts[0]
  32. }
  33. appURL := setting.AppUrl
  34. appSubURL := setting.AppSubUrl
  35. // special case when doing localhost call from phantomjs
  36. if c.IsRenderCall {
  37. appURL = fmt.Sprintf("%s://localhost:%s", setting.Protocol, setting.HttpPort)
  38. appSubURL = ""
  39. settings["appSubUrl"] = ""
  40. }
  41. hasEditPermissionInFoldersQuery := m.HasEditPermissionInFoldersQuery{SignedInUser: c.SignedInUser}
  42. if err := bus.Dispatch(&hasEditPermissionInFoldersQuery); err != nil {
  43. return nil, err
  44. }
  45. var data = dtos.IndexViewData{
  46. User: &dtos.CurrentUser{
  47. Id: c.UserId,
  48. IsSignedIn: c.IsSignedIn,
  49. Login: c.Login,
  50. Email: c.Email,
  51. Name: c.Name,
  52. OrgCount: c.OrgCount,
  53. OrgId: c.OrgId,
  54. OrgName: c.OrgName,
  55. OrgRole: c.OrgRole,
  56. GravatarUrl: dtos.GetGravatarUrl(c.Email),
  57. IsGrafanaAdmin: c.IsGrafanaAdmin,
  58. LightTheme: prefs.Theme == lightName,
  59. Timezone: prefs.Timezone,
  60. Locale: locale,
  61. HelpFlags1: c.HelpFlags1,
  62. HasEditPermissionInFolders: hasEditPermissionInFoldersQuery.Result,
  63. },
  64. Settings: settings,
  65. Theme: prefs.Theme,
  66. AppUrl: appURL,
  67. AppSubUrl: appSubURL,
  68. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  69. GoogleTagManagerId: setting.GoogleTagManagerId,
  70. BuildVersion: setting.BuildVersion,
  71. BuildCommit: setting.BuildCommit,
  72. NewGrafanaVersion: plugins.GrafanaLatestVersion,
  73. NewGrafanaVersionExists: plugins.GrafanaHasUpdate,
  74. AppName: setting.ApplicationName,
  75. AppNameBodyClass: getAppNameBodyClass(setting.ApplicationName),
  76. }
  77. if setting.DisableGravatar {
  78. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/user_profile.png"
  79. }
  80. if len(data.User.Name) == 0 {
  81. data.User.Name = data.User.Login
  82. }
  83. themeURLParam := c.Query("theme")
  84. if themeURLParam == lightName {
  85. data.User.LightTheme = true
  86. data.Theme = lightName
  87. } else if themeURLParam == darkName {
  88. data.User.LightTheme = false
  89. data.Theme = darkName
  90. }
  91. if hasEditPermissionInFoldersQuery.Result {
  92. children := []*dtos.NavLink{
  93. {Text: "Dashboard", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/dashboard/new"},
  94. }
  95. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  96. children = append(children, &dtos.NavLink{Text: "Folder", SubTitle: "Create a new folder to organize your dashboards", Id: "folder", Icon: "gicon gicon-folder-new", Url: setting.AppSubUrl + "/dashboards/folder/new"})
  97. }
  98. children = append(children, &dtos.NavLink{Text: "Import", SubTitle: "Import dashboard from file or Grafana.com", Id: "import", Icon: "gicon gicon-dashboard-import", Url: setting.AppSubUrl + "/dashboard/import"})
  99. data.NavTree = append(data.NavTree, &dtos.NavLink{
  100. Text: "Create",
  101. Id: "create",
  102. Icon: "fa fa-fw fa-plus",
  103. Url: setting.AppSubUrl + "/dashboard/new",
  104. Children: children,
  105. })
  106. }
  107. dashboardChildNavs := []*dtos.NavLink{
  108. {Text: "Home", Id: "home", Url: setting.AppSubUrl + "/", Icon: "gicon gicon-home", HideFromTabs: true},
  109. {Text: "Divider", Divider: true, Id: "divider", HideFromTabs: true},
  110. {Text: "Manage", Id: "manage-dashboards", Url: setting.AppSubUrl + "/dashboards", Icon: "gicon gicon-manage"},
  111. {Text: "Playlists", Id: "playlists", Url: setting.AppSubUrl + "/playlists", Icon: "gicon gicon-playlists"},
  112. {Text: "Snapshots", Id: "snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "gicon gicon-snapshots"},
  113. }
  114. data.NavTree = append(data.NavTree, &dtos.NavLink{
  115. Text: "Dashboards",
  116. Id: "dashboards",
  117. SubTitle: "Manage dashboards & folders",
  118. Icon: "gicon gicon-dashboard",
  119. Url: setting.AppSubUrl + "/",
  120. Children: dashboardChildNavs,
  121. })
  122. if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  123. data.NavTree = append(data.NavTree, &dtos.NavLink{
  124. Text: "Explore",
  125. Id: "explore",
  126. SubTitle: "Explore your data",
  127. Icon: "fa fa-rocket",
  128. Url: setting.AppSubUrl + "/explore",
  129. Children: []*dtos.NavLink{
  130. {Text: "New tab", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/explore"},
  131. },
  132. })
  133. }
  134. if c.IsSignedIn {
  135. // Only set login if it's different from the name
  136. var login string
  137. if c.SignedInUser.Login != c.SignedInUser.NameOrFallback() {
  138. login = c.SignedInUser.Login
  139. }
  140. profileNode := &dtos.NavLink{
  141. Text: c.SignedInUser.NameOrFallback(),
  142. SubTitle: login,
  143. Id: "profile",
  144. Img: data.User.GravatarUrl,
  145. Url: setting.AppSubUrl + "/profile",
  146. HideFromMenu: true,
  147. Children: []*dtos.NavLink{
  148. {Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "gicon gicon-preferences"},
  149. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  150. },
  151. }
  152. if !setting.DisableSignoutMenu {
  153. // add sign out first
  154. profileNode.Children = append(profileNode.Children, &dtos.NavLink{
  155. Text: "Sign out", Id: "sign-out", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self",
  156. })
  157. }
  158. data.NavTree = append(data.NavTree, profileNode)
  159. }
  160. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  161. alertChildNavs := []*dtos.NavLink{
  162. {Text: "Alert Rules", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "gicon gicon-alert-rules"},
  163. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "gicon gicon-alert-notification-channel"},
  164. }
  165. data.NavTree = append(data.NavTree, &dtos.NavLink{
  166. Text: "Alerting",
  167. SubTitle: "Alert rules & notifications",
  168. Id: "alerting",
  169. Icon: "gicon gicon-alert",
  170. Url: setting.AppSubUrl + "/alerting/list",
  171. Children: alertChildNavs,
  172. })
  173. }
  174. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  175. if err != nil {
  176. return nil, err
  177. }
  178. for _, plugin := range enabledPlugins.Apps {
  179. if plugin.Pinned {
  180. appLink := &dtos.NavLink{
  181. Text: plugin.Name,
  182. Id: "plugin-page-" + plugin.Id,
  183. Url: plugin.DefaultNavUrl,
  184. Img: plugin.Info.Logos.Small,
  185. }
  186. for _, include := range plugin.Includes {
  187. if !c.HasUserRole(include.Role) {
  188. continue
  189. }
  190. if include.Type == "page" && include.AddToNav {
  191. link := &dtos.NavLink{
  192. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  193. Text: include.Name,
  194. }
  195. appLink.Children = append(appLink.Children, link)
  196. }
  197. if include.Type == "dashboard" && include.AddToNav {
  198. link := &dtos.NavLink{
  199. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  200. Text: include.Name,
  201. }
  202. appLink.Children = append(appLink.Children, link)
  203. }
  204. }
  205. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  206. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  207. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "gicon gicon-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  208. }
  209. if len(appLink.Children) > 0 {
  210. data.NavTree = append(data.NavTree, appLink)
  211. }
  212. }
  213. }
  214. if c.IsGrafanaAdmin || c.OrgRole == m.ROLE_ADMIN {
  215. cfgNode := &dtos.NavLink{
  216. Id: "cfg",
  217. Text: "Configuration",
  218. SubTitle: "Organization: " + c.OrgName,
  219. Icon: "gicon gicon-cog",
  220. Url: setting.AppSubUrl + "/datasources",
  221. Children: []*dtos.NavLink{
  222. {
  223. Text: "Data Sources",
  224. Icon: "gicon gicon-datasources",
  225. Description: "Add and configure data sources",
  226. Id: "datasources",
  227. Url: setting.AppSubUrl + "/datasources",
  228. },
  229. {
  230. Text: "Users",
  231. Id: "users",
  232. Description: "Manage org members",
  233. Icon: "gicon gicon-user",
  234. Url: setting.AppSubUrl + "/org/users",
  235. },
  236. {
  237. Text: "Teams",
  238. Id: "teams",
  239. Description: "Manage org groups",
  240. Icon: "gicon gicon-team",
  241. Url: setting.AppSubUrl + "/org/teams",
  242. },
  243. {
  244. Text: "Plugins",
  245. Id: "plugins",
  246. Description: "View and configure plugins",
  247. Icon: "gicon gicon-plugins",
  248. Url: setting.AppSubUrl + "/plugins",
  249. },
  250. {
  251. Text: "Preferences",
  252. Id: "org-settings",
  253. Description: "Organization preferences",
  254. Icon: "gicon gicon-preferences",
  255. Url: setting.AppSubUrl + "/org",
  256. },
  257. {
  258. Text: "API Keys",
  259. Id: "apikeys",
  260. Description: "Create & manage API keys",
  261. Icon: "gicon gicon-apikeys",
  262. Url: setting.AppSubUrl + "/org/apikeys",
  263. },
  264. },
  265. }
  266. if c.OrgRole != m.ROLE_ADMIN {
  267. cfgNode = &dtos.NavLink{
  268. Id: "cfg",
  269. Text: "Configuration",
  270. SubTitle: "Organization: " + c.OrgName,
  271. Icon: "gicon gicon-cog",
  272. Url: setting.AppSubUrl + "/admin/users",
  273. Children: make([]*dtos.NavLink, 0),
  274. }
  275. }
  276. if c.OrgRole == m.ROLE_ADMIN && c.IsGrafanaAdmin {
  277. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  278. Divider: true, HideFromTabs: true, Id: "admin-divider", Text: "Text",
  279. })
  280. }
  281. if c.IsGrafanaAdmin {
  282. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  283. Text: "Server Admin",
  284. HideFromTabs: true,
  285. SubTitle: "Manage all users & orgs",
  286. Id: "admin",
  287. Icon: "gicon gicon-shield",
  288. Url: setting.AppSubUrl + "/admin/users",
  289. Children: []*dtos.NavLink{
  290. {Text: "Users", Id: "global-users", Url: setting.AppSubUrl + "/admin/users", Icon: "gicon gicon-user"},
  291. {Text: "Orgs", Id: "global-orgs", Url: setting.AppSubUrl + "/admin/orgs", Icon: "gicon gicon-org"},
  292. {Text: "Settings", Id: "server-settings", Url: setting.AppSubUrl + "/admin/settings", Icon: "gicon gicon-preferences"},
  293. {Text: "Stats", Id: "server-stats", Url: setting.AppSubUrl + "/admin/stats", Icon: "fa fa-fw fa-bar-chart"},
  294. {Text: "Style Guide", Id: "styleguide", Url: setting.AppSubUrl + "/styleguide", Icon: "fa fa-fw fa-eyedropper"},
  295. },
  296. })
  297. }
  298. data.NavTree = append(data.NavTree, cfgNode)
  299. }
  300. data.NavTree = append(data.NavTree, &dtos.NavLink{
  301. Text: "Help",
  302. SubTitle: fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit),
  303. Id: "help",
  304. Url: "#",
  305. Icon: "gicon gicon-question",
  306. HideFromMenu: true,
  307. Children: []*dtos.NavLink{
  308. {Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
  309. {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
  310. {Text: "Documentation", Url: "http://docs.grafana.org", Icon: "fa fa-fw fa-file", Target: "_blank"},
  311. },
  312. })
  313. hs.HooksService.RunIndexDataHooks(&data)
  314. return &data, nil
  315. }
  316. func (hs *HTTPServer) Index(c *m.ReqContext) {
  317. data, err := hs.setIndexViewData(c)
  318. if err != nil {
  319. c.Handle(500, "Failed to get settings", err)
  320. return
  321. }
  322. c.HTML(200, "index", data)
  323. }
  324. func (hs *HTTPServer) NotFoundHandler(c *m.ReqContext) {
  325. if c.IsApiRequest() {
  326. c.JsonApiErr(404, "Not found", nil)
  327. return
  328. }
  329. data, err := hs.setIndexViewData(c)
  330. if err != nil {
  331. c.Handle(500, "Failed to get settings", err)
  332. return
  333. }
  334. c.HTML(404, "index", data)
  335. }
  336. func getAppNameBodyClass(name string) string {
  337. switch name {
  338. case setting.APP_NAME:
  339. return "app-grafana"
  340. case setting.APP_NAME_ENTERPRISE:
  341. return "app-enterprise"
  342. default:
  343. return ""
  344. }
  345. }