Просмотр исходного кода

Merge pull request #3354 from raintank/externalPlugin

Merge changes to external-plugins branch
Torkel Ödegaard 10 лет назад
Родитель
Сommit
5a6981e7af
4 измененных файлов с 49 добавлено и 24 удалено
  1. 17 18
      pkg/api/externalplugin.go
  2. 22 1
      pkg/api/index.go
  3. 5 3
      pkg/plugins/models.go
  4. 5 2
      pkg/plugins/plugins_test.go

+ 17 - 18
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)
 		}
 	}

+ 22 - 1
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})
+			}
 		}
 	}
 

+ 5 - 3
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"`

+ 5 - 2
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)