Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,36 @@
@collection = Collection.find_by(name: params[:collection_id]) if params[:collection_id]
end

def privileged_collection_admin?
policy(Collection).access?
end

def users_or_privileged_collection_admins_only
return if logged_in? || privileged_collection_admin?

logged_in_as_admin? ? admin_only_access_denied : access_denied
end

def collection_maintainers_only
logged_in? && @collection && @collection.user_is_maintainer?(current_user) || access_denied
end

def collection_maintainers_or_privileged_admins_only
return if (logged_in? && @collection && @collection.user_is_maintainer?(current_user)) || privileged_collection_admin?

Check warning on line 354 in app/controllers/application_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Wrap expressions with varying precedence with parentheses to avoid ambiguity. Raw Output: app/controllers/application_controller.rb:354:5: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
logged_in_as_admin? ? admin_only_access_denied : access_denied
end

def collection_owners_only
logged_in? && @collection && @collection.user_is_owner?(current_user) || access_denied
end

def collection_owners_or_privileged_admins_only
return if (logged_in? && @collection && @collection.user_is_owner?(current_user)) || privileged_collection_admin?

Check warning on line 364 in app/controllers/application_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Wrap expressions with varying precedence with parentheses to avoid ambiguity. Raw Output: app/controllers/application_controller.rb:364:5: W: Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
logged_in_as_admin? ? admin_only_access_denied : access_denied
end

def not_allowed(fallback=nil)
flash[:error] = ts("Sorry, you're not allowed to do that.")
redirect_to (fallback || root_path) rescue redirect_to '/'
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/challenge/gift_exchange_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Challenge::GiftExchangeController < ChallengesController

Check warning on line 2 in app/controllers/challenge/gift_exchange_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Extra empty line detected at class body beginning. Raw Output: app/controllers/challenge/gift_exchange_controller.rb:2:1: C: Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.
before_action :users_only
before_action :users_only, except: [:edit]
before_action :load_collection
before_action :load_challenge, except: [:new, :create]
before_action :collection_owners_only, only: [:new, :create, :edit, :update, :destroy]
before_action :collection_owners_or_privileged_admins_only, only: [:edit]
before_action :collection_owners_only, only: [:new, :create, :update, :destroy]

# ACTIONS

Expand Down
5 changes: 3 additions & 2 deletions app/controllers/challenge/prompt_meme_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Challenge::PromptMemeController < ChallengesController

Check warning on line 2 in app/controllers/challenge/prompt_meme_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Extra empty line detected at class body beginning. Raw Output: app/controllers/challenge/prompt_meme_controller.rb:2:1: C: Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.
before_action :users_only
before_action :users_only, except: [:edit]
before_action :load_collection
before_action :load_challenge, except: [:new, :create]
before_action :collection_owners_only, only: [:new, :create, :edit, :update, :destroy]
before_action :collection_owners_or_privileged_admins_only, only: [:edit]
before_action :collection_owners_only, only: [:new, :create, :update, :destroy]

# ACTIONS

Expand Down
7 changes: 4 additions & 3 deletions app/controllers/challenge_assignments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ChallengeAssignmentsController < ApplicationController
before_action :users_only
before_action :users_only, except: [:index, :show]
before_action :users_or_privileged_collection_admins_only, only: [:index, :show]

before_action :load_collection, except: [:index]
before_action :load_challenge, except: [:index]
Expand Down Expand Up @@ -89,7 +90,7 @@ def index
return unless load_collection
@challenge = @collection.challenge if @collection
signup_open and return unless !@challenge.signup_open
access_denied and return unless @challenge.user_allowed_to_see_assignments?(current_user)
access_denied and return unless @challenge.user_allowed_to_see_assignments?(current_user) || privileged_collection_admin?

# we temporarily are ordering by requesting pseud to avoid left join
@assignments = case
Expand All @@ -108,7 +109,7 @@ def index
end

def show
unless @challenge.user_allowed_to_see_assignments?(current_user) || @challenge_assignment.offering_pseud.user == current_user
unless @challenge.user_allowed_to_see_assignments?(current_user) || @challenge_assignment.offering_pseud.user == current_user || privileged_collection_admin?
flash[:error] = ts("You aren't allowed to see that assignment!")
redirect_to "/" and return
end
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/challenge_claims_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ChallengeClaimsController < ApplicationController

Check warning on line 2 in app/controllers/challenge_claims_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Extra empty line detected at class body beginning. Raw Output: app/controllers/challenge_claims_controller.rb:2:1: C: Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning.
before_action :users_only
before_action :users_only, except: [:index]
before_action :users_or_privileged_collection_admins_only, only: [:index]
before_action :load_collection, except: [:index]
before_action :collection_owners_only, except: [:index, :show, :create, :destroy]
before_action :load_claim_from_id, only: [:show, :destroy]
Expand Down Expand Up @@ -68,17 +69,17 @@
# ACTIONS

def index
if !(@collection = Collection.find_by(name: params[:collection_id])).nil? && @collection.closed? && !@collection.user_is_maintainer?(current_user)
if !(@collection = Collection.find_by(name: params[:collection_id])).nil? && @collection.closed? && !@collection.user_is_maintainer?(current_user) && !privileged_collection_admin?

Check warning on line 72 in app/controllers/challenge_claims_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`. Raw Output: app/controllers/challenge_claims_controller.rb:72:5: C: Style/IfUnlessModifier: Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
flash[:notice] = ts("This challenge is currently closed to new posts.")
end
if params[:collection_id]
return unless load_collection

@challenge = @collection.challenge
not_allowed(@collection) unless user_scoped? || @challenge.user_allowed_to_see_assignments?(current_user)
not_allowed(@collection) unless user_scoped? || @challenge.user_allowed_to_see_assignments?(current_user) || privileged_collection_admin?

@claims = ChallengeClaim.unposted_in_collection(@collection)
@claims = @claims.where(claiming_user_id: current_user.id) if user_scoped?
@claims = @claims.where(claiming_user_id: current_user.id) if user_scoped? && current_user

# sorting
set_sort_order
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/challenge_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
flash.now[:notice] = ts("Collection could not be found")
redirect_to "/" and return
end
unless @collection.challenge_type == "PromptMeme" || (@collection.challenge_type == "GiftExchange" && @collection.challenge.user_allowed_to_see_requests_summary?(current_user))
flash.now[:notice] = ts("You are not allowed to view the requests summary!")
redirect_to collection_path(@collection) and return
end

return if @collection.challenge_type == "PromptMeme" || privileged_collection_admin?
return if @collection.challenge_type == "GiftExchange" && @collection.challenge.user_allowed_to_see_requests_summary?(current_user)

return admin_only_access_denied if logged_in_as_admin?

flash.now[:notice] = ts("You are not allowed to view the requests summary!")

Check warning on line 16 in app/controllers/challenge_requests_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Prefer Rails built-in `t` helper over `ts` and move the text into the yml file. `ts` is not actually translatable. For more information, refer to https://github.com/otwcode/otwarchive/wiki/Internationalization-(i18n)-Standards Raw Output: app/controllers/challenge_requests_controller.rb:16:26: C: I18n/DeprecatedHelper: Prefer Rails built-in `t` helper over `ts` and move the text into the yml file. `ts` is not actually translatable. For more information, refer to https://github.com/otwcode/otwarchive/wiki/Internationalization-(i18n)-Standards
redirect_to collection_path(@collection) and return
end

def index
Expand Down
10 changes: 6 additions & 4 deletions app/controllers/challenge_signups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class ChallengeSignupsController < ApplicationController
include ExportsHelper

before_action :users_only, except: [:summary]
before_action :users_only, except: [:summary, :index, :show]
before_action :users_or_privileged_collection_admins_only, only: [:index, :show]
before_action :load_collection, except: [:index]
before_action :load_challenge, except: [:index]
before_action :load_signup_from_id, only: [:show, :edit, :update, :destroy, :confirm_delete]
Expand Down Expand Up @@ -45,7 +46,7 @@
end

def maintainer_or_signup_owner_only
not_allowed(@collection) and return unless (@challenge_signup.pseud.user == current_user || @collection.user_is_maintainer?(current_user))
not_allowed(@collection) and return unless (@challenge_signup.pseud.user == current_user || @collection.user_is_maintainer?(current_user) || privileged_collection_admin?)

Check warning on line 49 in app/controllers/challenge_signups_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Don't use parentheses around the condition of an `unless`. Raw Output: app/controllers/challenge_signups_controller.rb:49:48: C: Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an `unless`.
end

def not_signup_owner
Expand Down Expand Up @@ -100,7 +101,7 @@
# see ExportsHelper for export_csv method
respond_to do |format|
format.html {
if @challenge.user_allowed_to_see_signups?(current_user)
if @challenge.user_allowed_to_see_signups?(current_user) || privileged_collection_admin?

Check warning on line 104 in app/controllers/challenge_signups_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Use 2 (not 4) spaces for indentation. Raw Output: app/controllers/challenge_signups_controller.rb:104:7: C: Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
@challenge_signups = @collection.signups.joins(:pseud)
if params[:query]
@query = params[:query]
Expand All @@ -114,7 +115,8 @@
end
}
format.csv {
if (@collection.gift_exchange? && @challenge.user_allowed_to_see_signups?(current_user)) ||
if privileged_collection_admin? ||
(@collection.gift_exchange? && @challenge.user_allowed_to_see_signups?(current_user)) ||
(@collection.prompt_meme? && @collection.user_is_maintainer?(current_user))
csv_data = self.send("#{@challenge.class.name.underscore}_to_csv")
filename = "#{@collection.name}_signups_#{Time.now.strftime('%Y-%m-%d-%H%M')}.csv"
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/collection_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CollectionItemsController < ApplicationController
def index

# TODO: AO3-6507 Refactor to use send instead of case statements.
if @collection && @collection.user_is_maintainer?(current_user)
if @collection && (@collection.user_is_maintainer?(current_user) || privileged_collection_admin?)
@collection_items = @collection.collection_items.include_for_works
@collection_items = case params[:status]
when "approved"
Expand Down Expand Up @@ -39,6 +39,8 @@ def index
@collection_items.unreviewed_by_user
end
else
admin_only_access_denied and return if logged_in_as_admin?

flash[:error] = ts("You don't have permission to see that, sorry!")
redirect_to collections_path and return
end
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/collection_participants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class CollectionParticipantsController < ApplicationController
before_action :users_only
before_action :users_only, except: [:index]
before_action :load_collection
before_action :load_participant, only: [:update, :destroy]
before_action :allowed_to_promote, only: [:update]
before_action :allowed_to_destroy, only: [:destroy]
before_action :has_other_owners, only: [:update, :destroy]
before_action :collection_maintainers_only, only: [:index, :add, :update]
before_action :collection_maintainers_or_privileged_admins_only, only: [:index]
before_action :collection_maintainers_only, only: [:add, :update]

cache_sweeper :collection_sweeper

Expand Down
5 changes: 3 additions & 2 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class CollectionsController < ApplicationController
before_action :users_only, only: [:new, :edit, :create, :update]
before_action :users_only, only: [:new, :create]
before_action :load_collection_from_id, only: [:show, :edit, :update, :destroy, :confirm_delete]
before_action :collection_owners_only, only: [:edit, :update, :destroy, :confirm_delete]
before_action :collection_owners_or_privileged_admins_only, only: [:edit]
before_action :collection_owners_only, only: [:update, :destroy, :confirm_delete]
before_action :check_user_status, only: [:new, :create, :edit, :update, :destroy]
before_action :validate_challenge_type
before_action :check_parent_visible, only: [:index]
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/potential_matches_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class PotentialMatchesController < ApplicationController

before_action :users_only
before_action :load_collection
before_action :collection_maintainers_only
before_action :collection_maintainers_or_privileged_admins_only, only: [:index]
before_action :collection_maintainers_only, except: [:index]
before_action :load_challenge
before_action :check_assignments_not_sent
before_action :check_signup_closed, only: [:generate]
Expand Down
7 changes: 7 additions & 0 deletions app/policies/collection_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CollectionPolicy < ApplicationPolicy
ACCESS_ROLES = %w[support policy_and_abuse superadmin].freeze

def access?
user_has_roles?(ACCESS_ROLES)
end
end
30 changes: 30 additions & 0 deletions spec/controllers/challenge_assignments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,34 @@
end
end
end

describe "admin access to assignments pages" do
authorized_roles = %w[support policy_and_abuse superadmin].freeze
let(:gift_exchange) { create(:gift_exchange, assignments_sent_at: Faker::Time.backward) }
let(:user) { other_user }
Comment thread
not-varram marked this conversation as resolved.

before { fake_logout }
Comment thread
not-varram marked this conversation as resolved.

describe "GET #index" do
subject { get :index, params: { collection_id: collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end

describe "GET #show" do
subject { get :show, params: { id: assignment.id, collection_id: collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:show)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end
end
end
15 changes: 15 additions & 0 deletions spec/controllers/challenge_claims_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@
end
end
end

describe "admin access to claims index" do
authorized_roles = %w[support policy_and_abuse superadmin].freeze
let!(:claim_one) { create(:challenge_claim, collection: collection, claiming_user: create(:user)) }
let!(:claim_two) { create(:challenge_claim, collection: collection, claiming_user: create(:user)) }

subject { get :index, params: { collection_id: collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(assigns(:claims)).to include(claim_one, claim_two)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end
end
19 changes: 17 additions & 2 deletions spec/controllers/challenge_requests_controller_spec.rb
Comment thread
not-varram marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "spec_helper"

describe ChallengeRequestsController, bookmark_search: true, collection_search: true, work_search: true do
describe ChallengeRequestsController do
include LoginMacros
include RedirectExpectationHelper

describe "index" do
context "when there are anonymous prompts" do
context "when there are anonymous prompts", bookmark_search: true, collection_search: true, work_search: true do
render_views

it "does not throw a 500 error if sorting by prompter with an anonymous prompt" do
Expand All @@ -16,5 +16,20 @@
expect(response.status).not_to eq(500)
end
end

context "with gift exchanges where request summary is private" do
authorized_roles = %w[support policy_and_abuse superadmin].freeze
let(:challenge) { create(:gift_exchange, requests_summary_visible: false) }
let(:collection) { create(:collection, challenge: challenge) }

subject { get :index, params: { collection_id: collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end
end
end
35 changes: 35 additions & 0 deletions spec/controllers/challenge_signups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,39 @@
end
end
end

describe "admin access to signups pages" do
authorized_roles = %w[support policy_and_abuse superadmin].freeze

describe "GET #index" do
subject { get :index, params: { collection_id: closed_collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end

describe "GET #show" do
subject { get :show, params: { id: closed_signup.id, collection_id: closed_collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:show)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end

it "allows support admins to download CSV" do
fake_login_admin(create(:support_admin))

get :index, params: { collection_id: closed_collection.name, format: :csv }

expect(response).to have_http_status(:success)
expect(response.content_type).to include("text/csv")
end
end
end
13 changes: 13 additions & 0 deletions spec/controllers/collection_items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,17 @@
end
end
end

describe "admin access to manage items" do
authorized_roles = %w[support policy_and_abuse superadmin].freeze

subject { get :index, params: { collection_id: collection.name } }

let(:success) do
expect(response).to have_http_status(:success)
expect(response).to render_template(:index)
end

it_behaves_like "an action only authorized admins can access", authorized_roles: authorized_roles
end
end
Loading
Loading