Skip to content

Commit d60aea6

Browse files
authored
Merge pull request #11476 from alphagov/fix-access-limiting-bug
Raise validation error if badly access-limiting draft on-create [WHIT-3458]
2 parents 60d8f7d + 51c4e72 commit d60aea6

5 files changed

Lines changed: 41 additions & 2 deletions

File tree

app/controllers/admin/editions_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def enforce_permissions!
3232
when "choose_type", "change_type", "change_type_preview", "apply_change_type"
3333
enforce_permission!(:create, edition_class || Edition)
3434
when "create"
35-
enforce_permission!(:create, @edition)
35+
enforce_permission!(:create, @edition) if @edition.persisted?
3636
when "edit", "update", "revise", "diff", "update_bypass_id", "features"
3737
enforce_permission!(:update, @edition)
3838
when "destroy", "confirm_destroy"
@@ -95,6 +95,7 @@ def create
9595
updater.perform!
9696
redirect_to show_or_edit_path, saved_confirmation_notice
9797
else
98+
flash.now[:alert] = updater.failure_reason
9899
build_edition_dependencies
99100
render :new
100101
end

app/services/draft_edition_updater.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def should_check_current_user_will_retain_access?
2828
end
2929

3030
def access_limit_excludes_current_user?
31+
# Some users may not necessarily belong to an organisation. We should fail-closed for them.
32+
return true unless @options[:current_user].organisation
33+
3134
edition.organisation_association_enabled? && edition.edition_organisations.map(&:organisation_id).exclude?(@options[:current_user].organisation.id)
3235
end
3336
end

app/views/admin/errors/forbidden.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="govuk-grid-row govuk-!-margin-bottom-0">
66
<div class="govuk-grid-column-two-thirds">
77
<p class="govuk-body">You do not have permission to access this page.</p>
8-
<% if @edition && can?(:perform_administrative_tasks, Edition) %>
8+
<% if @edition&.persisted? && can?(:perform_administrative_tasks, Edition) %>
99
<p class="govuk-body">
1010
<a href="<%= edit_access_limited_admin_edition_path(@edition) %>" class="govuk-link">
1111
As an administrator, you can update the access permissions of the page.

features/admin-limit-access-editions.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ Feature: Viewing most recent editions in admin
1515
And I view the current edition of document "Road accidents"
1616
Then I am told I do not have permissions to access this page
1717
And I should see a link to edit the access
18+
19+
Scenario: Attempting to access limit a new (unpersisted) document against a different org
20+
Given I am an editor
21+
When I begin drafting a new document
22+
And I set the Lead organisation to an org I am not in
23+
And I check the "Limit access to publishers from organisations associated with this document before you publish" box
24+
When I click "Save"
25+
Then I should see the validation error "Access can only be limited by users belonging to an organisation tagged to the document"

features/step_definitions/admin_limit_access_steps.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,30 @@
1414
Then(/^I should see a link to edit the access/) do
1515
expect(page).to have_link("As an administrator, you can update the access permissions of the page.", href: edit_access_limited_admin_edition_path(@edition))
1616
end
17+
18+
When(/^I begin drafting a new document$/) do
19+
@org = create(:organisation, name: "Some made up org")
20+
21+
visit "/government/admin/publications/new"
22+
select "Policy paper", from: "edition_publication_type_id"
23+
fill_in "edition[title]", with: "Test publication"
24+
fill_in "edition[summary]", with: "Test summary"
25+
fill_in "edition[body]", with: "Test body"
26+
check "Applies to all UK nations"
27+
end
28+
29+
When(/^I set the Lead organisation to an org I am not in$/) do
30+
select @org.name, from: "edition_lead_organisation_ids_1"
31+
end
32+
33+
When(/^I check the "(.+)" box$/) do |box_name|
34+
check box_name
35+
end
36+
37+
When(/^I click "(.+)"$/) do |button_name|
38+
click_on button_name
39+
end
40+
41+
Then(/^I should see the validation error "(.+)"$/) do |string|
42+
expect(page).to have_content string
43+
end

0 commit comments

Comments
 (0)