index.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. AppName: setting.ApplicationName,
  70. }
  71. if setting.DisableGravatar {
  72. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/user_profile.png"
  73. }
  74. if len(data.User.Name) == 0 {
  75. data.User.Name = data.User.Login
  76. }
  77. themeURLParam := c.Query("theme")
  78. if themeURLParam == "light" {
  79. data.User.LightTheme = true
  80. data.Theme = "light"
  81. } else if themeURLParam == "dark" {
  82. data.User.LightTheme = false
  83. data.Theme = "dark"
  84. }
  85. if hasEditPermissionInFoldersQuery.Result {
  86. children := []*dtos.NavLink{
  87. {Text: "Dashboard", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/dashboard/new"},
  88. }
  89. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  90. 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"})
  91. }
  92. 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"})
  93. data.NavTree = append(data.NavTree, &dtos.NavLink{
  94. Text: "Create",
  95. Id: "create",
  96. Icon: "fa fa-fw fa-plus",
  97. Url: setting.AppSubUrl + "/dashboard/new",
  98. Children: children,
  99. })
  100. }
  101. dashboardChildNavs := []*dtos.NavLink{
  102. {Text: "Home", Id: "home", Url: setting.AppSubUrl + "/", Icon: "gicon gicon-home", HideFromTabs: true},
  103. {Text: "Divider", Divider: true, Id: "divider", HideFromTabs: true},
  104. {Text: "Manage", Id: "manage-dashboards", Url: setting.AppSubUrl + "/dashboards", Icon: "gicon gicon-manage"},
  105. {Text: "Playlists", Id: "playlists", Url: setting.AppSubUrl + "/playlists", Icon: "gicon gicon-playlists"},
  106. {Text: "Snapshots", Id: "snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "gicon gicon-snapshots"},
  107. }
  108. data.NavTree = append(data.NavTree, &dtos.NavLink{
  109. Text: "Dashboards",
  110. Id: "dashboards",
  111. SubTitle: "Manage dashboards & folders",
  112. Icon: "gicon gicon-dashboard",
  113. Url: setting.AppSubUrl + "/",
  114. Children: dashboardChildNavs,
  115. })
  116. if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  117. data.NavTree = append(data.NavTree, &dtos.NavLink{
  118. Text: "Explore",
  119. Id: "explore",
  120. SubTitle: "Explore your data",
  121. Icon: "fa fa-rocket",
  122. Url: setting.AppSubUrl + "/explore",
  123. Children: []*dtos.NavLink{
  124. {Text: "New tab", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/explore"},
  125. },
  126. })
  127. }
  128. if c.IsSignedIn {
  129. // Only set login if it's different from the name
  130. var login string
  131. if c.SignedInUser.Login != c.SignedInUser.NameOrFallback() {
  132. login = c.SignedInUser.Login
  133. }
  134. profileNode := &dtos.NavLink{
  135. Text: c.SignedInUser.NameOrFallback(),
  136. SubTitle: login,
  137. Id: "profile",
  138. Img: data.User.GravatarUrl,
  139. Url: setting.AppSubUrl + "/profile",
  140. HideFromMenu: true,
  141. Children: []*dtos.NavLink{
  142. {Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "gicon gicon-preferences"},
  143. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  144. },
  145. }
  146. if !setting.DisableSignoutMenu {
  147. // add sign out first
  148. profileNode.Children = append(profileNode.Children, &dtos.NavLink{
  149. Text: "Sign out", Id: "sign-out", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self",
  150. })
  151. }
  152. data.NavTree = append(data.NavTree, profileNode)
  153. }
  154. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  155. alertChildNavs := []*dtos.NavLink{
  156. {Text: "Alert Rules", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "gicon gicon-alert-rules"},
  157. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "gicon gicon-alert-notification-channel"},
  158. }
  159. data.NavTree = append(data.NavTree, &dtos.NavLink{
  160. Text: "Alerting",
  161. SubTitle: "Alert rules & notifications",
  162. Id: "alerting",
  163. Icon: "gicon gicon-alert",
  164. Url: setting.AppSubUrl + "/alerting/list",
  165. Children: alertChildNavs,
  166. })
  167. }
  168. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  169. if err != nil {
  170. return nil, err
  171. }
  172. for _, plugin := range enabledPlugins.Apps {
  173. if plugin.Pinned {
  174. appLink := &dtos.NavLink{
  175. Text: plugin.Name,
  176. Id: "plugin-page-" + plugin.Id,
  177. Url: plugin.DefaultNavUrl,
  178. Img: plugin.Info.Logos.Small,
  179. }
  180. for _, include := range plugin.Includes {
  181. if !c.HasUserRole(include.Role) {
  182. continue
  183. }
  184. if include.Type == "page" && include.AddToNav {
  185. link := &dtos.NavLink{
  186. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  187. Text: include.Name,
  188. }
  189. appLink.Children = append(appLink.Children, link)
  190. }
  191. if include.Type == "dashboard" && include.AddToNav {
  192. link := &dtos.NavLink{
  193. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  194. Text: include.Name,
  195. }
  196. appLink.Children = append(appLink.Children, link)
  197. }
  198. }
  199. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  200. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  201. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "gicon gicon-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  202. }
  203. if len(appLink.Children) > 0 {
  204. data.NavTree = append(data.NavTree, appLink)
  205. }
  206. }
  207. }
  208. if c.IsGrafanaAdmin || c.OrgRole == m.ROLE_ADMIN {
  209. cfgNode := &dtos.NavLink{
  210. Id: "cfg",
  211. Text: "Configuration",
  212. SubTitle: "Organization: " + c.OrgName,
  213. Icon: "gicon gicon-cog",
  214. Url: setting.AppSubUrl + "/datasources",
  215. Children: []*dtos.NavLink{
  216. {
  217. Text: "Data Sources",
  218. Icon: "gicon gicon-datasources",
  219. Description: "Add and configure data sources",
  220. Id: "datasources",
  221. Url: setting.AppSubUrl + "/datasources",
  222. },
  223. {
  224. Text: "Users",
  225. Id: "users",
  226. Description: "Manage org members",
  227. Icon: "gicon gicon-user",
  228. Url: setting.AppSubUrl + "/org/users",
  229. },
  230. {
  231. Text: "Teams",
  232. Id: "teams",
  233. Description: "Manage org groups",
  234. Icon: "gicon gicon-team",
  235. Url: setting.AppSubUrl + "/org/teams",
  236. },
  237. {
  238. Text: "Plugins",
  239. Id: "plugins",
  240. Description: "View and configure plugins",
  241. Icon: "gicon gicon-plugins",
  242. Url: setting.AppSubUrl + "/plugins",
  243. },
  244. {
  245. Text: "Preferences",
  246. Id: "org-settings",
  247. Description: "Organization preferences",
  248. Icon: "gicon gicon-preferences",
  249. Url: setting.AppSubUrl + "/org",
  250. },
  251. {
  252. Text: "API Keys",
  253. Id: "apikeys",
  254. Description: "Create & manage API keys",
  255. Icon: "gicon gicon-apikeys",
  256. Url: setting.AppSubUrl + "/org/apikeys",
  257. },
  258. },
  259. }
  260. if c.OrgRole != m.ROLE_ADMIN {
  261. cfgNode = &dtos.NavLink{
  262. Id: "cfg",
  263. Text: "Configuration",
  264. SubTitle: "Organization: " + c.OrgName,
  265. Icon: "gicon gicon-cog",
  266. Url: setting.AppSubUrl + "/admin/users",
  267. Children: make([]*dtos.NavLink, 0),
  268. }
  269. }
  270. if c.OrgRole == m.ROLE_ADMIN && c.IsGrafanaAdmin {
  271. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  272. Divider: true, HideFromTabs: true, Id: "admin-divider", Text: "Text",
  273. })
  274. }
  275. if c.IsGrafanaAdmin {
  276. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  277. Text: "Server Admin",
  278. HideFromTabs: true,
  279. SubTitle: "Manage all users & orgs",
  280. Id: "admin",
  281. Icon: "gicon gicon-shield",
  282. Url: setting.AppSubUrl + "/admin/users",
  283. Children: []*dtos.NavLink{
  284. {Text: "Users", Id: "global-users", Url: setting.AppSubUrl + "/admin/users", Icon: "gicon gicon-user"},
  285. {Text: "Orgs", Id: "global-orgs", Url: setting.AppSubUrl + "/admin/orgs", Icon: "gicon gicon-org"},
  286. {Text: "Settings", Id: "server-settings", Url: setting.AppSubUrl + "/admin/settings", Icon: "gicon gicon-preferences"},
  287. {Text: "Stats", Id: "server-stats", Url: setting.AppSubUrl + "/admin/stats", Icon: "fa fa-fw fa-bar-chart"},
  288. {Text: "Style Guide", Id: "styleguide", Url: setting.AppSubUrl + "/styleguide", Icon: "fa fa-fw fa-eyedropper"},
  289. },
  290. })
  291. }
  292. data.NavTree = append(data.NavTree, cfgNode)
  293. }
  294. data.NavTree = append(data.NavTree, &dtos.NavLink{
  295. Text: "Help",
  296. SubTitle: fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit),
  297. Id: "help",
  298. Url: "#",
  299. Icon: "gicon gicon-question",
  300. HideFromMenu: true,
  301. Children: []*dtos.NavLink{
  302. {Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
  303. {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
  304. {Text: "Documentation", Url: "http://docs.grafana.org", Icon: "fa fa-fw fa-file", Target: "_blank"},
  305. },
  306. })
  307. return &data, nil
  308. }
  309. func Index(c *m.ReqContext) {
  310. data, err := setIndexViewData(c)
  311. if err != nil {
  312. c.Handle(500, "Failed to get settings", err)
  313. return
  314. }
  315. c.HTML(200, "index", data)
  316. }
  317. func NotFoundHandler(c *m.ReqContext) {
  318. if c.IsApiRequest() {
  319. c.JsonApiErr(404, "Not found", nil)
  320. return
  321. }
  322. data, err := setIndexViewData(c)
  323. if err != nil {
  324. c.Handle(500, "Failed to get settings", err)
  325. return
  326. }
  327. c.HTML(404, "index", data)
  328. }