Skip to content

Commit 9fddb0a

Browse files
authored
AO3-6204 Allow certain admins to remove non-default orphan pseuds from orphaned works (#5022)
* AO3-6204 Allow certain admins to remove non-default orphan pseuds from works * AO3-6204 ERB lint * AO3-6204 Pressed TAB but not alt * AO3-6204 Add test for attempting to remove a normal users pseud * AO3-6204 Add test for attempting to remove a normal users pseud * AO3-6204 Review fixes * AO3-6204 Move authorized admin shared example to shared file * AO3-6204 Fix test
1 parent 35b3366 commit 9fddb0a

File tree

16 files changed

+381
-75
lines changed

16 files changed

+381
-75
lines changed

app/controllers/admin/user_creations_controller.rb

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Admin::UserCreationsController < Admin::BaseController
2-
before_action :get_creation
2+
before_action :get_creation, only: [:hide, :set_spam, :destroy]
33
before_action :can_be_marked_as_spam, only: [:set_spam]
44

55
def get_creation
@@ -62,4 +62,40 @@ def destroy
6262
redirect_to works_path
6363
end
6464
end
65+
66+
def confirm_remove_pseud
67+
@work = authorize Work.find(params[:id])
68+
69+
@orphan_pseuds = @work.orphan_pseuds
70+
return unless @orphan_pseuds.empty?
71+
72+
flash[:error] = t(".must_have_orphan_pseuds")
73+
redirect_to work_path(@work) and return
74+
end
75+
76+
def remove_pseud
77+
@work = authorize Work.find(params[:id])
78+
79+
pseuds = params[:pseuds]
80+
orphan_account = User.orphan_account
81+
if pseuds.blank?
82+
pseuds = @work.orphan_pseuds
83+
if pseuds.length > 1
84+
flash[:error] = t(".must_select_pseud")
85+
redirect_to work_path(@work) and return
86+
end
87+
else
88+
pseuds = Pseud.find(pseuds).select { |p| p.user_id == orphan_account.id }
89+
end
90+
91+
orphan_pseud = orphan_account.default_pseud
92+
pseuds.each do |pseud|
93+
pseud.change_ownership(@work, orphan_pseud)
94+
end
95+
unless pseuds.empty?
96+
AdminActivity.log_action(current_admin, @work, action: "remove orphan_account pseuds")
97+
flash[:notice] = t(".success", pseuds: pseuds.map(&:byline).to_sentence, count: pseuds.length)
98+
end
99+
redirect_to work_path(@work)
100+
end
65101
end

app/controllers/orphans_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create
3333
use_default = params[:use_default] == "true"
3434
Creatorship.orphan(@pseuds, @orphans, use_default)
3535
flash[:notice] = ts("Orphaning was successful.")
36-
redirect_to(current_user)
36+
redirect_to user_path(current_user)
3737
end
3838

3939
protected

app/models/concerns/creatable.rb

+5
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,9 @@ def user_is_owner_or_invited?(user)
220220
return false unless user.is_a?(User)
221221
creatorships.for_user(user).exists?
222222
end
223+
224+
# Get all orphan_account pseuds that (co-)created this creatable, excluding the orphan_account's default_pseud
225+
def orphan_pseuds
226+
self.pseuds.where(user_id: User.orphan_account.id, is_default: false)
227+
end
223228
end

app/policies/work_policy.rb

+6
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ def destroy?
1919
def set_spam?
2020
user_has_roles?(%w[superadmin policy_and_abuse])
2121
end
22+
23+
def remove_pseud?
24+
user_has_roles?(%w[superadmin support policy_and_abuse])
25+
end
26+
27+
alias confirm_remove_pseud? remove_pseud?
2228
end

app/views/admin/_admin_options.html.erb

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@
3737
<% end %>
3838
</li>
3939
<% end %>
40+
<% if policy(@work).remove_pseud? %>
41+
<% orphan_pseuds = @work.orphan_pseuds %>
42+
<% if orphan_pseuds.length == 1 %>
43+
<li>
44+
<%= link_to t(".remove_pseud"),
45+
confirm_remove_pseud_admin_user_creation_path(@work),
46+
data: { confirm: t(".remove_pseud_confirmation") } %>
47+
</li>
48+
<% elsif orphan_pseuds.length > 1 %>
49+
<li>
50+
<%= link_to t(".remove_pseud"), confirm_remove_pseud_admin_user_creation_path(@work) %>
51+
</li>
52+
<% end %>
53+
<% end %>
4054
<% end %>
4155
<% if item.class == ExternalWork %>
4256
<% if policy(item).edit? %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!--Descriptive page name, messages and instructions-->
2+
<h2 class="heading"><%= t(".page_heading") %></h2>
3+
4+
<!--main content-->
5+
<%= form_with url: remove_pseud_admin_user_creation_path(@work), method: :put, class: "simple destroy" do |f| %>
6+
<% if @orphan_pseuds.length > 1 %>
7+
<p class="caution notice">
8+
<%= t(".choose") %>
9+
</p>
10+
<ul>
11+
<%= f.collection_check_boxes :pseuds, @orphan_pseuds, :id, :byline, { include_hidden: false } do |builder| %>
12+
<li>
13+
<%= builder.check_box %>
14+
<%= builder.label %>
15+
</li>
16+
<% end %>
17+
</ul>
18+
<p class="actions">
19+
<%= f.submit t(".submit_multiple") %>
20+
</p>
21+
<% else %>
22+
<p class="caution notice">
23+
<%= t(".caution") %>
24+
</p>
25+
<p class="actions">
26+
<%= f.submit t(".submit_one") %>
27+
</p>
28+
<% end %>
29+
<% end %>
30+
<!--/content-->

config/locales/controllers/en.yml

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ en:
1515
admin_users:
1616
destroy_user_creations:
1717
success: All creations by user %{login} have been deleted.
18+
user_creations:
19+
confirm_remove_pseud:
20+
must_have_orphan_pseuds: Sorry, this action is only available for works by orphan_account pseuds.
21+
remove_pseud:
22+
must_select_pseud: You must select which orphan_account pseud to remove.
23+
success:
24+
one: Successfully removed pseud %{pseuds} from this work.
25+
other: Successfully removed pseuds %{pseuds} from this work.
1826
archive_faqs:
1927
create:
2028
success: Archive FAQ was successfully created.

config/locales/views/en.yml

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ en:
119119
work: Hide Work
120120
landmark: Admin Actions
121121
not_spam: Mark Not Spam
122+
remove_pseud: Remove Pseud
123+
remove_pseud_confirmation: Are you sure you want to remove the creator's pseud from this work?
122124
spam: Mark As Spam
123125
unhide:
124126
bookmark: Make Bookmark Visible
@@ -352,6 +354,13 @@ en:
352354
update: Update
353355
update:
354356
success: Archive settings were successfully updated.
357+
user_creations:
358+
confirm_remove_pseud:
359+
caution: Are you sure you want to remove the creator's pseud from this work?
360+
choose: Please choose which creators' pseuds you would like to remove from this work.
361+
page_heading: Remove Pseud
362+
submit_multiple: Remove Pseuds
363+
submit_one: Yes, Remove Pseud
355364
admin_posts:
356365
admin_post_form:
357366
comment_permissions:

config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@
197197
member do
198198
put :hide
199199
put :set_spam
200+
get :confirm_remove_pseud
201+
put :remove_pseud
200202
end
201203
end
202204
resources :users, controller: "admin_users", only: [:index, :show] do

features/admins/admin_works.feature

+113-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Feature: Admin Actions for Works, Comments, Series, Bookmarks
6262
Scenario Outline: Can unhide works
6363
Given I am logged in as "regular_user"
6464
And I post the work "ToS Violation"
65-
When I am logged in as a "policy_and_abuse" admin
65+
When I am logged in as a "<role>" admin
6666
And I view the work "ToS Violation"
6767
And I follow "Hide Work"
6868
And all indexing jobs have been run
@@ -541,3 +541,115 @@ Feature: Admin Actions for Works, Comments, Series, Bookmarks
541541
| superadmin |
542542
| legal |
543543
| policy_and_abuse |
544+
545+
Scenario Outline: Certain admins can remove orphan_account pseuds from works
546+
Given I have an orphan account
547+
And I am logged in as "Leaver"
548+
And I post the work "Bye"
549+
And I orphan and keep my pseud on the work "Bye"
550+
When I am logged in as a "<role>" admin
551+
And I view the work "Bye"
552+
Then I should see "Remove Pseud"
553+
When I follow "Remove Pseud"
554+
Then I should see "Are you sure you want to remove the creator's pseud from this work?"
555+
# Expire byline cache
556+
When it is currently 1 second from now
557+
And I press "Yes, Remove Pseud"
558+
Then I should see "Successfully removed pseud Leaver (orphan_account) from this work."
559+
And I should see "orphan_account" within ".byline"
560+
But I should not see "Leaver" within ".byline"
561+
562+
Examples:
563+
| role |
564+
| superadmin |
565+
| policy_and_abuse |
566+
| support |
567+
568+
@javascript
569+
Scenario Outline: Removing orphan_account pseuds from works with JavaScript shows a confirmation pop-up instead of a page
570+
Given I have an orphan account
571+
And I am logged in as "Leaver"
572+
And I post the work "Bye"
573+
And I orphan and keep my pseud on the work "Bye"
574+
When I am logged in as a "<role>" admin
575+
And I view the work "Bye"
576+
Then I should see "Remove Pseud"
577+
# Expire byline cache
578+
When it is currently 1 second from now
579+
And I follow "Remove Pseud"
580+
And I confirm I want to remove the pseud
581+
Then I should see "Successfully removed pseud Leaver (orphan_account) from this work."
582+
And I should see "orphan_account" within ".byline"
583+
But I should not see "Leaver" within ".byline"
584+
585+
Examples:
586+
| role |
587+
| superadmin |
588+
| policy_and_abuse |
589+
| support |
590+
591+
Scenario: When removing orphan_account pseuds from a work with multiple pseuds admins choose which pseud to remove
592+
Given I have an orphan account
593+
And I am logged in as "Leaver"
594+
And I post the work "Bye"
595+
And I add the co-author "Another" to the work "Bye"
596+
And it is currently 1 second from now
597+
And I add the co-author "Third" to the work "Bye"
598+
And I orphan and keep my pseud on the work "Bye"
599+
And I am logged in as "Another"
600+
And I orphan and keep my pseud on the work "Bye"
601+
And I am logged in as "Third"
602+
And I orphan and keep my pseud on the work "Bye"
603+
When I am logged in as a "policy_and_abuse" admin
604+
And I view the work "Bye"
605+
Then I should see "Remove Pseud"
606+
When I follow "Remove Pseud"
607+
Then I should see "Please choose which creators' pseuds you would like to remove from this work."
608+
And I should see "Third (orphan_account)"
609+
When I check "Leaver (orphan_account)"
610+
And I check "Another (orphan_account)"
611+
# Expire byline cache
612+
And it is currently 1 second from now
613+
And I press "Remove Pseud"
614+
Then I should see "Successfully removed pseuds Leaver (orphan_account) and Another (orphan_account) from this work."
615+
And I should see "orphan_account, " within ".byline"
616+
And I should see "Third (orphan_account)" within ".byline"
617+
But I should not see "Leaver" within ".byline"
618+
And I should not see "Another" within ".byline"
619+
When I go to the admin-activities page
620+
Then I should see 1 admin activity log entry
621+
And I should see "remove orphan_account pseuds"
622+
623+
Scenario: The Remove pseud option is only shown on orphaned works with non-default pseuds
624+
Given I have an orphan account
625+
And I am logged in as "Leaver"
626+
And I post the work "Hey"
627+
And I post the work "Bye"
628+
And I orphan and take my pseud off the work "Bye"
629+
When I am logged in as a "superadmin" admin
630+
And I view the work "Hey"
631+
Then I should not see "Remove Pseud"
632+
When I view the work "Bye"
633+
Then I should not see "Remove Pseud"
634+
635+
Scenario Outline: The Remove pseud option is not shown to admins who don't have permissions to remove pseuds
636+
Given I have an orphan account
637+
And I am logged in as "Leaver"
638+
And I post the work "Bye"
639+
And I orphan and keep my pseud on the work "Bye"
640+
When I am logged in as a "<role>" admin
641+
And I view the work "Bye"
642+
Then I should not see "Remove Pseud"
643+
644+
Examples:
645+
| role |
646+
| board |
647+
| board_assistants_team |
648+
| communications |
649+
| development_and_membership |
650+
| docs |
651+
| elections |
652+
| legal |
653+
| translation |
654+
| tag_wrangling |
655+
| open_doors |

features/step_definitions/admin_steps.rb

+4
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@
349349
step "it is currently #{days} days from now"
350350
end
351351

352+
When "I confirm I want to remove the pseud" do
353+
expect(page.accept_alert).to eq("Are you sure you want to remove the creator's pseud from this work?") if @javascript
354+
end
355+
352356
### THEN
353357

354358
Then (/^the translation information should still be filled in$/) do

public/javascripts/application.js

+6
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ function prepareDeleteLinks() {
407407
$j(this).attr("data-method", "delete");
408408
});
409409

410+
// Removing non-default orphan_account pseuds from works
411+
$j('a[href$="/confirm_remove_pseud"][data-confirm]').each(function() {
412+
this.href = this.href.replace(/\/confirm_remove_pseud$/, "/remove_pseud");
413+
$j(this).attr("data-method", "put");
414+
});
415+
410416
// For purging assignments in gift exchanges. This is only on one page and easy to
411417
// check, so don't worry about adding a fallback data-confirm message.
412418
$j('a[href$="/confirm_purge"][data-confirm]').each(function() {

0 commit comments

Comments
 (0)