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

AO3-6761 Check admin roles in TagWranglingsController #4937

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
41 changes: 22 additions & 19 deletions app/controllers/tag_wranglings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ class TagWranglingsController < ApplicationController

def index
@counts = tag_counts_per_category
unless params[:show].blank?
raise "Redshirt: Attempted to constantize invalid class initialize tag_wranglings_controller_index #{params[:show].classify}" unless Tag::USER_DEFINED.include?(params[:show].classify)
authorize :wrangling, :read_access? if logged_in_as_admin?
return if params[:show].blank?

params[:sort_column] = 'created_at' if !valid_sort_column(params[:sort_column], 'tag')
params[:sort_direction] = 'ASC' if !valid_sort_direction(params[:sort_direction])
raise "Redshirt: Attempted to constantize invalid class initialize tag_wranglings_controller_index #{params[:show].classify}" unless Tag::USER_DEFINED.include?(params[:show].classify)

if params[:show] == "fandoms"
@media_names = Media.by_name.pluck(:name)
@page_subtitle = ts("fandoms")
end
params[:sort_column] = "created_at" unless valid_sort_column(params[:sort_column], "tag")
params[:sort_direction] = "ASC" unless valid_sort_direction(params[:sort_direction])

type = params[:show].singularize.capitalize
@tags = TagQuery.new({
type: type,
in_use: true,
unwrangleable: false,
unwrangled: true,
sort_column: params[:sort_column],
sort_direction: params[:sort_direction],
page: params[:page],
per_page: ArchiveConfig.ITEMS_PER_PAGE
}).search_results
if params[:show] == "fandoms"
@media_names = Media.by_name.pluck(:name)
@page_subtitle = t(".page_subtitle")
end

type = params[:show].singularize.capitalize
@tags = TagQuery.new({
type: type,
in_use: true,
unwrangleable: false,
unwrangled: true,
sort_column: params[:sort_column],
sort_direction: params[:sort_direction],
page: params[:page],
per_page: ArchiveConfig.ITEMS_PER_PAGE
}).search_results
end

def wrangle
authorize :wrangling, :full_access? if logged_in_as_admin?

params[:page] = '1' if params[:page].blank?
params[:sort_column] = 'name' if !valid_sort_column(params[:sort_column], 'tag')
params[:sort_direction] = 'ASC' if !valid_sort_direction(params[:sort_direction])
Expand Down
5 changes: 5 additions & 0 deletions app/policies/wrangling_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

class WranglingPolicy < ApplicationPolicy
FULL_ACCESS_ROLES = %w[superadmin tag_wrangling].freeze
READ_ACCESS_ROLES = (FULL_ACCESS_ROLES + %w[policy_and_abuse]).freeze

def full_access?
user_has_roles?(FULL_ACCESS_ROLES)
end

def read_access?
user_has_roles?(READ_ACCESS_ROLES)
end

alias create? full_access?
alias destroy? full_access?
alias show? full_access?
Expand Down
7 changes: 4 additions & 3 deletions app/views/admin/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<% if policy(AdminBlacklistedEmail).index? %>
<li><%= link_to t(".nav.banned_emails"), admin_blacklisted_emails_path %></li>
<% end %>

<% if policy(ModeratedWork).index? %>
<li><%= link_to t(".nav.spam"), admin_spam_index_path %></li>
<% end %>
Expand All @@ -58,8 +58,9 @@
</ul>
</li>
<% end %>
<li><%= link_to t(".nav.wrangling"), tag_wranglings_path %></li>

<% if policy(:wrangling).full_access? %>
<li><%= link_to t(".nav.wrangling"), tag_wranglings_path %></li>
<% end %>
<% if policy(Locale).index? %>
<li><%= link_to t(".nav.locales"), locales_path %></li>
<% end %>
Expand Down
93 changes: 58 additions & 35 deletions app/views/tag_wranglings/_wrangler_dashboard.html.erb
Original file line number Diff line number Diff line change
@@ -1,39 +1,62 @@
<div id="dashboard" role="navigation region" class="tag wrangling region">
<ul class="navigation actions">
<% if current_user.is_a?(User) %>
<li><%= span_if_current(ts('Wrangling Home'), tag_wrangler_path(current_user)) %></li>
<% if (logged_in_as_admin? && policy(:wrangling).read_access?) || current_user.try(:is_tag_wrangler?) || @counts %>
<div id="dashboard" role="navigation region" class="tag wrangling region">
<% if (logged_in_as_admin? && policy(:wrangling).read_access?) || current_user.try(:is_tag_wrangler?) %>
<ul class="navigation actions">
<% if current_user.is_a?(User) %>
<li><%= span_if_current(t(".wrangling_home"), tag_wrangler_path(current_user)) %></li>
<% end %>
<li><%= span_if_current(t(".wrangling_tools"), tag_wranglings_path, current_page?(tag_wranglings_path) && params[:show].blank?) %></li>
<% if policy(:wrangling).full_access? || current_user.try(:is_tag_wrangler?) %>
<li><%= span_if_current(t(".wranglers"), tag_wranglers_path) %></li>
<% end %>
<li><%= span_if_current(t(".search_tags"), search_tags_path) %></li>
<% if policy(:wrangling).full_access? || current_user.try(:is_tag_wrangler?) %>
<li><%= span_if_current(t(".new_tag"), new_tag_path) %></li>
<% end %>
</ul>
<% end %>
<li><%= span_if_current(ts('Wrangling Tools'), tag_wranglings_path) %></li>
<li><%= span_if_current(ts('Wranglers'), tag_wranglers_path) %></li>
<li><%= span_if_current(ts('Search Tags'), search_tags_path) %></li>
<li><%= span_if_current(ts('New Tag'), new_tag_path) %></li>
</ul>

<% if @counts %>
<ul class="navigation actions">
<% if @tag && @uses %>
<% @uses.each do |key| %>
<% if key == 'Works' || key == 'Bookmarks' %>
<li><%= span_if_current "#{key} (#{@counts[key]})", { controller: key.downcase.to_sym, action: :index, tag_id: @tag } %></li>
<% elsif key == 'External Works' %>
<li><%= span_if_current "#{key} (#{@counts[key]})", { controller: :bookmarks, action: :index, tag_id: @tag } %></li>
<% else %>
<li><span><%= "#{key} (#{@counts[key]})" %></span></li>
<% if @counts %>
<ul class="navigation actions">
<% if @tag && @uses %>
<% @uses.each do |key| %>
<% if key == "Works" || key == "Bookmarks" %>
<li>
<%= span_if_current t(".use_type_and_count", use_type: t(".use_type.#{key.downcase}"), count: @counts[key]),
{ controller: key.downcase.to_sym, action: :index, tag_id: @tag } %>
</li>
<% elsif key == "External Works" %>
<li>
<%= span_if_current t(".use_type_and_count", use_type: t(".use_type.external_works"), count: @counts[key]),
{ controller: :bookmarks, action: :index, tag_id: @tag } %>
</li>
<% else %>
<li>
<span>
<%= t(".use_type_and_count",
use_type: t(".use_type.#{key.downcase.gsub(' ', '_')}"),
count: @counts[key]) %>
</span>
</li>
<% end %>
<% end %>
<% elsif @tag && @tag.child_types %>
<% @tag.child_types.each do |tag_type| %>
<li>
<%= span_if_current t(".tag_type_and_count",
tag_type: t(".tag_type.#{tag_type.downcase.gsub(' ', '_')}"),
count: @counts[tag_type.underscore.pluralize.to_sym]),
url_for(show: tag_type.underscore.pluralize, id: @tag) %>
</li>
<% end %>
<% else %>
<li><%= span_if_current t(".fandoms_by_media", count: @counts[:fandoms]), tag_wranglings_path(show: "fandoms") %></li>
<li><%= span_if_current t(".characters_by_fandom", count: @counts[:characters]), tag_wranglings_path(show: "characters") %></li>
<li><%= span_if_current t(".relationships_by_fandom", count: @counts[:relationships]), tag_wranglings_path(show: "relationships") %></li>
<li><%= span_if_current t(".freeforms_by_fandom", count: @counts[:freeforms]), tag_wranglings_path(show: "freeforms") %></li>
<li><%= span_if_current t(".unsorted_tags", count: @counts[:UnsortedTag]), unsorted_tags_path %></li>
<% end %>
<% elsif @tag && @tag.child_types %>
<% @tag.child_types.each do |tag_type| %>
<li>
<%= span_if_current tag_type.pluralize + " (#{@counts[tag_type.underscore.pluralize.to_sym]})", url_for(show: tag_type.underscore.pluralize, id: @tag) %>
</li>
<% end %>
<% else %>
<li><%= span_if_current ts("Fandoms by media (%{count})", count: @counts[:fandoms]), tag_wranglings_path(show: "fandoms") %></li>
<li><%= span_if_current ts("Characters by fandom (%{count})", count: @counts[:characters]), tag_wranglings_path(show: "characters") %></li>
<li><%= span_if_current ts("Relationships by fandom (%{count})", count: @counts[:relationships]), tag_wranglings_path(show: "relationships") %></li>
<li><%= span_if_current ts("Freeforms by fandom (%{count})", count: @counts[:freeforms]), tag_wranglings_path(show: "freeforms") %></li>
<li><%= span_if_current(ts("Unsorted Tags (%{count})", count: @counts[:UnsortedTag]), unsorted_tags_path) %></li>
<% end %>
</ul>
<% end %>
</div>
</ul>
<% end %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions config/locales/controllers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ en:
muted: You have muted the user %{name}.
destroy:
unmuted: You have unmuted the user %{name}.
tag_wranglings:
index:
page_subtitle: fandoms
users:
passwords:
create:
Expand Down
28 changes: 28 additions & 0 deletions config/locales/views/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,34 @@ en:
show:
last_wrangled_html: "%{wrangler_login} last wrangled at %{time}."
tags_wrangled_csv: Tags Wrangled (CSV)
tag_wranglings:
wrangler_dashboard:
characters_by_fandom: Characters by fandom (%{count})
fandoms_by_media: Fandoms by media (%{count})
freeforms_by_fandom: Freeforms by fandom (%{count})
new_tag: New Tag
relationships_by_fandom: Relationships by fandom (%{count})
search_tags: Search Tags
tag_type:
character: Characters
fandom: Fandoms
freeform: Freeforms
merger: Mergers
relationship: Relationships
subtag: SubTags
tag_type_and_count: "%{tag_type} (%{count})"
unsorted_tags: Unsorted Tags (%{count})
use_type:
bookmarks: Bookmarks
drafts: Drafts
external_works: External Works
private_bookmarks: Private Bookmarks
taggings_count: Taggings Count
works: Works
use_type_and_count: "%{use_type} (%{count})"
wranglers: Wranglers
wrangling_home: Wrangling Home
wrangling_tools: Wrangling Tools
tags:
index:
about:
Expand Down
84 changes: 84 additions & 0 deletions features/tags_and_wrangling/tag_wrangling_admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,87 @@ Feature: Tag wrangling
Then I should see "Tags Wrangled (CSV)"
When I follow "Tags Wrangled (CSV)"
Then I should download a csv file with the header row "Name Last Updated Type Merger Fandoms Unwrangleable"

Scenario Outline: Authorized admins have the tag wrangling item in the admin navbar

Given I am logged in as a "<role>" admin
Then I should see "Tag Wrangling" within "ul.admin.primary.navigation"

Examples:
| role |
| superadmin |
| tag_wrangling |

Scenario Outline: Unauthorized admins do not have the tag wrangling item in the admin navbar

Given I am logged in as a "<role>" admin
Then I should not see "Tag Wrangling" within "ul.admin.primary.navigation"

Examples:
| role |
| board |
| board_assistants_team |
| communications |
| development_and_membership |
| docs |
| elections |
| legal |
| translation |
| support |
| policy_and_abuse |
| open_doors |

Scenario Outline: Fully-authorized admins get the wrangling dashboard sidebar

Given I am logged in as a "<role>" admin
And basic tags
When I go to the tags page
Then I should see "Wrangling Tools" within "div#dashboard"
And I should see "Wranglers" within "div#dashboard"
And I should see "Search Tags" within "div#dashboard"
And I should see "New Tag" within "div#dashboard"
But I should not see "Wrangling Home" within "div#dashboard"

Examples:
| role |
| superadmin |
| tag_wrangling |

Scenario Outline: Read-authorized admins get a partial wrangling dashboard sidebar

Given I am logged in as a "<role>" admin
And basic tags
When I go to the tags page
Then I should see "Wrangling Tools" within "div#dashboard"
And I should see "Search Tags" within "div#dashboard"
But I should not see "Wranglers" within "div#dashboard"
And I should not see "New Tag" within "div#dashboard"
And I should not see "Wrangling Home" within "div#dashboard"

Examples:
| role |
| policy_and_abuse |

Scenario Outline: Unauthorized admins do not get the wrangling dashboard sidebar

Given I am logged in as a "<role>" admin
And basic tags
When I go to the tags page
Then I should not see "Wrangling Tools"
And I should not see "Wranglers"
And I should not see "Search Tags"
And I should not see "New Tag"
And I should not see "Wrangling Home"

Examples:
| role |
| board |
| board_assistants_team |
| communications |
| development_and_membership |
| docs |
| elections |
| legal |
| translation |
| support |
| open_doors |
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Scenario: relationship wrangling - syns, mergers, characters, autocompletes
And a canonical character "Zoe Washburne"
And a canonical character "Jack Harkness"
And a canonical character "Ianto Jones"
And I am logged in as an admin
And I am logged in as a "tag_wrangling" admin
And I follow "Tag Wrangling"

# create a new canonical relationship from tag wrangling interface
Expand Down
Loading
Loading