index.go 12 KB

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