REFACTOR: Run some parts of user anonymizing in background job

This commit is contained in:
Gerhard Schlager 2018-06-07 15:59:51 +02:00
parent cf038cf72a
commit 2564a8285d
3 changed files with 114 additions and 81 deletions

View File

@ -0,0 +1,50 @@
module Jobs
class AnonymizeUser < Jobs::Base
sidekiq_options queue: 'low'
def execute(args)
@user_id = args[:user_id]
@prev_email = args[:prev_email]
@anonymize_ip = args[:anonymize_ip]
make_anonymous
end
def make_anonymous
anonymize_ips(@anonymize_ip) if @anonymize_ip
Invite.with_deleted.where(user_id: @user_id).destroy_all
EmailToken.where(user_id: @user_id).destroy_all
EmailLog.where(user_id: @user_id).delete_all
IncomingEmail.where("user_id = ? OR from_address = ?", @user_id, @prev_email).delete_all
Post.with_deleted
.where(user_id: @user_id)
.where.not(raw_email: nil)
.update_all(raw_email: nil)
end
def ip_where(column = 'user_id')
["#{column} = :user_id AND ip_address IS NOT NULL", user_id: @user_id]
end
def anonymize_ips(new_ip)
IncomingLink.where(ip_where('current_user_id')).update_all(ip_address: new_ip)
ScreenedEmail.where(email: @prev_email).update_all(ip_address: new_ip)
SearchLog.where(ip_where).update_all(ip_address: new_ip)
TopicLinkClick.where(ip_where).update_all(ip_address: new_ip)
TopicViewItem.where(ip_where).update_all(ip_address: new_ip)
UserHistory.where(ip_where('acting_user_id')).update_all(ip_address: new_ip)
UserProfileView.where(ip_where).update_all(ip_address: new_ip)
# UserHistory for delete_user logs the user's IP. Note this is quite ugly but we don't
# have a better way of querying on details right now.
UserHistory.where(
"action = :action AND details LIKE 'id: #{@user_id}\n%'",
action: UserHistory.actions[:delete_user]
).update_all(ip_address: new_ip)
end
end
end

View File

@ -25,12 +25,15 @@ class UserAnonymizer
@user.reload
@user.password = SecureRandom.hex
@user.email = "#{@user.username}@example.com"
@user.email = "#{@user.username}@anonymized.invalid"
@user.name = SiteSetting.full_name_required ? @user.username : nil
@user.date_of_birth = nil
@user.title = nil
anonymize_ips(@opts[:anonymize_ip]) if @opts.has_key?(:anonymize_ip)
if @opts.has_key?(:anonymize_ip)
@user.ip_address = @opts[:anonymize_ip]
@user.registration_ip_address = @opts[:anonymize_ip]
end
@user.save
@ -57,29 +60,13 @@ class UserAnonymizer
@user.user_open_ids.find_each { |x| x.destroy }
@user.api_key.try(:destroy)
history_details = {
action: UserHistory.actions[:anonymize_user],
target_user_id: @user.id,
acting_user_id: @actor ? @actor.id : @user.id,
}
Invite.with_deleted.where(user_id: @user.id).destroy_all
EmailToken.where(user_id: @user.id).destroy_all
EmailLog.where(user_id: @user.id).delete_all
IncomingEmail.where("user_id = ? OR from_address = ?", @user.id, @prev_email).delete_all
Post.with_deleted
.where(user_id: @user.id)
.where.not(raw_email: nil)
.update_all(raw_email: nil)
if SiteSetting.log_anonymizer_details?
history_details[:email] = @prev_email
history_details[:details] = "username: #{@prev_username}"
@user_history = log_action
end
@user_history = UserHistory.create(history_details)
end
Jobs.enqueue(:anonymize_user,
user_id: @user.id,
prev_email: @prev_email,
anonymize_ip: @opts[:anonymize_ip])
DiscourseEvent.trigger(:user_anonymized, user: @user, opts: @opts)
@user
@ -95,29 +82,18 @@ class UserAnonymizer
raise "Failed to generate an anon username"
end
def ip_where(column = 'user_id')
["#{column} = :user_id AND ip_address IS NOT NULL", user_id: @user.id]
def log_action
history_details = {
action: UserHistory.actions[:anonymize_user],
target_user_id: @user.id,
acting_user_id: @actor ? @actor.id : @user.id,
}
if SiteSetting.log_anonymizer_details?
history_details[:email] = @prev_email
history_details[:details] = "username: #{@prev_username}"
end
def anonymize_ips(new_ip)
@user.ip_address = new_ip
@user.registration_ip_address = new_ip
IncomingLink.where(ip_where('current_user_id')).update_all(ip_address: new_ip)
ScreenedEmail.where(email: @prev_email).update_all(ip_address: new_ip)
SearchLog.where(ip_where).update_all(ip_address: new_ip)
TopicLinkClick.where(ip_where).update_all(ip_address: new_ip)
TopicViewItem.where(ip_where).update_all(ip_address: new_ip)
UserHistory.where(ip_where('acting_user_id')).update_all(ip_address: new_ip)
UserProfileView.where(ip_where).update_all(ip_address: new_ip)
# UserHistory for delete_user logs the user's IP. Note this is quite ugly but we don't
# have a better way of querying on details right now.
UserHistory.where(
"action = :action AND details LIKE 'id: #{@user.id}\n%'",
action: UserHistory.actions[:delete_user]
).update_all(ip_address: new_ip)
UserHistory.create(history_details)
end
end

View File

@ -35,7 +35,7 @@ describe UserAnonymizer do
it "changes email address" do
make_anonymous
expect(user.reload.email).to eq("#{user.username}@example.com")
expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
end
it "turns off all notifications" do
@ -203,6 +203,11 @@ describe UserAnonymizer do
expect(user.api_key).to eq(nil)
end
context "executes job" do
before do
SiteSetting.queue_jobs = false
end
it "removes invites" do
Fabricate(:invite, user: user)
Fabricate(:invite, user: another_user)
@ -247,6 +252,7 @@ describe UserAnonymizer do
expect(post2.reload).to have_attributes(via_email: true, raw_email: "raw email from another user")
end
end
end
describe "anonymize_ip" do
let(:old_ip) { "1.2.3.4" }
@ -267,6 +273,7 @@ describe UserAnonymizer do
end
it "exhaustively replaces all user ips" do
SiteSetting.queue_jobs = false
link = IncomingLink.create!(current_user_id: user.id, ip_address: old_ip, post_id: post.id)
screened_email = ScreenedEmail.create!(email: user.email, ip_address: old_ip)