@@ -32,8 +32,27 module ApplicationHelper | |||||
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | # Display a link if user is authorized |
|
34 | # Display a link if user is authorized | |
|
35 | # | |||
|
36 | # @param [String] name Anchor text (passed to link_to) | |||
|
37 | # @param [Hash, String] options Hash params or url for the link target (passed to link_to). | |||
|
38 | # This will checked by authorize_for to see if the user is authorized | |||
|
39 | # @param [optional, Hash] html_options Options passed to link_to | |||
|
40 | # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to | |||
35 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
41 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) | |
36 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) |
|
42 | if options.is_a?(String) | |
|
43 | begin | |||
|
44 | route = ActionController::Routing::Routes.recognize_path(options.gsub(/\?.*/,''), :method => options[:method] || :get) | |||
|
45 | link_controller = route[:controller] | |||
|
46 | link_action = route[:action] | |||
|
47 | rescue ActionController::RoutingError # Parse failed, not a route | |||
|
48 | link_controller, link_action = nil, nil | |||
|
49 | end | |||
|
50 | else | |||
|
51 | link_controller = options[:controller] || params[:controller] | |||
|
52 | link_action = options[:action] | |||
|
53 | end | |||
|
54 | ||||
|
55 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(link_controller, link_action) | |||
37 | end |
|
56 | end | |
38 |
|
57 | |||
39 | # Display a link to remote if user is authorized |
|
58 | # Display a link to remote if user is authorized |
@@ -30,6 +30,35 class ApplicationHelperTest < ActionView::TestCase | |||||
30 | def setup |
|
30 | def setup | |
31 | super |
|
31 | super | |
32 | end |
|
32 | end | |
|
33 | ||||
|
34 | context "#link_to_if_authorized" do | |||
|
35 | context "authorized user" do | |||
|
36 | should "be tested" | |||
|
37 | end | |||
|
38 | ||||
|
39 | context "unauthorized user" do | |||
|
40 | should "be tested" | |||
|
41 | end | |||
|
42 | ||||
|
43 | should "allow using the :controller and :action for the target link" do | |||
|
44 | User.current = User.find_by_login('admin') | |||
|
45 | ||||
|
46 | @project = Issue.first.project # Used by helper | |||
|
47 | response = link_to_if_authorized("By controller/action", | |||
|
48 | {:controller => 'issues', :action => 'edit', :id => Issue.first.id}) | |||
|
49 | assert_match /href/, response | |||
|
50 | end | |||
|
51 | ||||
|
52 | should "allow using the url for the target link" do | |||
|
53 | User.current = User.find_by_login('admin') | |||
|
54 | ||||
|
55 | @project = Issue.first.project # Used by helper | |||
|
56 | response = link_to_if_authorized("By url", | |||
|
57 | new_issue_move_path(:id => Issue.first.id)) | |||
|
58 | assert_match /href/, response | |||
|
59 | end | |||
|
60 | ||||
|
61 | end | |||
33 |
|
62 | |||
34 | def test_auto_links |
|
63 | def test_auto_links | |
35 | to_test = { |
|
64 | to_test = { |
General Comments 0
You need to be logged in to leave comments.
Login now