Skip to content

Commit

Permalink
restrict format access in ResourceController
Browse files Browse the repository at this point in the history
fixes #3539
ActiveAdmin raises Access Denied Error if format was disabled with download_links
  • Loading branch information
Fivell committed Mar 18, 2017
1 parent 33bdc23 commit 9f83674
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
22 changes: 22 additions & 0 deletions features/index/formats.feature
Expand Up @@ -51,6 +51,15 @@ Feature: Index Formats
And I should not see a link to download "XML"
And I should not see a link to download "JSON"

When I go to the csv index page for posts
Then access denied

When I go to the xml index page for posts
Then access denied

When I go to the json index page for posts
Then access denied

Scenario: View index with download_links block which returns [:csv]
Given an index configuration of:
"""
Expand All @@ -64,3 +73,16 @@ Feature: Index Formats
And I should not see a link to download "XML"
And I should not see a link to download "JSON"
And I should not see a link to download "PDF"

Scenario: View index with restricted formats
Given an index configuration of:
"""
ActiveAdmin.register Post do
index download_links: -> { [:json] }
end
"""
When I go to the csv index page for posts
Then access denied

When I go to the xml index page for posts
Then access denied
4 changes: 4 additions & 0 deletions features/step_definitions/format_steps.rb
Expand Up @@ -50,3 +50,7 @@
Then /^the encoding of the CSV file should be "([^"]*)"$/ do |text|
expect(page.driver.response.body.encoding).to be Encoding.find(Encoding.aliases[text] || text)
end

Then /^access denied$/ do
expect(page).to have_content(I18n.t("active_admin.access_denied.message"))
end
3 changes: 3 additions & 0 deletions features/support/paths.rb
Expand Up @@ -41,6 +41,9 @@ def path_to(page_name)
when /^the index page for (.*)$/
send "admin_#{$1}_path"

when /^the (.*) index page for (.*)$/
send "admin_#{$2}_path", format: $1

when /^the last author's posts$/
admin_user_posts_path(User.last)

Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/error.rb
Expand Up @@ -5,7 +5,7 @@ module ActiveAdmin
class AccessDenied < StandardError
attr_reader :user, :action, :subject

def initialize(user, action, subject)
def initialize(user, action, subject = nil)
@user, @action, @subject = user, action, subject

super()
Expand Down
13 changes: 13 additions & 0 deletions lib/active_admin/resource_controller.rb
Expand Up @@ -21,6 +21,7 @@ class ResourceController < BaseController
include Scoping
include Streaming
include Sidebars
include ViewHelpers::DownloadFormatLinksHelper
extend ResourceClassMethods

def self.active_admin_config=(config)
Expand All @@ -45,7 +46,19 @@ def self.inherited(base)
def renderer_for(action)
active_admin_namespace.view_factory["#{action}_page"]
end

helper_method :renderer_for

def restrict_format_access!
unless request.format.html?
presenter = active_admin_config.get_page_presenter(:index)
download_formats = (presenter || {}).fetch(:download_links, active_admin_config.namespace.download_links)
unless build_download_formats(download_formats).include?(request.format.symbol)
raise ActiveAdmin::AccessDenied.new(current_active_admin_user, :index)
end
end
end

before_filter :restrict_format_access!, only: [:index, :show]

This comment has been minimized.

Copy link
@agustinf

agustinf Mar 21, 2017

I think this line breaks any installation that uses Rails 5.x.x because rails/rails@9d62e04

This comment has been minimized.

Copy link
@varyonic

varyonic Mar 22, 2017

Contributor

@agustinf Yes, see branch rails-5-1 for work in progress.

end
end

0 comments on commit 9f83674

Please sign in to comment.