mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
NavTree: Refactor out the navtree building from api/index.go and into it's own service (#55552)
This commit is contained in:
69
pkg/services/navtree/models.go
Normal file
69
pkg/services/navtree/models.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package navtree
|
||||
|
||||
const (
|
||||
// These weights may be used by an extension to reliably place
|
||||
// itself in relation to a particular item in the menu. The weights
|
||||
// are negative to ensure that the default items are placed above
|
||||
// any items with default weight.
|
||||
|
||||
WeightSavedItems = (iota - 20) * 100
|
||||
WeightCreate
|
||||
WeightDashboard
|
||||
WeightExplore
|
||||
WeightAlerting
|
||||
WeightDataConnections
|
||||
WeightPlugin
|
||||
WeightConfig
|
||||
WeightAdmin
|
||||
WeightProfile
|
||||
WeightHelp
|
||||
)
|
||||
|
||||
const (
|
||||
NavSectionCore string = "core"
|
||||
NavSectionPlugin string = "plugin"
|
||||
NavSectionConfig string = "config"
|
||||
)
|
||||
|
||||
type NavLink struct {
|
||||
Id string `json:"id,omitempty"`
|
||||
Text string `json:"text"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Section string `json:"section,omitempty"`
|
||||
SubTitle string `json:"subTitle,omitempty"`
|
||||
Icon string `json:"icon,omitempty"` // Available icons can be browsed in Storybook: https://developers.grafana.com/ui/latest/index.html?path=/story/docs-overview-icon--icons-overview
|
||||
Img string `json:"img,omitempty"`
|
||||
Url string `json:"url,omitempty"`
|
||||
Target string `json:"target,omitempty"`
|
||||
SortWeight int64 `json:"sortWeight,omitempty"`
|
||||
Divider bool `json:"divider,omitempty"`
|
||||
HideFromMenu bool `json:"hideFromMenu,omitempty"`
|
||||
HideFromTabs bool `json:"hideFromTabs,omitempty"`
|
||||
ShowIconInNavbar bool `json:"showIconInNavbar,omitempty"`
|
||||
RoundIcon bool `json:"roundIcon,omitempty"`
|
||||
Children []*NavLink `json:"children,omitempty"`
|
||||
HighlightText string `json:"highlightText,omitempty"`
|
||||
HighlightID string `json:"highlightId,omitempty"`
|
||||
EmptyMessageId string `json:"emptyMessageId,omitempty"`
|
||||
}
|
||||
|
||||
// NavIDCfg is the id for org configuration navigation node
|
||||
const NavIDCfg = "cfg"
|
||||
|
||||
func GetServerAdminNode(children []*NavLink) *NavLink {
|
||||
url := ""
|
||||
if len(children) > 0 {
|
||||
url = children[0].Url
|
||||
}
|
||||
return &NavLink{
|
||||
Text: "Server admin",
|
||||
SubTitle: "Manage all users and orgs",
|
||||
HideFromTabs: true,
|
||||
Id: "admin",
|
||||
Icon: "shield",
|
||||
Url: url,
|
||||
SortWeight: WeightAdmin,
|
||||
Section: NavSectionConfig,
|
||||
Children: children,
|
||||
}
|
||||
}
|
||||
10
pkg/services/navtree/navtree.go
Normal file
10
pkg/services/navtree/navtree.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package navtree
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
pref "github.com/grafana/grafana/pkg/services/preference"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
GetNavTree(c *models.ReqContext, hasEditPerm bool, prefs *pref.Preference) ([]*NavLink, error)
|
||||
}
|
||||
116
pkg/services/navtree/navtreeimpl/admin.go
Normal file
116
pkg/services/navtree/navtreeimpl/admin.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package navtreeimpl
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/correlations"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/navtree"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
||||
)
|
||||
|
||||
func (s *ServiceImpl) setupConfigNodes(c *models.ReqContext) ([]*navtree.NavLink, error) {
|
||||
var configNodes []*navtree.NavLink
|
||||
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
if hasAccess(ac.ReqOrgAdmin, datasources.ConfigurationPageAccess) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Data sources",
|
||||
Icon: "database",
|
||||
Description: "Add and configure data sources",
|
||||
Id: "datasources",
|
||||
Url: s.cfg.AppSubURL + "/datasources",
|
||||
})
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagCorrelations) && hasAccess(ac.ReqOrgAdmin, correlations.ConfigurationPageAccess) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Correlations",
|
||||
Icon: "gf-glue",
|
||||
Description: "Add and configure correlations",
|
||||
Id: "correlations",
|
||||
Url: s.cfg.AppSubURL + "/datasources/correlations",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqOrgAdmin, ac.EvalPermission(ac.ActionOrgUsersRead)) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Users",
|
||||
Id: "users",
|
||||
Description: "Manage org members",
|
||||
Icon: "user",
|
||||
Url: s.cfg.AppSubURL + "/org/users",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(s.ReqCanAdminTeams, ac.TeamsAccessEvaluator) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Teams",
|
||||
Id: "teams",
|
||||
Description: "Manage org groups",
|
||||
Icon: "users-alt",
|
||||
Url: s.cfg.AppSubURL + "/org/teams",
|
||||
})
|
||||
}
|
||||
|
||||
// FIXME: while we don't have a permissions for listing plugins the legacy check has to stay as a default
|
||||
if plugins.ReqCanAdminPlugins(s.cfg)(c) || hasAccess(plugins.ReqCanAdminPlugins(s.cfg), plugins.AdminAccessEvaluator) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Plugins",
|
||||
Id: "plugins",
|
||||
Description: "View and configure plugins",
|
||||
Icon: "plug",
|
||||
Url: s.cfg.AppSubURL + "/plugins",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqOrgAdmin, ac.OrgPreferencesAccessEvaluator) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Preferences",
|
||||
Id: "org-settings",
|
||||
Description: "Organization preferences",
|
||||
Icon: "sliders-v-alt",
|
||||
Url: s.cfg.AppSubURL + "/org",
|
||||
})
|
||||
}
|
||||
|
||||
hideApiKeys, _, _ := s.kvStore.Get(c.Req.Context(), c.OrgID, "serviceaccounts", "hideApiKeys")
|
||||
apiKeys, err := s.apiKeyService.GetAllAPIKeys(c.Req.Context(), c.OrgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiKeysHidden := hideApiKeys == "1" && len(apiKeys) == 0
|
||||
if hasAccess(ac.ReqOrgAdmin, ac.ApiKeyAccessEvaluator) && !apiKeysHidden {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "API keys",
|
||||
Id: "apikeys",
|
||||
Description: "Create & manage API keys",
|
||||
Icon: "key-skeleton-alt",
|
||||
Url: s.cfg.AppSubURL + "/org/apikeys",
|
||||
})
|
||||
}
|
||||
|
||||
if enableServiceAccount(s, c) {
|
||||
configNodes = append(configNodes, &navtree.NavLink{
|
||||
Text: "Service accounts",
|
||||
Id: "serviceaccounts",
|
||||
Description: "Manage service accounts",
|
||||
Icon: "gf-service-account",
|
||||
Url: s.cfg.AppSubURL + "/org/serviceaccounts",
|
||||
})
|
||||
}
|
||||
return configNodes, nil
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) ReqCanAdminTeams(c *models.ReqContext) bool {
|
||||
return c.OrgRole == org.RoleAdmin || (s.cfg.EditorsCanAdmin && c.OrgRole == org.RoleEditor)
|
||||
}
|
||||
|
||||
func enableServiceAccount(s *ServiceImpl, c *models.ReqContext) bool {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
return hasAccess(ac.ReqOrgAdmin, serviceaccounts.AccessEvaluator)
|
||||
}
|
||||
113
pkg/services/navtree/navtreeimpl/applinks.go
Normal file
113
pkg/services/navtree/navtreeimpl/applinks.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package navtreeimpl
|
||||
|
||||
import (
|
||||
"path"
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/navtree"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||
)
|
||||
|
||||
func (s *ServiceImpl) getAppLinks(c *models.ReqContext) ([]*navtree.NavLink, error) {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
appLinks := []*navtree.NavLink{}
|
||||
|
||||
pss, err := s.pluginSettings.GetPluginSettings(c.Req.Context(), &pluginsettings.GetArgs{OrgID: c.OrgID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
isPluginEnabled := func(plugin plugins.PluginDTO) bool {
|
||||
if plugin.AutoEnabled {
|
||||
return true
|
||||
}
|
||||
for _, ps := range pss {
|
||||
if ps.PluginID == plugin.ID {
|
||||
return ps.Enabled
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
for _, plugin := range s.pluginStore.Plugins(c.Req.Context(), plugins.App) {
|
||||
if !isPluginEnabled(plugin) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !hasAccess(ac.ReqSignedIn,
|
||||
ac.EvalPermission(plugins.ActionAppAccess, plugins.ScopeProvider.GetResourceScope(plugin.ID))) {
|
||||
continue
|
||||
}
|
||||
|
||||
appLink := &navtree.NavLink{
|
||||
Text: plugin.Name,
|
||||
Id: "plugin-page-" + plugin.ID,
|
||||
Img: plugin.Info.Logos.Small,
|
||||
Section: navtree.NavSectionPlugin,
|
||||
SortWeight: navtree.WeightPlugin,
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
appLink.Url = s.cfg.AppSubURL + "/a/" + plugin.ID
|
||||
} else {
|
||||
appLink.Url = path.Join(s.cfg.AppSubURL, plugin.DefaultNavURL)
|
||||
}
|
||||
|
||||
for _, include := range plugin.Includes {
|
||||
if !c.HasUserRole(include.Role) {
|
||||
continue
|
||||
}
|
||||
|
||||
if include.Type == "page" && include.AddToNav {
|
||||
var link *navtree.NavLink
|
||||
if len(include.Path) > 0 {
|
||||
link = &navtree.NavLink{
|
||||
Url: s.cfg.AppSubURL + include.Path,
|
||||
Text: include.Name,
|
||||
}
|
||||
if include.DefaultNav && !s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
appLink.Url = link.Url // Overwrite the hardcoded page logic
|
||||
}
|
||||
} else {
|
||||
link = &navtree.NavLink{
|
||||
Url: s.cfg.AppSubURL + "/plugins/" + plugin.ID + "/page/" + include.Slug,
|
||||
Text: include.Name,
|
||||
}
|
||||
}
|
||||
link.Icon = include.Icon
|
||||
appLink.Children = append(appLink.Children, link)
|
||||
}
|
||||
|
||||
if include.Type == "dashboard" && include.AddToNav {
|
||||
dboardURL := include.DashboardURLPath()
|
||||
if dboardURL != "" {
|
||||
link := &navtree.NavLink{
|
||||
Url: path.Join(s.cfg.AppSubURL, dboardURL),
|
||||
Text: include.Name,
|
||||
}
|
||||
appLink.Children = append(appLink.Children, link)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(appLink.Children) > 0 {
|
||||
// If we only have one child and it's the app default nav then remove it from children
|
||||
if len(appLink.Children) == 1 && appLink.Children[0].Url == appLink.Url {
|
||||
appLink.Children = []*navtree.NavLink{}
|
||||
}
|
||||
appLinks = append(appLinks, appLink)
|
||||
}
|
||||
}
|
||||
|
||||
if len(appLinks) > 0 {
|
||||
sort.SliceStable(appLinks, func(i, j int) bool {
|
||||
return appLinks[i].Text < appLinks[j].Text
|
||||
})
|
||||
}
|
||||
|
||||
return appLinks, nil
|
||||
}
|
||||
598
pkg/services/navtree/navtreeimpl/navtree.go
Normal file
598
pkg/services/navtree/navtreeimpl/navtree.go
Normal file
@@ -0,0 +1,598 @@
|
||||
package navtreeimpl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/infra/kvstore"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/apikey"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/services/navtree"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||
pref "github.com/grafana/grafana/pkg/services/preference"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
type ServiceImpl struct {
|
||||
cfg *setting.Cfg
|
||||
log log.Logger
|
||||
accessControl ac.AccessControl
|
||||
pluginStore plugins.Store
|
||||
pluginSettings pluginsettings.Service
|
||||
starService star.Service
|
||||
features *featuremgmt.FeatureManager
|
||||
dashboardService dashboards.DashboardService
|
||||
accesscontrolService ac.Service
|
||||
kvStore kvstore.KVStore
|
||||
apiKeyService apikey.Service
|
||||
}
|
||||
|
||||
func ProvideService(cfg *setting.Cfg, accessControl ac.AccessControl, pluginStore plugins.Store, pluginSettings pluginsettings.Service, starService star.Service, features *featuremgmt.FeatureManager, dashboardService dashboards.DashboardService, accesscontrolService ac.Service, kvStore kvstore.KVStore, apiKeyService apikey.Service) navtree.Service {
|
||||
return &ServiceImpl{
|
||||
cfg: cfg,
|
||||
log: log.New("navtree service"),
|
||||
accessControl: accessControl,
|
||||
pluginStore: pluginStore,
|
||||
pluginSettings: pluginSettings,
|
||||
starService: starService,
|
||||
features: features,
|
||||
dashboardService: dashboardService,
|
||||
accesscontrolService: accesscontrolService,
|
||||
kvStore: kvStore,
|
||||
apiKeyService: apiKeyService,
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func (s *ServiceImpl) GetNavTree(c *models.ReqContext, hasEditPerm bool, prefs *pref.Preference) ([]*navtree.NavLink, error) {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
var navTree []*navtree.NavLink
|
||||
|
||||
if hasAccess(ac.ReqSignedIn, ac.EvalPermission(dashboards.ActionDashboardsRead)) {
|
||||
starredItemsLinks, err := s.buildStarredItemsNavLinks(c, prefs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
navTree = append(navTree, &navtree.NavLink{
|
||||
Text: "Starred",
|
||||
Id: "starred",
|
||||
Icon: "star",
|
||||
SortWeight: navtree.WeightSavedItems,
|
||||
Section: navtree.NavSectionCore,
|
||||
Children: starredItemsLinks,
|
||||
EmptyMessageId: "starred-empty",
|
||||
})
|
||||
|
||||
dashboardChildLinks := s.buildDashboardNavLinks(c, hasEditPerm)
|
||||
|
||||
dashboardsUrl := "/dashboards"
|
||||
|
||||
dashboardLink := &navtree.NavLink{
|
||||
Text: "Dashboards",
|
||||
Id: "dashboards",
|
||||
SubTitle: "Manage dashboards and folders",
|
||||
Icon: "apps",
|
||||
Url: s.cfg.AppSubURL + dashboardsUrl,
|
||||
SortWeight: navtree.WeightDashboard,
|
||||
Section: navtree.NavSectionCore,
|
||||
Children: dashboardChildLinks,
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
dashboardLink.Id = "dashboards/browse"
|
||||
}
|
||||
|
||||
navTree = append(navTree, dashboardLink)
|
||||
}
|
||||
|
||||
canExplore := func(context *models.ReqContext) bool {
|
||||
return c.OrgRole == org.RoleAdmin || c.OrgRole == org.RoleEditor || setting.ViewersCanEdit
|
||||
}
|
||||
|
||||
if setting.ExploreEnabled && hasAccess(canExplore, ac.EvalPermission(ac.ActionDatasourcesExplore)) {
|
||||
navTree = append(navTree, &navtree.NavLink{
|
||||
Text: "Explore",
|
||||
Id: "explore",
|
||||
SubTitle: "Explore your data",
|
||||
Icon: "compass",
|
||||
SortWeight: navtree.WeightExplore,
|
||||
Section: navtree.NavSectionCore,
|
||||
Url: s.cfg.AppSubURL + "/explore",
|
||||
})
|
||||
}
|
||||
|
||||
navTree = s.addProfile(navTree, c)
|
||||
|
||||
_, uaIsDisabledForOrg := s.cfg.UnifiedAlerting.DisabledOrgs[c.OrgID]
|
||||
uaVisibleForOrg := s.cfg.UnifiedAlerting.IsEnabled() && !uaIsDisabledForOrg
|
||||
|
||||
if setting.AlertingEnabled != nil && *setting.AlertingEnabled {
|
||||
navTree = append(navTree, s.buildLegacyAlertNavLinks(c)...)
|
||||
} else if uaVisibleForOrg {
|
||||
navTree = append(navTree, s.buildAlertNavLinks(c, hasEditPerm)...)
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagDataConnectionsConsole) {
|
||||
navTree = append(navTree, s.buildDataConnectionsNavLink(c))
|
||||
}
|
||||
|
||||
appLinks, err := s.getAppLinks(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// When topnav is enabled we can test new information architecture where plugins live in Apps category
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
navTree = append(navTree, &navtree.NavLink{
|
||||
Text: "Apps",
|
||||
Icon: "apps",
|
||||
Description: "App plugins",
|
||||
Id: "apps",
|
||||
Children: appLinks,
|
||||
Section: navtree.NavSectionCore,
|
||||
Url: s.cfg.AppSubURL + "/apps",
|
||||
})
|
||||
} else {
|
||||
navTree = append(navTree, appLinks...)
|
||||
}
|
||||
|
||||
configNodes, err := s.setupConfigNodes(c)
|
||||
if err != nil {
|
||||
return navTree, err
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagLivePipeline) {
|
||||
liveNavLinks := []*navtree.NavLink{}
|
||||
|
||||
liveNavLinks = append(liveNavLinks, &navtree.NavLink{
|
||||
Text: "Status", Id: "live-status", Url: s.cfg.AppSubURL + "/live", Icon: "exchange-alt",
|
||||
})
|
||||
liveNavLinks = append(liveNavLinks, &navtree.NavLink{
|
||||
Text: "Pipeline", Id: "live-pipeline", Url: s.cfg.AppSubURL + "/live/pipeline", Icon: "arrow-to-right",
|
||||
})
|
||||
liveNavLinks = append(liveNavLinks, &navtree.NavLink{
|
||||
Text: "Cloud", Id: "live-cloud", Url: s.cfg.AppSubURL + "/live/cloud", Icon: "cloud-upload",
|
||||
})
|
||||
navTree = append(navTree, &navtree.NavLink{
|
||||
Id: "live",
|
||||
Text: "Live",
|
||||
SubTitle: "Event streaming",
|
||||
Icon: "exchange-alt",
|
||||
Url: s.cfg.AppSubURL + "/live",
|
||||
Children: liveNavLinks,
|
||||
Section: navtree.NavSectionConfig,
|
||||
HideFromTabs: true,
|
||||
})
|
||||
}
|
||||
|
||||
var configNode *navtree.NavLink
|
||||
var serverAdminNode *navtree.NavLink
|
||||
|
||||
if len(configNodes) > 0 {
|
||||
configNode = &navtree.NavLink{
|
||||
Id: navtree.NavIDCfg,
|
||||
Text: "Configuration",
|
||||
SubTitle: "Organization: " + c.OrgName,
|
||||
Icon: "cog",
|
||||
Url: configNodes[0].Url,
|
||||
Section: navtree.NavSectionConfig,
|
||||
SortWeight: navtree.WeightConfig,
|
||||
Children: configNodes,
|
||||
}
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
configNode.Url = "/admin"
|
||||
} else {
|
||||
configNode.Url = configNodes[0].Url
|
||||
}
|
||||
navTree = append(navTree, configNode)
|
||||
}
|
||||
|
||||
adminNavLinks := s.buildAdminNavLinks(c)
|
||||
|
||||
if len(adminNavLinks) > 0 {
|
||||
serverAdminNode = navtree.GetServerAdminNode(adminNavLinks)
|
||||
navTree = append(navTree, serverAdminNode)
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
// Move server admin into Configuration and rename to administration
|
||||
if configNode != nil && serverAdminNode != nil {
|
||||
configNode.Text = "Administration"
|
||||
serverAdminNode.Url = "/admin/server"
|
||||
serverAdminNode.HideFromTabs = false
|
||||
configNode.Children = append(configNode.Children, serverAdminNode)
|
||||
adminNodeIndex := len(navTree) - 1
|
||||
navTree = navTree[:adminNodeIndex]
|
||||
}
|
||||
}
|
||||
|
||||
navTree = s.addHelpLinks(navTree, c)
|
||||
|
||||
return navTree, nil
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) addHelpLinks(navTree []*navtree.NavLink, c *models.ReqContext) []*navtree.NavLink {
|
||||
if setting.HelpEnabled {
|
||||
helpVersion := fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit)
|
||||
if s.cfg.AnonymousHideVersion && !c.IsSignedIn {
|
||||
helpVersion = setting.ApplicationName
|
||||
}
|
||||
|
||||
navTree = append(navTree, &navtree.NavLink{
|
||||
Text: "Help",
|
||||
SubTitle: helpVersion,
|
||||
Id: "help",
|
||||
Url: "#",
|
||||
Icon: "question-circle",
|
||||
SortWeight: navtree.WeightHelp,
|
||||
Section: navtree.NavSectionConfig,
|
||||
Children: []*navtree.NavLink{},
|
||||
})
|
||||
}
|
||||
return navTree
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) addProfile(navTree []*navtree.NavLink, c *models.ReqContext) []*navtree.NavLink {
|
||||
if setting.ProfileEnabled && c.IsSignedIn {
|
||||
navTree = append(navTree, s.getProfileNode(c))
|
||||
}
|
||||
return navTree
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) getProfileNode(c *models.ReqContext) *navtree.NavLink {
|
||||
// Only set login if it's different from the name
|
||||
var login string
|
||||
if c.SignedInUser.Login != c.SignedInUser.NameOrFallback() {
|
||||
login = c.SignedInUser.Login
|
||||
}
|
||||
gravatarURL := dtos.GetGravatarUrl(c.Email)
|
||||
|
||||
children := []*navtree.NavLink{
|
||||
{
|
||||
Text: "Preferences", Id: "profile/settings", Url: s.cfg.AppSubURL + "/profile", Icon: "sliders-v-alt",
|
||||
},
|
||||
}
|
||||
|
||||
children = append(children, &navtree.NavLink{
|
||||
Text: "Notification history", Id: "profile/notifications", Url: s.cfg.AppSubURL + "/profile/notifications", Icon: "bell",
|
||||
})
|
||||
|
||||
if setting.AddChangePasswordLink() {
|
||||
children = append(children, &navtree.NavLink{
|
||||
Text: "Change password", Id: "profile/password", Url: s.cfg.AppSubURL + "/profile/password",
|
||||
Icon: "lock",
|
||||
})
|
||||
}
|
||||
|
||||
if !setting.DisableSignoutMenu {
|
||||
// add sign out first
|
||||
children = append(children, &navtree.NavLink{
|
||||
Text: "Sign out",
|
||||
Id: "sign-out",
|
||||
Url: s.cfg.AppSubURL + "/logout",
|
||||
Icon: "arrow-from-right",
|
||||
Target: "_self",
|
||||
HideFromTabs: true,
|
||||
})
|
||||
}
|
||||
|
||||
return &navtree.NavLink{
|
||||
Text: c.SignedInUser.NameOrFallback(),
|
||||
SubTitle: login,
|
||||
Id: "profile",
|
||||
Img: gravatarURL,
|
||||
Url: s.cfg.AppSubURL + "/profile",
|
||||
Section: navtree.NavSectionConfig,
|
||||
SortWeight: navtree.WeightProfile,
|
||||
Children: children,
|
||||
RoundIcon: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildStarredItemsNavLinks(c *models.ReqContext, prefs *pref.Preference) ([]*navtree.NavLink, error) {
|
||||
starredItemsChildNavs := []*navtree.NavLink{}
|
||||
|
||||
query := star.GetUserStarsQuery{
|
||||
UserID: c.SignedInUser.UserID,
|
||||
}
|
||||
|
||||
starredDashboardResult, err := s.starService.GetByUser(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
starredDashboards := []*models.Dashboard{}
|
||||
starredDashboardsCounter := 0
|
||||
for dashboardId := range starredDashboardResult.UserStars {
|
||||
// Set a loose limit to the first 50 starred dashboards found
|
||||
if starredDashboardsCounter > 50 {
|
||||
break
|
||||
}
|
||||
starredDashboardsCounter++
|
||||
query := &models.GetDashboardQuery{
|
||||
Id: dashboardId,
|
||||
OrgId: c.OrgID,
|
||||
}
|
||||
err := s.dashboardService.GetDashboard(c.Req.Context(), query)
|
||||
if err == nil {
|
||||
starredDashboards = append(starredDashboards, query.Result)
|
||||
}
|
||||
}
|
||||
|
||||
if len(starredDashboards) > 0 {
|
||||
sort.Slice(starredDashboards, func(i, j int) bool {
|
||||
return starredDashboards[i].Title < starredDashboards[j].Title
|
||||
})
|
||||
for _, starredItem := range starredDashboards {
|
||||
starredItemsChildNavs = append(starredItemsChildNavs, &navtree.NavLink{
|
||||
Id: starredItem.Uid,
|
||||
Text: starredItem.Title,
|
||||
Url: starredItem.GetUrl(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return starredItemsChildNavs, nil
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildDashboardNavLinks(c *models.ReqContext, hasEditPerm bool) []*navtree.NavLink {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
hasEditPermInAnyFolder := func(c *models.ReqContext) bool {
|
||||
return hasEditPerm
|
||||
}
|
||||
|
||||
dashboardChildNavs := []*navtree.NavLink{}
|
||||
if !s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Browse", Id: "dashboards/browse", Url: s.cfg.AppSubURL + "/dashboards", Icon: "sitemap",
|
||||
})
|
||||
}
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Playlists", Id: "dashboards/playlists", Url: s.cfg.AppSubURL + "/playlists", Icon: "presentation-play",
|
||||
})
|
||||
|
||||
if c.IsSignedIn {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Snapshots",
|
||||
Id: "dashboards/snapshots",
|
||||
Url: s.cfg.AppSubURL + "/dashboard/snapshots",
|
||||
Icon: "camera",
|
||||
})
|
||||
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Library panels",
|
||||
Id: "dashboards/library-panels",
|
||||
Url: s.cfg.AppSubURL + "/library-panels",
|
||||
Icon: "library-panel",
|
||||
})
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagScenes) {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Scenes",
|
||||
Id: "scenes",
|
||||
Url: s.cfg.AppSubURL + "/scenes",
|
||||
Icon: "apps",
|
||||
})
|
||||
}
|
||||
|
||||
if hasEditPerm {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Divider", Divider: true, Id: "divider", HideFromTabs: true,
|
||||
})
|
||||
|
||||
if hasAccess(hasEditPermInAnyFolder, ac.EvalPermission(dashboards.ActionDashboardsCreate)) {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "New dashboard", Icon: "plus", Url: s.cfg.AppSubURL + "/dashboard/new", HideFromTabs: true, Id: "dashboards/new", ShowIconInNavbar: true,
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqOrgAdminOrEditor, ac.EvalPermission(dashboards.ActionFoldersCreate)) {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "New folder", SubTitle: "Create a new folder to organize your dashboards", Id: "dashboards/folder/new",
|
||||
Icon: "plus", Url: s.cfg.AppSubURL + "/dashboards/folder/new", HideFromTabs: true, ShowIconInNavbar: true,
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(hasEditPermInAnyFolder, ac.EvalPermission(dashboards.ActionDashboardsCreate)) {
|
||||
dashboardChildNavs = append(dashboardChildNavs, &navtree.NavLink{
|
||||
Text: "Import", SubTitle: "Import dashboard from file or Grafana.com", Id: "dashboards/import", Icon: "plus",
|
||||
Url: s.cfg.AppSubURL + "/dashboard/import", HideFromTabs: true, ShowIconInNavbar: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
return dashboardChildNavs
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildLegacyAlertNavLinks(c *models.ReqContext) []*navtree.NavLink {
|
||||
var alertChildNavs []*navtree.NavLink
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Alert rules", Id: "alert-list", Url: s.cfg.AppSubURL + "/alerting/list", Icon: "list-ul",
|
||||
})
|
||||
|
||||
if c.HasRole(org.RoleEditor) {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Notification channels", Id: "channels", Url: s.cfg.AppSubURL + "/alerting/notifications",
|
||||
Icon: "comment-alt-share",
|
||||
})
|
||||
}
|
||||
|
||||
var alertNav = navtree.NavLink{
|
||||
Text: "Alerting",
|
||||
SubTitle: "Alert rules and notifications",
|
||||
Id: "alerting-legacy",
|
||||
Icon: "bell",
|
||||
Children: alertChildNavs,
|
||||
Section: navtree.NavSectionCore,
|
||||
SortWeight: navtree.WeightAlerting,
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
alertNav.Url = s.cfg.AppSubURL + "/alerting"
|
||||
} else {
|
||||
alertNav.Url = s.cfg.AppSubURL + "/alerting/list"
|
||||
}
|
||||
|
||||
return []*navtree.NavLink{&alertNav}
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildAlertNavLinks(c *models.ReqContext, hasEditPerm bool) []*navtree.NavLink {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
var alertChildNavs []*navtree.NavLink
|
||||
|
||||
if hasAccess(ac.ReqViewer, ac.EvalAny(ac.EvalPermission(ac.ActionAlertingRuleRead), ac.EvalPermission(ac.ActionAlertingRuleExternalRead))) {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Alert rules", Id: "alert-list", Url: s.cfg.AppSubURL + "/alerting/list", Icon: "list-ul",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqOrgAdminOrEditor, ac.EvalAny(ac.EvalPermission(ac.ActionAlertingNotificationsRead), ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead))) {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Contact points", Id: "receivers", Url: s.cfg.AppSubURL + "/alerting/notifications",
|
||||
Icon: "comment-alt-share", SubTitle: "Manage the settings of your contact points",
|
||||
})
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{Text: "Notification policies", Id: "am-routes", Url: s.cfg.AppSubURL + "/alerting/routes", Icon: "sitemap"})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqViewer, ac.EvalAny(ac.EvalPermission(ac.ActionAlertingInstanceRead), ac.EvalPermission(ac.ActionAlertingInstancesExternalRead))) {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{Text: "Silences", Id: "silences", Url: s.cfg.AppSubURL + "/alerting/silences", Icon: "bell-slash"})
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{Text: "Alert groups", Id: "groups", Url: s.cfg.AppSubURL + "/alerting/groups", Icon: "layer-group"})
|
||||
}
|
||||
|
||||
if c.OrgRole == org.RoleAdmin {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Admin", Id: "alerting-admin", Url: s.cfg.AppSubURL + "/alerting/admin",
|
||||
Icon: "cog",
|
||||
})
|
||||
}
|
||||
|
||||
fallbackHasEditPerm := func(*models.ReqContext) bool { return hasEditPerm }
|
||||
|
||||
if hasAccess(fallbackHasEditPerm, ac.EvalAny(ac.EvalPermission(ac.ActionAlertingRuleCreate), ac.EvalPermission(ac.ActionAlertingRuleExternalWrite))) {
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "Divider", Divider: true, Id: "divider", HideFromTabs: true,
|
||||
})
|
||||
|
||||
alertChildNavs = append(alertChildNavs, &navtree.NavLink{
|
||||
Text: "New alert rule", SubTitle: "Create an alert rule", Id: "alert",
|
||||
Icon: "plus", Url: s.cfg.AppSubURL + "/alerting/new", HideFromTabs: true, ShowIconInNavbar: true,
|
||||
})
|
||||
}
|
||||
|
||||
if len(alertChildNavs) > 0 {
|
||||
var alertNav = navtree.NavLink{
|
||||
Text: "Alerting",
|
||||
SubTitle: "Alert rules and notifications",
|
||||
Id: "alerting",
|
||||
Icon: "bell",
|
||||
Children: alertChildNavs,
|
||||
Section: navtree.NavSectionCore,
|
||||
SortWeight: navtree.WeightAlerting,
|
||||
}
|
||||
|
||||
if s.features.IsEnabled(featuremgmt.FlagTopnav) {
|
||||
alertNav.Url = s.cfg.AppSubURL + "/alerting"
|
||||
} else {
|
||||
alertNav.Url = s.cfg.AppSubURL + "/alerting/list"
|
||||
}
|
||||
|
||||
return []*navtree.NavLink{&alertNav}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildDataConnectionsNavLink(c *models.ReqContext) *navtree.NavLink {
|
||||
var children []*navtree.NavLink
|
||||
var navLink *navtree.NavLink
|
||||
|
||||
baseId := "data-connections"
|
||||
baseUrl := s.cfg.AppSubURL + "/" + baseId
|
||||
|
||||
children = append(children, &navtree.NavLink{
|
||||
Id: baseId + "-datasources",
|
||||
Text: "Data sources",
|
||||
Icon: "database",
|
||||
Description: "Add and configure data sources",
|
||||
Url: baseUrl + "/datasources",
|
||||
})
|
||||
|
||||
children = append(children, &navtree.NavLink{
|
||||
Id: baseId + "-plugins",
|
||||
Text: "Plugins",
|
||||
Icon: "plug",
|
||||
Description: "Manage plugins",
|
||||
Url: baseUrl + "/plugins",
|
||||
})
|
||||
|
||||
children = append(children, &navtree.NavLink{
|
||||
Id: baseId + "-cloud-integrations",
|
||||
Text: "Cloud integrations",
|
||||
Icon: "bolt",
|
||||
Description: "Manage your cloud integrations",
|
||||
Url: baseUrl + "/cloud-integrations",
|
||||
})
|
||||
|
||||
navLink = &navtree.NavLink{
|
||||
Text: "Data Connections",
|
||||
Icon: "link",
|
||||
Id: baseId,
|
||||
Url: baseUrl,
|
||||
Children: children,
|
||||
Section: navtree.NavSectionCore,
|
||||
SortWeight: navtree.WeightDataConnections,
|
||||
}
|
||||
|
||||
return navLink
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) buildAdminNavLinks(c *models.ReqContext) []*navtree.NavLink {
|
||||
hasAccess := ac.HasAccess(s.accessControl, c)
|
||||
hasGlobalAccess := ac.HasGlobalAccess(s.accessControl, s.accesscontrolService, c)
|
||||
orgsAccessEvaluator := ac.EvalPermission(ac.ActionOrgsRead)
|
||||
adminNavLinks := []*navtree.NavLink{}
|
||||
|
||||
if hasAccess(ac.ReqGrafanaAdmin, ac.EvalPermission(ac.ActionUsersRead, ac.ScopeGlobalUsersAll)) {
|
||||
adminNavLinks = append(adminNavLinks, &navtree.NavLink{
|
||||
Text: "Users", Id: "global-users", Url: s.cfg.AppSubURL + "/admin/users", Icon: "user",
|
||||
})
|
||||
}
|
||||
|
||||
if hasGlobalAccess(ac.ReqGrafanaAdmin, orgsAccessEvaluator) {
|
||||
adminNavLinks = append(adminNavLinks, &navtree.NavLink{
|
||||
Text: "Orgs", Id: "global-orgs", Url: s.cfg.AppSubURL + "/admin/orgs", Icon: "building",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)) {
|
||||
adminNavLinks = append(adminNavLinks, &navtree.NavLink{
|
||||
Text: "Settings", Id: "server-settings", Url: s.cfg.AppSubURL + "/admin/settings", Icon: "sliders-v-alt",
|
||||
})
|
||||
}
|
||||
|
||||
if hasAccess(ac.ReqGrafanaAdmin, ac.EvalPermission(ac.ActionSettingsRead)) && s.features.IsEnabled(featuremgmt.FlagStorage) {
|
||||
adminNavLinks = append(adminNavLinks, &navtree.NavLink{
|
||||
Text: "Storage",
|
||||
Id: "storage",
|
||||
Description: "Manage file storage",
|
||||
Icon: "cube",
|
||||
Url: s.cfg.AppSubURL + "/admin/storage",
|
||||
})
|
||||
}
|
||||
|
||||
if s.cfg.LDAPEnabled && hasAccess(ac.ReqGrafanaAdmin, ac.EvalPermission(ac.ActionLDAPStatusRead)) {
|
||||
adminNavLinks = append(adminNavLinks, &navtree.NavLink{
|
||||
Text: "LDAP", Id: "ldap", Url: s.cfg.AppSubURL + "/admin/ldap", Icon: "book",
|
||||
})
|
||||
}
|
||||
|
||||
return adminNavLinks
|
||||
}
|
||||
Reference in New Issue
Block a user