A platform for community discussion. Free, open, simple.
FEATURE: Allow rejected user details to be scrubbed (#31987)
Gary Pendergastb4cdc39e5193
diff --git a/app/assets/javascripts/admin/addon/components/modal/scrub-rejected-user.gjs b/app/assets/javascripts/admin/addon/components/modal/scrub-rejected-user.gjsnew file mode 100644index 00000000000..42d5aec7920--- /dev/null+++ b/app/assets/javascripts/admin/addon/components/modal/scrub-rejected-user.gjs@@ -0,0 +1,63 @@+import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { on } from "@ember/modifier";+import { action } from "@ember/object";+import { isEmpty } from "@ember/utils";+import DButton from "discourse/components/d-button";+import DModal from "discourse/components/d-modal";+import TextField from "discourse/components/text-field";+import { i18n } from "discourse-i18n";++export default class ScrubRejectedUserModal extends Component {+ @tracked isScrubbing = false;+ @tracked scrubReason = "";++ @action+ async confirmScrub() {+ this.isScrubbing = true;+ await this.args.model.confirmScrub(this.scrubReason);+ this.args.closeModal();+ }++ get scrubButtonDisabled() {+ return isEmpty(this.scrubReason);+ }++ <template>+ <DModal+ @bodyClass="scrub-rejected-user"+ class="admin-scrub-rejected-user-modal"+ @title={{i18n "review.user.scrub_record.confirm_title"}}+ @closeModal={{if this.isScrubbing null @closeModal}}+ >+ <:body>+ <p>{{i18n "review.user.scrub_record.confirm_body"}}</p>+ <label class="scrub-reason-title" for="scrub-reason">{{i18n+ "review.user.scrub_record.reason_title"+ }}</label>++ <TextField+ class="scrub-reason"+ id="scrub-reason"+ @placeholderKey="review.user.scrub_record.reason_placeholder"+ {{on "input" (action (mut this.scrubReason) value="target.value")}}+ />+ </:body>+ <:footer>+ <DButton+ class="btn btn-danger"+ @action={{this.confirmScrub}}+ @isLoading={{this.isScrubbing}}+ @disabled={{this.scrubButtonDisabled}}+ @label="review.user.scrub_record.confirm_button"+ />+ <DButton+ class="btn btn-default"+ @action={{@closeModal}}+ @disabled={{this.isScrubbing}}+ @label="review.user.scrub_record.cancel_button"+ />+ </:footer>+ </DModal>+ </template>+}diff --git a/app/assets/javascripts/admin/addon/models/staff-action-log.js b/app/assets/javascripts/admin/addon/models/staff-action-log.jsindex 9be53840687..c2350fa3d2e 100644--- a/app/assets/javascripts/admin/addon/models/staff-action-log.js+++ b/app/assets/javascripts/admin/addon/models/staff-action-log.js@@ -99,7 +99,7 @@ export default class StaffActionLog extends RestModel {@discourseComputed("details")useModalForDetails(details) {- return details && details.length > 100;+ return details && (details.length > 100 || details.includes("\n"));}@discourseComputed("action_name")diff --git a/app/assets/javascripts/discourse/app/components/reviewable-item.js b/app/assets/javascripts/discourse/app/components/reviewable-item.jsindex f15c226dbdb..3d36662ed55 100644--- a/app/assets/javascripts/discourse/app/components/reviewable-item.js+++ b/app/assets/javascripts/discourse/app/components/reviewable-item.js@@ -296,25 +296,9 @@ export default class ReviewableItem extends Component {let actionMethod =this[`client${classify(performableAction.client_action)}`];if (actionMethod) {- if (!this.reviewable.claimed_by) {- const claim = this.store.createRecord("reviewable-claimed-topic");-- try {- await claim.save({- topic_id: this.reviewable.topic.id,- automatic: true,- });- this.reviewable.set("claimed_by", {- user: this.currentUser,- automatic: true,- });- } catch (e) {- popupAjaxError(e);- return;- }+ if (await this.#claimReviewable()) {+ return actionMethod.call(this, reviewable, performAction);}-- return actionMethod.call(this, reviewable, performAction);} else {// eslint-disable-next-line no-consoleconsole.error(@@ -418,6 +402,33 @@ export default class ReviewableItem extends Component {}}+ async #claimReviewable() {+ if (!this.reviewable.topic) {+ // We can't claim a reviewable without a topic, so treat it as claimed+ return true;+ }++ if (!this.reviewable.claimed_by) {+ const claim = this.store.createRecord("reviewable-claimed-topic");++ try {+ await claim.save({+ topic_id: this.reviewable.topic.id,+ automatic: true,+ });+ this.reviewable.set("claimed_by", {+ user: this.currentUser,+ automatic: true,+ });+ } catch (e) {+ popupAjaxError(e);+ return false;+ }+ }++ return this.reviewable.claimed_by?.user?.id === this.currentUser.id;+ }+@actionexplainReviewable(reviewable, event) {event.preventDefault();@@ -474,7 +485,7 @@ export default class ReviewableItem extends Component {}@action- perform(performableAction) {+ async perform(performableAction) {if (this.updating) {return;}@@ -486,18 +497,22 @@ export default class ReviewableItem extends Component {: actionModalClassMap[performableAction.server_action];if (message) {- this.dialog.confirm({- message,- didConfirm: () => this._performConfirmed(performableAction),- });+ if (await this.#claimReviewable()) {+ this.dialog.confirm({+ message,+ didConfirm: () => this._performConfirmed(performableAction),+ });+ }} else if (actionModalClass) {- this.modal.show(actionModalClass, {- model: {- reviewable: this.reviewable,- performConfirmed: this._performConfirmed,- action: performableAction,- },- });+ if (await this.#claimReviewable()) {+ this.modal.show(actionModalClass, {+ model: {+ reviewable: this.reviewable,+ performConfirmed: this._performConfirmed,+ action: performableAction,+ },+ });+ }} else {return this._performConfirmed(performableAction);}diff --git a/app/assets/javascripts/discourse/app/components/reviewable-user.gjs b/app/assets/javascripts/discourse/app/components/reviewable-user.gjsindex f242612a1c0..bd01781ef81 100644--- a/app/assets/javascripts/discourse/app/components/reviewable-user.gjs+++ b/app/assets/javascripts/discourse/app/components/reviewable-user.gjs@@ -1,89 +1,169 @@import Component from "@ember/component";import { concat } from "@ember/helper";+import { on } from "@ember/modifier";+import { action } from "@ember/object";+import { service } from "@ember/service";import ReviewableField from "discourse/components/reviewable-field";import getUrl from "discourse/helpers/get-url";-import discourseComputed from "discourse/lib/decorators";+import rawDate from "discourse/helpers/raw-date";+import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import discourseComputed, { bind } from "discourse/lib/decorators";+import { REJECTED } from "discourse/models/reviewable";import { i18n } from "discourse-i18n";+import ScrubRejectedUserModal from "admin/components/modal/scrub-rejected-user";export default class ReviewableUser extends Component {+ @service currentUser;+ @service modal;+ @service store;+@discourseComputed("reviewable.user_fields")userFields(fields) {return this.site.collectUserFields(fields);}+ @discourseComputed("reviewable.status", "currentUser", "isScrubbed")+ canScrubRejectedUser(status, currentUser, isScrubbed) {+ return status === REJECTED && currentUser.admin && !isScrubbed;+ }++ @discourseComputed("reviewable.payload")+ isScrubbed(payload) {+ return !!payload?.scrubbed_by;+ }++ @action+ showScrubRejectedUserModal() {+ this.modal.show(ScrubRejectedUserModal, {+ model: {+ confirmScrub: this.scrubRejectedUser,+ },+ });+ }++ @bind+ async scrubRejectedUser(reason) {+ try {+ await ajax({+ url: `/review/${this.reviewable.id}/scrub`,+ type: "PUT",+ data: { reason },+ });+ this.store.find("reviewable", this.reviewable.id);+ } catch (e) {+ popupAjaxError(e);+ }+ }+<template><div class="reviewable-user-info"><div class="reviewable-user-fields">- <div class="reviewable-user-details username">- <div class="name">{{i18n "review.user.username"}}</div>- <div class="value">- {{#if this.reviewable.link_admin}}- <a- href={{getUrl- (concat- "/admin/users/"- this.reviewable.user_id- "/"- this.reviewable.payload.username- )- }}- >- {{this.reviewable.payload.username}}- </a>- {{else}}- {{this.reviewable.payload.username}}- {{/if}}- </div>- </div>-- <ReviewableField- @classes="reviewable-user-details name"- @name={{i18n "review.user.name"}}- @value={{this.reviewable.payload.name}}- />-- <ReviewableField- @classes="reviewable-user-details email"- @name={{i18n "review.user.email"}}- @value={{this.reviewable.payload.email}}- />-- <ReviewableField- @classes="reviewable-user-details bio"- @name={{i18n "review.user.bio"}}- @value={{this.reviewable.payload.bio}}- />-- {{#if this.reviewable.payload.website}}- <div class="reviewable-user-details website">- <div class="name">{{i18n "review.user.website"}}</div>+ {{#if this.isScrubbed}}+ <ReviewableField+ @classes="reviewable-user-details reject-reason"+ @name={{i18n "review.user.scrubbed_reject_reason"}}+ @value={{this.reviewable.reject_reason}}+ />++ <ReviewableField+ @classes="reviewable-user-details scrubbed-by"+ @name={{i18n "review.user.scrubbed_by"}}+ @value={{this.reviewable.payload.scrubbed_by}}+ />++ <ReviewableField+ @classes="reviewable-user-details scrubbed-reason"+ @name={{i18n "review.user.scrubbed_reason"}}+ @value={{this.reviewable.payload.scrubbed_reason}}+ />++ <ReviewableField+ @classes="reviewable-user-details scrubbed-at"+ @name={{i18n "review.user.scrubbed_at"}}+ @value={{rawDate this.reviewable.payload.scrubbed_at}}+ />+ {{else}}+ <div class="reviewable-user-details username">+ <div class="name">{{i18n "review.user.username"}}</div><div class="value">- <a- href={{this.reviewable.payload.website}}- target="_blank"- rel="noopener noreferrer"- >{{this.reviewable.payload.website}}</a>+ {{#if this.reviewable.link_admin}}+ <a+ href={{getUrl+ (concat+ "/admin/users/"+ this.reviewable.user_id+ "/"+ this.reviewable.payload.username+ )+ }}+ >+ {{this.reviewable.payload.username}}+ </a>+ {{else}}+ {{this.reviewable.payload.username}}+ {{/if}}</div></div>- {{/if}}+ <ReviewableField+ @classes="reviewable-user-details name"+ @name={{i18n "review.user.name"}}+ @value={{this.reviewable.payload.name}}+ />++ <ReviewableField+ @classes="reviewable-user-details email"+ @name={{i18n "review.user.email"}}+ @value={{this.reviewable.payload.email}}+ />- <ReviewableField- @classes="reviewable-user-details reject-reason"- @name={{i18n "review.user.reject_reason"}}- @value={{this.reviewable.reject_reason}}- />+ <ReviewableField+ @classes="reviewable-user-details bio"+ @name={{i18n "review.user.bio"}}+ @value={{this.reviewable.payload.bio}}+ />++ {{#if this.reviewable.payload.website}}+ <div class="reviewable-user-details website">+ <div class="name">{{i18n "review.user.website"}}</div>+ <div class="value">+ <a+ href={{this.reviewable.payload.website}}+ target="_blank"+ rel="noopener noreferrer"+ >{{this.reviewable.payload.website}}</a>+ </div>+ </div>+ {{/if}}- {{#each this.userFields as |f|}}<ReviewableField- @classes="reviewable-user-details user-field"- @name={{f.name}}- @value={{f.value}}- @tagName=""+ @classes="reviewable-user-details reject-reason"+ @name={{i18n "review.user.reject_reason"}}+ @value={{this.reviewable.reject_reason}}/>- {{/each}}++ {{#each this.userFields as |f|}}+ <ReviewableField+ @classes="reviewable-user-details user-field"+ @name={{f.name}}+ @value={{f.value}}+ @tagName=""+ />+ {{/each}}+ {{/if}}</div>{{yield}}</div>+ {{#if this.canScrubRejectedUser}}+ <div class="scrub-rejected-user">+ <button+ class="btn btn-danger"+ {{on "click" this.showScrubRejectedUserModal}}+ >+ {{i18n "review.user.scrub_record.button"}}+ </button>+ </div>+ {{/if}}</template>}diff --git a/app/assets/javascripts/discourse/tests/unit/models/staff-action-log-test.js b/app/assets/javascripts/discourse/tests/unit/models/staff-action-log-test.jsindex 520eb75c219..88594e07d39 100644--- a/app/assets/javascripts/discourse/tests/unit/models/staff-action-log-test.js+++ b/app/assets/javascripts/discourse/tests/unit/models/staff-action-log-test.js@@ -12,4 +12,32 @@ module("Unit | Model | staff-action-log", function (hooks) {"can be created without arguments");});++ test("useModalForDetails - should return true when details is > 100 characters long", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const model = store.createRecord("staff-action-log", {+ details: "a".repeat(101),+ });+ assert.true(+ model.useModalForDetails,+ "should return true when details is > 100 characters long"+ );++ model.set("details", "a".repeat(100));+ assert.false(+ model.useModalForDetails,+ "should return false when details is <= 100 characters long"+ );+ });++ test("useModalForDetails - should return true when details includes a \n", function (assert) {+ const store = getOwner(this).lookup("service:store");+ const model = store.createRecord("staff-action-log", {+ details: "important information\nthat doesn't exceed the limit",+ });+ assert.true(+ model.useModalForDetails,+ "should return true when details includes a newline character"+ );+ });});diff --git a/app/assets/stylesheets/common/base/reviewables.scss b/app/assets/stylesheets/common/base/reviewables.scssindex 944ed301660..f661265f0e6 100644--- a/app/assets/stylesheets/common/base/reviewables.scss+++ b/app/assets/stylesheets/common/base/reviewables.scss@@ -113,6 +113,10 @@}}+.scrub-rejected-user {+ width: 100%;+}+.no-review {margin-top: 1em;}diff --git a/app/controllers/reviewables_controller.rb b/app/controllers/reviewables_controller.rbindex 93d0c3290c7..c9dce5b3adc 100644--- a/app/controllers/reviewables_controller.rb+++ b/app/controllers/reviewables_controller.rb@@ -196,6 +196,26 @@ class ReviewablesController < ApplicationControllerrender json: success_jsonend+ def scrub+ raise Discourse::InvalidAccess unless @guardian.is_admin?++ params.require(:reason)++ reviewable =+ Reviewable.find_by(+ id: params[:reviewable_id],+ status: Reviewable.statuses[:rejected],+ type: Reviewable.scrubbable_types,+ )+ raise Discourse::NotFound if reviewable.blank?++ raise Discourse::InvalidAccess if reviewable.payload["scrubbed_by"].present?++ reviewable.scrub(params[:reason], @guardian)++ render json: success_json+ end+def updatereviewable = find_reviewableif error = claim_error?(reviewable)diff --git a/app/models/reviewable.rb b/app/models/reviewable.rbindex 8245f80d82e..4139e75041f 100644--- a/app/models/reviewable.rb+++ b/app/models/reviewable.rb@@ -77,6 +77,10 @@ class Reviewable < ActiveRecord::Base[ReviewableFlaggedPost, ReviewableQueuedPost, ReviewableUser, ReviewablePost]end+ def self.scrubbable_types+ [ReviewableUser]+ end+def self.sti_namesself.types.map(&:sti_name)enddiff --git a/app/models/reviewable_user.rb b/app/models/reviewable_user.rbindex 4087c6c5d80..4b77d20d9b2 100644--- a/app/models/reviewable_user.rb+++ b/app/models/reviewable_user.rb@@ -36,6 +36,40 @@ class ReviewableUser < Reviewablecreate_result(:success, :approved)end+ def scrub(reason, guardian)+ self.class.transaction do+ scrubbed_at = Time.zone.now+ # We need to scrub the UserHistory record for when this user was deleted, as well as this reviewable's payload+ UserHistory+ .where(action: UserHistory.actions[:delete_user])+ .where("details LIKE :query", query: "%\nusername: #{payload["username"]}\n%")+ .where(created_at: (updated_at - 10.minutes)..(updated_at + 10.minutes))+ .update_all(+ details:+ I18n.t(+ "user.destroy_reasons.reviewable_details_scrubbed",+ staff: guardian.current_user.username,+ reason: reason,+ timestamp: scrubbed_at,+ ),+ ip_address: nil,+ )++ self.payload = {+ scrubbed_by: guardian.current_user.username,+ scrubbed_reason: reason,+ scrubbed_at:,+ }+ self.save!++ result = create_result(:success)++ notify_users(result, guardian)++ result+ end+ end+def perform_delete_user(performed_by, args)# We'll delete the user if we canif target.present?diff --git a/app/serializers/reviewable_user_serializer.rb b/app/serializers/reviewable_user_serializer.rbindex 1033b6d8b2f..3880a9b063b 100644--- a/app/serializers/reviewable_user_serializer.rb+++ b/app/serializers/reviewable_user_serializer.rb@@ -3,7 +3,16 @@class ReviewableUserSerializer < ReviewableSerializerattributes :link_admin, :user_fields, :reject_reason- payload_attributes(:username, :email, :name, :bio, :website)+ payload_attributes(+ :username,+ :email,+ :name,+ :bio,+ :website,+ :scrubbed_by,+ :scrubbed_reason,+ :scrubbed_at,+ )def link_adminscope.is_staff? && object.target.present?diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 0ba9d7f54f3..df31db500bc 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -670,6 +670,18 @@ en:name: "Name"fields: "Fields"reject_reason: "Reason"+ scrubbed_by: "Record scrubbed by"+ scrubbed_reason: "Reason"+ scrubbed_at: "Scrubbed at"+ scrubbed_reject_reason: "User rejected reason"+ scrub_record:+ button: Scrub record+ confirm_title: Remove personal data+ confirm_body: "Are you SURE you want to scrub this review queue record? All personal information (e.g. username, email, IP address) will be permanently removed from this record."+ reason_title: "A reason for scrubbing this record is required."+ reason_placeholder: "Reason..."+ confirm_button: "Confirm removal"+ cancel_button: "Cancel"user_percentage:summary:diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex a63a2013644..01ab6f9eda2 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3093,6 +3093,7 @@ en:inactive_user: "Inactive user"reviewable_reject_auto: "Auto handle queued reviewables"reviewable_reject: "Reviewable user rejected"+ reviewable_details_scrubbed: "User details scrubbed by %{staff}\n\nReason: %{reason}\n\nTimestamp: %{timestamp}"email_in_spam_header: "User's first email was flagged as spam"already_silenced: "User was already silenced by %{staff} %{time_ago}."already_suspended: "User was already suspended by %{staff} %{time_ago}."diff --git a/config/routes.rb b/config/routes.rbindex 6b6393c64f9..a9ea6f99c13 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -495,6 +495,10 @@ Discourse::Application.routes.draw doaction_id: /[a-z\_]+/,}put "review/:reviewable_id" => "reviewables#update", :constraints => { reviewable_id: /\d+/ }+ put "review/:reviewable_id/scrub" => "reviewables#scrub",+ :constraints => {+ reviewable_id: /\d+/,+ }delete "review/:reviewable_id" => "reviewables#destroy",:constraints => {reviewable_id: /\d+/,diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 0c33118a612..d1f292afb66 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -891,6 +891,9 @@ class Plugin::Instancedef register_reviewable_type(reviewable_type_class)return unless reviewable_type_class < Reviewableextend_list_method(Reviewable, :types, reviewable_type_class)+ if reviewable_type_class.method_defined?(:scrub)+ extend_list_method(Reviewable, :scrubbable_types, reviewable_type_class)+ endenddef extend_list_method(klass, method, new_attributes)diff --git a/spec/lib/plugin/instance_spec.rb b/spec/lib/plugin/instance_spec.rbindex 67ccbd4ea61..da32ebdd35b 100644--- a/spec/lib/plugin/instance_spec.rb+++ b/spec/lib/plugin/instance_spec.rb@@ -631,6 +631,15 @@ TEXTMyReviewableend+ let(:new_scrubbable_type) do+ class MyScrubbableReviewable < Reviewable+ def scrub(reason, guardian)+ # scrub logic+ end+ end+ MyScrubbableReviewable+ end+it "adds the provided class to the existing types" doexpect { register_reviewable_type }.to change { Reviewable.types.size }.by(1)expect(Reviewable.types).to include(new_type)@@ -641,6 +650,16 @@ TEXTexpect(Reviewable.source_for(new_type)).to eq("discourse-sample-plugin")end+ it "isn't listed as a scrubbable type if it doesn't have a scrub method" do+ register_reviewable_type+ expect(Reviewable.scrubbable_types).not_to include(new_type)+ end++ it "is listed as a scrubbable type if it has a scrub method" do+ plugin_instance.register_reviewable_type(new_scrubbable_type)+ expect(Reviewable.scrubbable_types).to include(new_scrubbable_type)+ end+context "when the plugin is disabled" dobefore doregister_reviewable_typediff --git a/spec/models/reviewable_user_spec.rb b/spec/models/reviewable_user_spec.rbindex 68fcfad236c..99ca11aebfb 100644--- a/spec/models/reviewable_user_spec.rb+++ b/spec/models/reviewable_user_spec.rb@@ -243,5 +243,90 @@ RSpec.describe ReviewableUser, type: :model doexpect(event[:event_name]).to eq(:user_approved)expect(event[:params].first).to eq(user)end++ describe "#scrub" do+ it "scrubs the user history record" do+ UserDestroyer.new(admin).destroy(user)+ reviewable.reload++ history = UserHistory.where(action: UserHistory.actions[:delete_user])+ expect(history.count).to eq(1)+ expect(history.first.details).to include("username: #{user.username}")+ expect(history.first.ip_address).not_to be_blank++ reviewable.scrub("reason", Guardian.new(admin))+ reviewable.reload+ history.reload++ expect(history.first.details).to include("User details scrubbed by #{admin.username}")+ expect(history.first.details).to include("reason")+ expect(history.first.details).to include(+ "Timestamp: #{Time.zone.parse(reviewable.payload["scrubbed_at"])}",+ )++ expect(history.first.details).not_to include(user.username)+ expect(history.first.ip_address).to be_blank+ end++ it "doesn't scrub older user history records for the same username" do+ # Create a history record with the same username but older than the reviewable+ UserHistory.create!(+ action: UserHistory.actions[:delete_user],+ details: "id: #{user.id}\nusername: #{user.username}\nname: ",+ created_at: 1.day.ago,+ updated_at: 1.day.ago,+ ip_address: "1.2.3.4",+ )++ UserDestroyer.new(admin).destroy(user)+ reviewable.reload++ history =+ UserHistory.where(action: UserHistory.actions[:delete_user]).order(created_at: :asc)++ expect(history.count).to eq(2)+ expect(history.first.details).to include("username: #{user.username}")+ expect(history.first.ip_address).not_to be_blank+ expect(history.last.details).to include("username: #{user.username}")+ expect(history.last.ip_address).not_to be_blank++ reviewable.scrub("reason", Guardian.new(admin))+ history.reload++ expect(history.first.details).to include(user.username)+ expect(history.first.details).to include("username: #{user.username}")+ expect(history.first.ip_address).not_to be_blank++ expect(history.last.details).to include("User details scrubbed by #{admin.username}")+ expect(history.last.details).to include("reason")+ expect(history.last.details).to include(+ "Timestamp: #{Time.zone.parse(reviewable.payload["scrubbed_at"])}",+ )+ expect(history.last.details).not_to include(user.username)+ expect(history.last.ip_address).to be_blank+ end++ it "replaces the reviewable payload with scrubbed details" do+ expect(reviewable.payload).to be_present+ expect(reviewable.payload["username"]).to eq(user.username)+ expect(reviewable.payload["email"]).to eq(user.email)+ expect(reviewable.payload["name"]).to eq(user.name)++ expect(reviewable.payload["scrubbed_by"]).to be_blank+ expect(reviewable.payload["scrubbed_reason"]).to be_blank+ expect(reviewable.payload["scrubbed_at"]).to be_blank++ reviewable.scrub("reason", Guardian.new(admin))+ reviewable.reload++ expect(reviewable.payload["scrubbed_by"]).to eq(admin.username)+ expect(reviewable.payload["scrubbed_reason"]).to eq("reason")+ expect(reviewable.payload["scrubbed_at"]).to be_present++ expect(reviewable.payload["username"]).to be_blank+ expect(reviewable.payload["email"]).to be_blank+ expect(reviewable.payload["name"]).to be_blank+ end+ endendenddiff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rbindex 982f8f84063..23bfe064413 100644--- a/spec/requests/reviewables_controller_spec.rb+++ b/spec/requests/reviewables_controller_spec.rb@@ -1021,6 +1021,74 @@ RSpec.describe ReviewablesController doendend+ describe "#scrub" do+ it "only allows admins to scrub reviewables" do+ moderator = Fabricate(:moderator)++ Jobs.run_immediately!+ SiteSetting.must_approve_users = true+ user = Fabricate(:user)+ user.activate+ reviewable = ReviewableUser.find_by(target: user)++ sign_in(moderator)+ put "/review/#{reviewable.id}/perform/delete_user.json?version=0"+ expect(response.status).to eq(200)++ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(403)++ sign_in(admin)+ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(200)+ end++ it "doesn't allow scrubbing of reviewables that haven't been rejected" do+ Jobs.run_immediately!+ SiteSetting.must_approve_users = true+ user = Fabricate(:user)+ user.activate+ reviewable = ReviewableUser.find_by(target: user)++ sign_in(admin)+ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(404)+ end++ it "doesn't allow scrubbing of reviewables that don't exist" do+ sign_in(admin)+ put "/review/123456789/scrub.json?reason=spam"+ expect(response.status).to eq(404)+ end++ it "doesn't allow scrubbing of reviewables that aren't scrubbable" do+ reviewable = Fabricate(:reviewable)+ expect(Reviewable.scrubbable_types).not_to include(reviewable.type)++ sign_in(admin)+ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(404)+ end++ it "doesn't allow scrubbing of reviewables that have already been scrubbed" do+ Jobs.run_immediately!+ SiteSetting.must_approve_users = true+ user = Fabricate(:user)+ user.activate+ reviewable = ReviewableUser.find_by(target: user)++ sign_in(admin)+ put "/review/#{reviewable.id}/perform/delete_user.json?version=0"+ expect(response.status).to eq(200)++ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(200)++ put "/review/#{reviewable.id}/scrub.json?reason=spam"+ expect(response.status).to eq(403)+ end+ end+describe "#count" dofab!(:admin)diff --git a/spec/serializers/reviewable_user_serializer_spec.rb b/spec/serializers/reviewable_user_serializer_spec.rbindex 70b7a933225..99911f8681e 100644--- a/spec/serializers/reviewable_user_serializer_spec.rb+++ b/spec/serializers/reviewable_user_serializer_spec.rb@@ -41,4 +41,18 @@ RSpec.describe ReviewableUserSerializer doexpect(json[:payload]["name"]).to eq(user.name)expect(json[:topic_url]).to be_blankend++ it "includes the scrubbed fields for scrubbed reviewables" do+ reviewable.scrub("reason", Guardian.new(admin))++ json = ReviewableUserSerializer.new(reviewable, scope: Guardian.new(admin), root: nil).as_json+ expect(json[:user_id]).to eq(reviewable.target_id)+ expect(json[:payload]["username"]).to eq(nil)+ expect(json[:payload]["email"]).to eq(nil)+ expect(json[:payload]["name"]).to eq(nil)+ expect(json[:payload]["scrubbed_by"]).to eq(admin.username)+ expect(json[:payload]["scrubbed_reason"]).to eq("reason")+ expect(json[:payload]["scrubbed_at"]).to be_present+ expect(json[:topic_url]).to be_blank+ endenddiff --git a/spec/system/page_objects/modals/scrub_rejected_user.rb b/spec/system/page_objects/modals/scrub_rejected_user.rbnew file mode 100644index 00000000000..39ab1bb122d--- /dev/null+++ b/spec/system/page_objects/modals/scrub_rejected_user.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++module PageObjects+ module Modals+ class ScrubRejectedUser < PageObjects::Modals::Base+ def modal+ find(".admin-scrub-rejected-user-modal")+ end++ def fill_in_scrub_reason(reason)+ modal.find("input#scrub-reason").fill_in(with: reason)+ end++ def scrub_button+ modal.find(".d-modal__footer .btn.btn-danger")+ end+ end+ end+enddiff --git a/spec/system/page_objects/pages/review.rb b/spec/system/page_objects/pages/review.rbindex 8046041ff11..58259cb5fd2 100644--- a/spec/system/page_objects/pages/review.rb+++ b/spec/system/page_objects/pages/review.rb@@ -71,8 +71,34 @@ module PageObjects)end- def has_error_dialog_visible?- page.has_css?(".dialog-container .dialog-content")+ def has_scrub_button?(reviewable)+ within(reviewable_by_id(reviewable.id)) { page.has_css?(".scrub-rejected-user button") }+ end++ def has_no_scrub_button?(reviewable)+ within(reviewable_by_id(reviewable.id)) { page.has_no_css?(".scrub-rejected-user button") }+ end++ def click_scrub_button(reviewable)+ within(reviewable_by_id(reviewable.id)) { find(".scrub-rejected-user button").click }+ end++ def has_reviewable_with_scrubbed_by?(reviewable, scrubbed_by)+ within(reviewable_by_id(reviewable.id)) do+ page.has_css?(".reviewable-user-details.scrubbed-by .value", text: scrubbed_by)+ end+ end++ def has_reviewable_with_scrubbed_reason?(reviewable, scrubbed_reason)+ within(reviewable_by_id(reviewable.id)) do+ page.has_css?(".reviewable-user-details.scrubbed-reason .value", text: scrubbed_reason)+ end+ end++ def has_reviewable_with_scrubbed_at?(reviewable, scrubbed_at)+ within(reviewable_by_id(reviewable.id)) do+ page.has_css?(".reviewable-user-details.scrubbed-at .value", text: scrubbed_at)+ endenddef has_no_error_dialog_visible?diff --git a/spec/system/reviewables_spec.rb b/spec/system/reviewables_spec.rbindex 810bd007c8f..1a253d580c2 100644--- a/spec/system/reviewables_spec.rb+++ b/spec/system/reviewables_spec.rb@@ -110,6 +110,7 @@ describe "Reviewables", type: :system dodescribe "when there is a reviewable user" dofab!(:user)let(:rejection_reason_modal) { PageObjects::Modals::RejectReasonReviewable.new }+ let(:scrub_user_modal) { PageObjects::Modals::ScrubRejectedUser.new }before doSiteSetting.must_approve_users = true@@ -138,6 +139,36 @@ describe "Reviewables", type: :system doexpect(mail.subject).to match(/You've been rejected on Discourse/)expect(mail.body.raw_source).to include rejection_reasonend++ it "Allows scrubbing user data after rejection" do+ rejection_reason = "user is spamming"+ scrubbing_reason = "a spammer who knows how to make GDPR requests"+ reviewable = ReviewableUser.find_by_target_id(user.id)++ review_page.visit_reviewable(reviewable)+ review_page.select_bundled_action(reviewable, "user-delete_user")+ rejection_reason_modal.fill_in_rejection_reason(rejection_reason)+ rejection_reason_modal.delete_user++ expect(review_page).to have_reviewable_with_rejected_status(reviewable)+ expect(review_page).to have_reviewable_with_rejection_reason(reviewable, rejection_reason)++ expect(review_page).to have_scrub_button(reviewable)+ review_page.click_scrub_button(reviewable)++ expect(scrub_user_modal.scrub_button).to be_disabled+ scrub_user_modal.fill_in_scrub_reason(scrubbing_reason)+ expect(scrub_user_modal.scrub_button).not_to be_disabled+ scrub_user_modal.scrub_button.click++ expect(review_page).to have_reviewable_with_scrubbed_by(reviewable, admin.username)+ expect(review_page).to have_reviewable_with_scrubbed_reason(reviewable, scrubbing_reason)+ expect(review_page).to have_reviewable_with_scrubbed_at(+ reviewable,+ reviewable.payload["scrubbed_at"],+ )+ expect(review_page).to have_no_scrub_button(reviewable)+ endendcontext "when performing a review action from the show route" do