mirror of
https://github.com/meineerde/redmine.git
synced 2025-12-29 19:59:38 +00:00
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
This commit is contained in:
parent
cb3676a553
commit
64fea07aff
@ -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
|
||||
|
||||
@ -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|
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user