index.go 11 KB

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