index.go 12 KB

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