summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2016-06-11 06:21:52 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2016-06-11 06:21:52 +0000
commit53710d80fc886297d901ac913b4b7570ff5acdb4 (patch)
tree9d090578ecb7bc8b5d32780a432006784550ead7
parent431a73c968e1222a60983039f695b2da7904ddc9 (diff)
downloadredmine-53710d80fc886297d901ac913b4b7570ff5acdb4.tar.gz
redmine-53710d80fc886297d901ac913b4b7570ff5acdb4.zip
Introduce virtual MenuNodes (#15880).
They are characterized by having a blank url. they will only be rendered if the user is authorized to see at least one of its children. they render as links which do nothing when clicked. Patch by Jan Schulz-Hofen. git-svn-id: http://svn.redmine.org/redmine/trunk@15501 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--lib/redmine/menu_manager.rb19
-rw-r--r--test/unit/lib/redmine/menu_manager/menu_helper_test.rb35
2 files changed, 52 insertions, 2 deletions
diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb
index fa7777065..0d2d19a43 100644
--- a/lib/redmine/menu_manager.rb
+++ b/lib/redmine/menu_manager.rb
@@ -147,7 +147,15 @@ module Redmine
end
def render_single_menu_node(item, caption, url, selected)
- link_to(h(caption), url, item.html_options(:selected => selected))
+ options = item.html_options(:selected => selected)
+
+ # virtual nodes are only there for their children to be displayed in the menu
+ # and should not do anything on click, except if otherwise defined elsewhere
+ if url.blank?
+ url = '#'
+ options.reverse_merge!(:onclick => 'return false;')
+ end
+ link_to(h(caption), url, options)
end
def render_unattached_menu_item(menu_item, project)
@@ -433,7 +441,14 @@ module Redmine
# * Checking the permission or the url target (project only)
# * Checking the conditions of the item
def allowed?(user, project)
- if user && project
+ if url.blank?
+ # this is a virtual node that is only there for its children to be diplayed in the menu
+ # it is considered an allowed node if at least one of the children is allowed
+ all_children = children
+ all_children += child_menus.call(project) if child_menus
+ return true if all_children.detect{|child| child.allowed?(user, project) }
+ return false
+ elsif user && project
if permission
unless user.allowed_to?(permission, project)
return false
diff --git a/test/unit/lib/redmine/menu_manager/menu_helper_test.rb b/test/unit/lib/redmine/menu_manager/menu_helper_test.rb
index 404ec6406..e19f066e0 100644
--- a/test/unit/lib/redmine/menu_manager/menu_helper_test.rb
+++ b/test/unit/lib/redmine/menu_manager/menu_helper_test.rb
@@ -208,6 +208,41 @@ class Redmine::MenuManager::MenuHelperTest < ActionView::TestCase
end
end
end
+
+ def test_render_empty_virtual_menu_node_with_children
+
+ # only empty item with no click target
+ Redmine::MenuManager.map :menu1 do |menu|
+ menu.push(:parent_node, nil, { })
+ end
+
+ # parent with unallowed unattached child
+ Redmine::MenuManager.map :menu2 do |menu|
+ menu.push(:parent_node, nil, {:children => Proc.new {|p|
+ [Redmine::MenuManager::MenuItem.new("test_child_unallowed", {:controller => 'issues', :action => 'new'}, {})]
+ } })
+ end
+
+ # parent with unallowed standard child
+ Redmine::MenuManager.map :menu3 do |menu|
+ menu.push(:parent_node, nil, {})
+ menu.push(:test_child_unallowed, {:controller =>'issues', :action => 'new'}, {:parent => :parent_node})
+ end
+
+ # should not be displayed to anonymous
+ User.current = User.find(6)
+ assert_nil render_menu(:menu1, Project.find(1))
+ assert_nil render_menu(:menu2, Project.find(1))
+ assert_nil render_menu(:menu3, Project.find(1))
+
+ # should be displayed to an admin
+ User.current = User.find(1)
+ @output_buffer = render_menu(:menu2, Project.find(1))
+ assert_select("ul li a.parent-node", "Parent node")
+ @output_buffer = render_menu(:menu3, Project.find(1))
+ assert_select("ul li a.parent-node", "Parent node")
+
+ end
def test_render_menu_node_with_children_without_an_array
parent_node = Redmine::MenuManager::MenuItem.new(:parent_node,