Skip to content

Commit

Permalink
Fix admins being disallowed from tags pages
Browse files Browse the repository at this point in the history
  • Loading branch information
WelpThatWorked committed Dec 23, 2024
1 parent 6caa157 commit f72c5fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
16 changes: 7 additions & 9 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,23 @@ def search
flash_search_warnings(@tags)
end

# if user is admin with view access or Tag Wrangler, show them details about the tag
# if user is not logged in or a regular user, show them
# 1. the works, if the tag had been wrangled and we can redirect them to works using it or its canonical merger
# 2. the tag, the works and the bookmarks using it, if the tag is unwrangled (because we can't redirect them
# to the works controller)
def show
authorize :wrangling, :read_access? if logged_in_as_admin?

@page_subtitle = @tag.name
if @tag.is_a?(Banned) && !logged_in_as_admin?
if @tag.is_a?(Banned)
if !logged_in_as_admin?
flash[:error] = ts('Please log in as admin')
redirect_to(tag_wranglings_path) && return
elsif !policy(:wrangling).read_access?
flash[:error] = ts('Sorry, only an authorized admin can access the page you were trying to reach.')

Check warning on line 66 in app/controllers/tags_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Use 2 (not 0) spaces for indentation. Raw Output: app/controllers/tags_controller.rb:66:7: C: Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.

Check warning on line 66 in app/controllers/tags_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/tags_controller.rb:66:23: 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

Check warning on line 66 in app/controllers/tags_controller.rb

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping. Raw Output: app/controllers/tags_controller.rb:66:26: C: Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
redirect_to(root_path) && return
end
end
# if tag is NOT wrangled, prepare to show works and bookmarks that are using it
if !@tag.canonical && !@tag.merger
if logged_in? # current_user.is_a?User

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

View workflow job for this annotation

GitHub Actions / Rubocop

[rubocop] reported by reviewdog 🐶 Use the return of the conditional for variable assignment and comparison. Raw Output: app/controllers/tags_controller.rb:72:7: C: Style/ConditionalAssignment: Use the return of the conditional for variable assignment and comparison.
@works = @tag.works.visible_to_registered_user.paginate(page: params[:page])
elsif logged_in_as_admin?
@works = @tag.works.visible_to_owner.paginate(page: params[:page])
@works = @tag.works.visible_to_admin.paginate(page: params[:page])
else
@works = @tag.works.visible_to_all.paginate(page: params[:page])
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/tags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def tag_category_name(tag_type)

# Should the current user be able to access tag wrangling pages?
def can_wrangle?
logged_in_as_admin? || (current_user.is_a?(User) && current_user.is_tag_wrangler?)
policy(:wrangling).read_access? || (current_user.is_a?(User) && current_user.is_tag_wrangler?)
end

# Determines whether or not to display warnings for a creation
Expand Down
27 changes: 26 additions & 1 deletion spec/controllers/tags_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,35 @@
end

describe "show" do
context "displays the tag information page" do
let(:tag) { create(:tag) }

subject { get :show, params: { id: tag.name } }
let(:success) do
expect(response).to have_http_status(:success)
end

it "for guests" do
subject
success
end

it "for users" do
fake_login
subject
success
end

it "for admins" do
fake_login_admin(create(:admin))
subject
success
end
end
context "when showing a banned tag" do
let(:tag) { create(:banned) }

subject { get :edit, params: { id: tag.name } }
subject { get :show, params: { id: tag.name } }
let(:success) do
expect(response).to have_http_status(:success)
end
Expand Down

0 comments on commit f72c5fe

Please sign in to comment.