summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2014-12-14 21:46:53 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2014-12-14 21:46:53 +0000
commit64fea07affaf161ce352b15dfacca755d28541e2 (patch)
treecd8fa1aee036b44fbdff89b5628c5b245433a392
parentcb3676a553fbce76f5e5ff351f9f7974c674a0d6 (diff)
downloadredmine-64fea07affaf161ce352b15dfacca755d28541e2.tar.gz
redmine-64fea07affaf161ce352b15dfacca755d28541e2.zip
Support for named route in project menu and a new :permission option (#6426).
git-svn-id: http://svn.redmine.org/redmine/trunk@13765 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--lib/redmine/menu_manager.rb26
-rw-r--r--test/unit/lib/redmine/menu_manager/menu_helper_test.rb27
-rw-r--r--test/unit/lib/redmine/menu_manager/menu_item_test.rb6
3 files changed, 47 insertions, 12 deletions
diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb
index 7cff20e46..019a3089f 100644
--- a/lib/redmine/menu_manager.rb
+++ b/lib/redmine/menu_manager.rb
@@ -178,7 +178,11 @@ module Redmine
when Hash
project.nil? ? item.url : {item.param => project}.merge(item.url)
when Symbol
- send(item.url)
+ if project
+ send(item.url, project)
+ else
+ send(item.url)
+ end
else
item.url
end
@@ -376,9 +380,9 @@ module Redmine
class MenuItem < MenuNode
include Redmine::I18n
- attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last
+ attr_reader :name, :url, :param, :condition, :parent, :child_menus, :last, :permission
- def initialize(name, url, options)
+ def initialize(name, url, options={})
raise ArgumentError, "Invalid option :if for menu item '#{name}'" if options[:if] && !options[:if].respond_to?(:call)
raise ArgumentError, "Invalid option :html for menu item '#{name}'" if options[:html] && !options[:html].is_a?(Hash)
raise ArgumentError, "Cannot set the :parent to be the same as this item" if options[:parent] == name.to_sym
@@ -386,6 +390,8 @@ module Redmine
@name = name
@url = url
@condition = options[:if]
+ @permission = options[:permission]
+ @permission ||= false if options.key?(:permission)
@param = options[:param] || :id
@caption = options[:caption]
@html_options = options[:html] || {}
@@ -423,11 +429,19 @@ module Redmine
# Checks if a user is allowed to access the menu item by:
#
- # * Checking the url target (project only)
+ # * Checking the permission or the url target (project only)
# * Checking the conditions of the item
def allowed?(user, project)
- if url.is_a?(Hash) && project && user && !user.allowed_to?(url, project)
- return false
+ if user && project
+ if permission
+ unless user.allowed_to?(permission, project)
+ return false
+ end
+ elsif permission.nil? && url.is_a?(Hash)
+ unless user.allowed_to?(url, project)
+ return false
+ end
+ end
end
if condition && !condition.call(project)
# Condition that doesn't pass
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 acdde9f41..45dfc027f 100644
--- a/test/unit/lib/redmine/menu_manager/menu_helper_test.rb
+++ b/test/unit/lib/redmine/menu_manager/menu_helper_test.rb
@@ -47,6 +47,20 @@ class Redmine::MenuManager::MenuHelperTest < ActionView::TestCase
end
end
+ def test_render_menu_node_with_symbol_as_url
+ node = Redmine::MenuManager::MenuItem.new(:testing, :issues_path)
+ @output_buffer = render_menu_node(node, nil)
+
+ assert_select "a[href=/issues]", "Testing"
+ end
+
+ def test_render_menu_node_with_symbol_as_url_and_project
+ node = Redmine::MenuManager::MenuItem.new(:testing, :project_issues_path)
+ @output_buffer = render_menu_node(node, Project.find(1))
+
+ assert_select "a[href=/projects/ecookbook/issues]", "Testing"
+ end
+
def test_render_menu_node_with_nested_items
parent_node = Redmine::MenuManager::MenuItem.new(:parent_node, '/test', { })
parent_node << Redmine::MenuManager::MenuItem.new(:child_one_node, '/test', { })
@@ -216,6 +230,19 @@ class Redmine::MenuManager::MenuHelperTest < ActionView::TestCase
assert_equal 2, items.size
end
+ def test_menu_items_for_should_skip_items_that_fail_the_permission
+ menu_name = :test_menu_items_for_should_skip_items_that_fail_the_permission
+ Redmine::MenuManager.map menu_name do |menu|
+ menu.push(:a_menu, :project_issues_path)
+ menu.push(:unallowed, :project_issues_path, :permission => :unallowed)
+ end
+
+ User.current = User.find(2)
+
+ items = menu_items_for(menu_name, Project.find(1))
+ assert_equal 1, items.size
+ end
+
def test_menu_items_for_should_skip_items_that_fail_the_conditions
menu_name = :test_menu_items_for_should_skip_items_that_fail_the_conditions
Redmine::MenuManager.map menu_name do |menu|
diff --git a/test/unit/lib/redmine/menu_manager/menu_item_test.rb b/test/unit/lib/redmine/menu_manager/menu_item_test.rb
index df0fc1be8..03c7ca476 100644
--- a/test/unit/lib/redmine/menu_manager/menu_item_test.rb
+++ b/test/unit/lib/redmine/menu_manager/menu_item_test.rb
@@ -46,12 +46,6 @@ class Redmine::MenuManager::MenuItemTest < ActiveSupport::TestCase
end
end
- def test_new_menu_item_should_require_the_options
- assert_raises ArgumentError do
- Redmine::MenuManager::MenuItem.new(:test_missing_options, '/test')
- end
- end
-
def test_new_menu_item_with_all_required_parameters
assert Redmine::MenuManager::MenuItem.new(:test_good_menu, '/test', {})
end