Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict access to publications based on the user's organisation #2354

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ def require_editor_permissions
flash[:danger] = "You do not have correct editor permissions for this action."
redirect_to edition_path(resource)
end

def require_user_accessibility_to_edition(edition)
render plain: "404 Not Found", status: :not_found unless edition.is_accessible_to?(current_user)
end
end
3 changes: 3 additions & 0 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class EditionsController < InheritedResources::Base

defaults resource_class: Edition, collection_name: "editions", instance_name: "resource"
before_action :setup_view_paths, except: %i[index]
before_action except: %i[index] do
require_user_accessibility_to_edition(@resource)
end

helper_method :locale_to_language

Expand Down
3 changes: 3 additions & 0 deletions app/controllers/legacy_editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class LegacyEditionsController < InheritedResources::Base
actions :create, :update, :destroy
defaults resource_class: Edition, collection_name: "editions", instance_name: "resource"
before_action :setup_view_paths, except: %i[index new create]
before_action except: %i[index create] do
require_user_accessibility_to_edition(@resource)
end
before_action only: %i[update duplicate progress review destroy admin] do
require_editor_permissions
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def index
content_type_filter = filter_params_hash[:content_type_filter]
title_filter = filter_params_hash[:title_filter]
@presenter = FilteredEditionsPresenter.new(
current_user,
states_filter: sanitised_states_filter_params,
assigned_to_filter: assignee_filter,
content_type_filter:,
Expand Down
17 changes: 17 additions & 0 deletions app/models/edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,20 @@ class ResurrectionError < RuntimeError

field :auth_bypass_id, type: String, default: -> { SecureRandom.uuid }

field :owning_org_slugs, type: Array, default: []

belongs_to :assigned_to, class_name: "User", optional: true

embeds_many :link_check_reports

scope :accessible_to,
lambda { |user|
return all unless Flipflop.enabled?(:restrict_access_by_org)
return all if user.gds_editor?

where(owning_org_slugs: user.organisation_slug)
}

# state_machine comes from Workflow
state_machine.states.map(&:name).each do |state|
scope state, -> { where(state:) }
Expand Down Expand Up @@ -503,6 +513,13 @@ def paths
paths
end

def is_accessible_to?(user)
return true unless Flipflop.enabled?(:restrict_access_by_org)
return true if user.gds_editor?

owning_org_slugs.include?(user.organisation_slug)
end

private

def base_field_keys
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ def welsh_editor?
def has_editor_permissions?(resource)
govuk_editor? || (welsh_editor? && resource.artefact.welsh?)
end

def gds_editor?
organisation_slug == "government-digital-service"
end
end
6 changes: 4 additions & 2 deletions app/presenters/filtered_editions_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class FilteredEditionsPresenter
ITEMS_PER_PAGE = 20

def initialize(states_filter: [], assigned_to_filter: nil, content_type_filter: nil, title_filter: nil, page: nil)
def initialize(user, states_filter: [], assigned_to_filter: nil, content_type_filter: nil, title_filter: nil, page: nil)
@user = user
@states_filter = states_filter || []
@assigned_to_filter = assigned_to_filter
@content_type_filter = content_type_filter
Expand Down Expand Up @@ -62,6 +63,7 @@ def editions
result = apply_states_filter(result)
result = apply_assigned_to_filter(result)
result = apply_title_filter(result)
result = result.accessible_to(user)
result = result.where.not(_type: "PopularLinksEdition")
result.order_by(%w[updated_at desc]).page(@page).per(ITEMS_PER_PAGE)
end
Expand Down Expand Up @@ -129,5 +131,5 @@ def apply_title_filter(editions)
editions.title_contains(title_filter)
end

attr_reader :states_filter, :assigned_to_filter, :content_type_filter, :title_filter, :page
attr_reader :user, :states_filter, :assigned_to_filter, :content_type_filter, :title_filter, :page
end
4 changes: 4 additions & 0 deletions config/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
feature :design_system_edit,
default: false,
description: "Update the publications edit page to use the GOV.UK Design System"

feature :restrict_access_by_org,
default: false,
description: "Restrict access to editions based on the user's org and which org(s) own the edition"
end
55 changes: 52 additions & 3 deletions test/functional/editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class EditionsControllerTest < ActionController::TestCase
login_as_stub_user
stub_linkables
stub_holidays_used_by_fact_check

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, false)
end

context "#template_folder_for" do
Expand All @@ -17,7 +20,7 @@ class EditionsControllerTest < ActionController::TestCase
end

context "#index" do
should "editions index redirects to root" do
should "redirect to root" do
get :index
assert_response :redirect
assert_redirected_to root_path
Expand All @@ -36,12 +39,12 @@ class EditionsControllerTest < ActionController::TestCase
@guide = GuideEdition.create!(title: "test", slug: "test2", panopticon_id: artefact.id)
end

should "requesting a publication that doesn't exist returns a 404" do
should "return a 404 when requesting a publication that doesn't exist" do
get :show, params: { id: "4e663834e2ba80480a0000e6" }
assert_response :not_found
end

should "we can view a guide" do
should "return a view for the requested guide" do
get :show, params: { id: @guide.id }
assert_response :success
assert_not_nil assigns(:resource)
Expand All @@ -64,4 +67,50 @@ class EditionsControllerTest < ActionController::TestCase
assert_equal EditionsController.new.method(:metadata).super_method.name, :show
end
end

context "when 'restrict_access_by_org' feature toggle is disabled" do
syed-ali-tw marked this conversation as resolved.
Show resolved Hide resolved
%i[show metadata history admin linking unpublish].each do |action|
context "##{action}" do
setup do
@edition = FactoryBot.create(:edition, owning_org_slugs: %w[org-two])
end

should "return a 200 when requesting an edition owned by a different organisation" do
login_as(FactoryBot.create(:user, organisation_slug: "org-one"))

get action, params: { id: @edition.id }

assert_response :ok
end
end
end
end

context "when 'restrict_access_by_org' feature toggle is enabled" do
syed-ali-tw marked this conversation as resolved.
Show resolved Hide resolved
setup do
test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, true)
end

teardown do
syed-ali-tw marked this conversation as resolved.
Show resolved Hide resolved
test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, false)
end

%i[show metadata history admin linking unpublish].each do |action|
context "##{action}" do
setup do
@edition = FactoryBot.create(:edition, owning_org_slugs: %w[org-two])
end

should "return a 404 when requesting an edition owned by a different organisation" do
login_as(FactoryBot.create(:user, organisation_slug: "org-one"))

get action, params: { id: @edition.id }

assert_response :not_found
end
end
end
end
end
49 changes: 49 additions & 0 deletions test/functional/legacy_editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class LegacyEditionsControllerTest < ActionController::TestCase
login_as_stub_user
stub_linkables
stub_holidays_used_by_fact_check

test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, false)
end

context "#create" do
Expand Down Expand Up @@ -1304,4 +1307,50 @@ class LegacyEditionsControllerTest < ActionController::TestCase
end
end
end

context "when 'restrict_access_by_org' feature toggle is disabled" do
%i[metadata history].each do |action|
context "##{action}" do
setup do
@edition = FactoryBot.create(:edition, owning_org_slugs: %w[org-two])
end

should "return a 200 when requesting an edition owned by a different organisation" do
login_as(FactoryBot.create(:user, organisation_slug: "org-one"))

get action, params: { id: @edition.id }

assert_response :ok
end
end
end
end

context "when 'restrict_access_by_org' feature toggle is enabled" do
setup do
test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, true)
end

teardown do
syed-ali-tw marked this conversation as resolved.
Show resolved Hide resolved
test_strategy = Flipflop::FeatureSet.current.test!
test_strategy.switch!(:restrict_access_by_org, false)
end

%i[show metadata history admin unpublish duplicate update linking update_tagging update_related_external_links review destroy progress diff process_unpublish diagram].each do |action|
context "##{action}" do
setup do
@edition = FactoryBot.create(:edition, owning_org_slugs: %w[org-two])
end

should "return a 404 when requesting an edition owned by a different organisation" do
login_as(FactoryBot.create(:user, :govuk_editor, organisation_slug: "org-one"))

get action, params: { id: @edition.id }

assert_response :not_found
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/functional/root_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class RootControllerTest < ActionController::TestCase
should "ignore unrecognised filter states" do
FilteredEditionsPresenter
.expects(:new)
.with(has_entry(:states_filter, %w[draft]))
.with(anything, has_entry(:states_filter, %w[draft]))
.returns(stub(
editions: Kaminari.paginate_array([]).page(1),
available_users: [],
Expand Down
Loading
Loading