Skip to content

Commit a5c1f04

Browse files
authored
AO3-7010 AO3-7017 Fix error when trying to access subpage of hidden unrevealed work (otwcode#5219)
* AO3-7010 Fix error when trying to access subpage of hidden unrevealed work * AO3-7017 Restrict access to subpages for draft and restricted works
1 parent 41b5db5 commit a5c1f04

7 files changed

Lines changed: 68 additions & 4 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,11 @@ def check_permission_to_wrangle
508508
end
509509

510510
# Checks if user is allowed to see related page if parent item is hidden or in unrevealed collection
511+
# Checks if user is logged in if parent item is restricted
511512
def check_visibility_for(parent)
512-
# Only admins and the owner can see related pages on something hidden by an admin.
513-
logged_in_as_admin? || current_user_owns?(parent) || access_denied(redirect: root_path) if parent.try(:hidden_by_admin)
513+
return if logged_in_as_admin? || current_user_owns?(parent) # Admins and the owner can see all related pages
514514

515-
# Only admins and the owner can see related pages on unrevealed works.
516-
logged_in_as_admin? || current_user_owns?(parent) || access_denied(redirect: root_path) if parent.try(:in_unrevealed_collection)
515+
access_denied(redirect: root_path) if parent.try(:hidden_by_admin) || parent.try(:in_unrevealed_collection) || (parent.respond_to?(:visible?) && !parent.visible?)
517516
end
518517

519518
public

spec/controllers/bookmarks_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ def it_redirects_to_user_login
369369

370370
include_examples "denies access for work that isn't visible to user"
371371
end
372+
373+
context "denies access for restricted work to guest" do
374+
let(:work) { create(:work, restricted: true) }
375+
376+
it "redirects with an error" do
377+
get :index, params: { work_id: work }
378+
it_redirects_to_with_error(root_path, "Sorry, you don't have permission to access the page you were trying to reach. Please log in.")
379+
end
380+
end
372381
end
373382

374383
describe "show" do

spec/controllers/collections_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919

2020
include_examples "denies access for work that isn't visible to user"
2121
end
22+
23+
context "denies access for restricted work to guest" do
24+
let(:work) { create(:work, restricted: true) }
25+
26+
it "redirects with an error" do
27+
get :index, params: { work_id: work }
28+
it_redirects_to_with_error(root_path, "Sorry, you don't have permission to access the page you were trying to reach. Please log in.")
29+
end
30+
end
2231
end
2332

2433
describe "GET #show" do

spec/controllers/comments/default_rails_actions_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,5 +1002,22 @@
10021002

10031003
it_redirects_to_simple("/404")
10041004
end
1005+
1006+
context "denies access for work that isn't visible to user" do
1007+
subject { get :index, params: { work_id: work } }
1008+
let(:success) { expect(response).to render_template("index") }
1009+
let(:success_admin) { success }
1010+
1011+
include_examples "denies access for work that isn't visible to user"
1012+
end
1013+
1014+
context "denies access for restricted work to guest" do
1015+
let(:work) { create(:work, restricted: true) }
1016+
1017+
it "redirects with an error" do
1018+
get :index, params: { work_id: work }
1019+
it_redirects_to(new_user_session_path(restricted_commenting: true))
1020+
end
1021+
end
10051022
end
10061023
end

spec/controllers/kudos_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,14 @@
238238

239239
include_examples "denies access for work that isn't visible to user"
240240
end
241+
242+
context "denies access for restricted work to guest" do
243+
let(:work) { create(:work, restricted: true) }
244+
245+
it "redirects with an error" do
246+
get :index, params: { work_id: work }
247+
it_redirects_to_with_error(root_path, "Sorry, you don't have permission to access the page you were trying to reach. Please log in.")
248+
end
249+
end
241250
end
242251
end

spec/controllers/works/works_controller_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@
1414

1515
include_examples "denies access for work that isn't visible to user"
1616
end
17+
18+
context "denies access for restricted work to guest" do
19+
let(:work) { create(:work, restricted: true) }
20+
21+
it "redirects with an error" do
22+
get :navigate, params: { id: work.id }
23+
it_redirects_to_with_error(root_path, "Sorry, you don't have permission to access the page you were trying to reach. Please log in.")
24+
end
25+
end
1726
end
1827
end

spec/support/shared_examples/access_shared_examples.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@
6767

6868
include_examples "denies access to random user"
6969
end
70+
71+
context "hidden and unrevealed work" do
72+
let(:work) { create(:work, authors: [creator.default_pseud], collections: [create(:unrevealed_collection)], hidden_by_admin: true) }
73+
74+
include_examples "denies access to random user"
75+
end
76+
77+
context "draft work" do
78+
let(:work) { create(:draft, authors: [creator.default_pseud]) }
79+
80+
include_examples "denies access to random user"
81+
end
7082
end

0 commit comments

Comments
 (0)