Skip to content

Commit

Permalink
fix(plugins): only add app nav link if it has any pages/dashboards wi…
Browse files Browse the repository at this point in the history
…th role matching current user, fixes #4784
  • Loading branch information
torkelo committed Apr 25, 2016
1 parent 97656d6 commit 04a7917
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
9 changes: 8 additions & 1 deletion pkg/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
}

for _, include := range plugin.Includes {
if !c.HasUserRole(include.Role) {
continue
}

if include.Type == "page" && include.AddToNav {
link := &dtos.NavLink{
Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
Text: include.Name,
}
appLink.Children = append(appLink.Children, link)
}

if include.Type == "dashboard" && include.AddToNav {
link := &dtos.NavLink{
Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
Expand All @@ -124,7 +129,9 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
}

data.MainNavLinks = append(data.MainNavLinks, appLink)
if len(appLink.Children) > 0 {
data.MainNavLinks = append(data.MainNavLinks, appLink)
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/models/org_user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package models

import (
"encoding/json"
"errors"
"fmt"
"time"
)

Expand Down Expand Up @@ -37,6 +39,26 @@ func (r RoleType) Includes(other RoleType) bool {
return r == other
}

func (r *RoleType) UnmarshalJSON(data []byte) error {
var str string
err := json.Unmarshal(data, &str)
if err != nil {
return err
}

*r = RoleType(str)

if (*r).IsValid() == false {
if (*r) != "" {
return errors.New(fmt.Sprintf("JSON validation error: invalid role value: %s", *r))
}

*r = ROLE_VIEWER
}

return nil
}

type OrgUser struct {
Id int64
OrgId int64
Expand Down
24 changes: 15 additions & 9 deletions pkg/plugins/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)

Expand Down Expand Up @@ -69,6 +69,12 @@ func (pb *PluginBase) registerPlugin(pluginDir string) error {
pb.Dependencies.GrafanaVersion = "*"
}

for _, include := range pb.Includes {
if include.Role == "" {
include.Role = m.RoleType(m.ROLE_VIEWER)
}
}

pb.PluginDir = pluginDir
Plugins[pb.Id] = pb
return nil
Expand All @@ -80,14 +86,14 @@ type PluginDependencies struct {
}

type PluginInclude struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Component string `json:"component"`
Role models.RoleType `json:"role"`
AddToNav bool `json:"addToNav"`
DefaultNav bool `json:"defaultNav"`
Slug string `json:"slug"`
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Component string `json:"component"`
Role m.RoleType `json:"role"`
AddToNav bool `json:"addToNav"`
DefaultNav bool `json:"defaultNav"`
Slug string `json:"slug"`

Id string `json:"-"`
}
Expand Down

0 comments on commit 04a7917

Please sign in to comment.