index.go 9.9 KB

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