diff --git a/pkg/api/externalplugin.go b/pkg/api/externalplugin.go index e4a7b8639c1..331de160ec7 100644 --- a/pkg/api/externalplugin.go +++ b/pkg/api/externalplugin.go @@ -9,33 +9,32 @@ import ( "github.com/Unknwon/macaron" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" + m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/util" ) func InitExternalPluginRoutes(r *macaron.Macaron) { - /* - // Handle Auth and role requirements - if route.ReqSignedIn { - c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})) - } - if route.ReqGrafanaAdmin { - c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})) - } - if route.ReqRole != nil { - if *route.ReqRole == m.ROLE_EDITOR { - c.Invoke(middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)) - } - if *route.ReqRole == m.ROLE_ADMIN { - c.Invoke(middleware.RoleAuth(m.ROLE_ADMIN)) - } - } - */ for _, plugin := range plugins.ExternalPlugins { log.Info("Plugin: Adding proxy routes for backend plugin") for _, route := range plugin.Routes { url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path) - r.Route(url, route.Method, ExternalPlugin(route.Url)) + handlers := make([]macaron.Handler, 0) + if route.ReqSignedIn { + handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})) + } + if route.ReqGrafanaAdmin { + handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})) + } + if route.ReqSignedIn && route.ReqRole != "" { + if route.ReqRole == m.ROLE_ADMIN { + handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN)) + } else if route.ReqRole == m.ROLE_EDITOR { + handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)) + } + } + handlers = append(handlers, ExternalPlugin(route.Url)) + r.Route(url, route.Method, handlers...) log.Info("Plugin: Adding route %s", url) } } diff --git a/pkg/api/index.go b/pkg/api/index.go index fb18045a18a..efc0fd7a69a 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -70,7 +70,28 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { data.PluginCss = append(data.PluginCss, css.Href) } for _, item := range plugin.MainNavLinks { - data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: item.Text, Href: item.Href, Icon: item.Icon}) + // only show menu items for the specified roles. + var validRoles []m.RoleType + if string(item.ReqRole) == "" || item.ReqRole == m.ROLE_VIEWER { + validRoles = []m.RoleType{m.ROLE_ADMIN, m.ROLE_EDITOR, m.ROLE_VIEWER} + } else if item.ReqRole == m.ROLE_EDITOR { + validRoles = []m.RoleType{m.ROLE_ADMIN, m.ROLE_EDITOR} + } else if item.ReqRole == m.ROLE_ADMIN { + validRoles = []m.RoleType{m.ROLE_ADMIN} + } + ok := true + if len(validRoles) > 0 { + ok = false + for _, role := range validRoles { + if role == c.OrgRole { + ok = true + break + } + } + } + if ok { + data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: item.Text, Href: item.Href, Icon: item.Icon}) + } } } diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index 49156739b5a..14a4407adad 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -41,9 +41,10 @@ type ExternalPluginJs struct { } type ExternalPluginNavLink struct { - Text string `json:"text"` - Icon string `json:"icon"` - Href string `json:"href"` + Text string `json:"text"` + Icon string `json:"icon"` + Href string `json:"href"` + ReqRole models.RoleType `json:"reqRole"` } type ExternalPluginCss struct { @@ -51,6 +52,7 @@ type ExternalPluginCss struct { } type ExternalPlugin struct { + Type string `json:"type"` Routes []*ExternalPluginRoute `json:"routes"` Js []*ExternalPluginJs `json:"js"` Css []*ExternalPluginCss `json:"css"` diff --git a/pkg/plugins/plugins_test.go b/pkg/plugins/plugins_test.go index 4d3e2c98836..bbeac4bba81 100644 --- a/pkg/plugins/plugins_test.go +++ b/pkg/plugins/plugins_test.go @@ -4,14 +4,17 @@ import ( "path/filepath" "testing" + "github.com/grafana/grafana/pkg/setting" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestPluginScans(t *testing.T) { Convey("When scaning for plugins", t, func() { - path, _ := filepath.Abs("../../public/app/plugins") - err := scan(path) + setting.StaticRootPath, _ = filepath.Abs("../../public/") + setting.Cfg = ini.Empty() + err := Init() So(err, ShouldBeNil) So(len(DataSources), ShouldBeGreaterThan, 1)