2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-06-07 09:59:51 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class AnonymizeUser < ::Jobs::Base
|
2018-06-07 09:59:51 -04:00
|
|
|
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
|
|
|
|
|
2021-06-17 03:45:40 -04:00
|
|
|
Invite.where(email: @prev_email).destroy_all
|
2020-06-09 11:19:32 -04:00
|
|
|
InvitedUser.where(user_id: @user_id).destroy_all
|
2018-06-07 09:59:51 -04:00
|
|
|
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)
|
2018-09-06 18:02:47 -04:00
|
|
|
|
|
|
|
anonymize_user_fields
|
2018-06-07 09:59:51 -04:00
|
|
|
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(
|
2022-11-01 15:05:13 -04:00
|
|
|
"action = :action AND details LIKE :details",
|
|
|
|
action: UserHistory.actions[:delete_user],
|
|
|
|
details: "id: #{@user_id}\n%",
|
2018-06-07 09:59:51 -04:00
|
|
|
).update_all(ip_address: new_ip)
|
|
|
|
end
|
|
|
|
|
2018-09-06 18:02:47 -04:00
|
|
|
def anonymize_user_fields
|
|
|
|
user_field_ids = UserField.pluck(:id)
|
|
|
|
user = User.find(@user_id)
|
|
|
|
return if user_field_ids.blank? || user.blank?
|
|
|
|
|
|
|
|
user_field_ids.each do |field_id|
|
|
|
|
user.custom_fields.delete("#{User::USER_FIELD_PREFIX}#{field_id}")
|
|
|
|
end
|
|
|
|
user.save!
|
|
|
|
end
|
2018-06-07 09:59:51 -04:00
|
|
|
end
|
|
|
|
end
|