2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-06 15:44:54 -06:00
|
|
|
class UserAnonymizer
|
2018-03-05 13:38:18 -06:00
|
|
|
attr_reader :user_history
|
|
|
|
|
2023-03-22 13:51:42 -05:00
|
|
|
EMAIL_SUFFIX = "@anonymized.invalid"
|
|
|
|
|
2018-04-30 12:42:51 -05:00
|
|
|
# opts:
|
|
|
|
# anonymize_ip - an optional new IP to update their logs with
|
|
|
|
def initialize(user, actor = nil, opts = nil)
|
2015-03-06 15:44:54 -06:00
|
|
|
@user = user
|
|
|
|
@actor = actor
|
2018-03-05 13:38:18 -06:00
|
|
|
@user_history = nil
|
2018-04-30 12:42:51 -05:00
|
|
|
@opts = opts || {}
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|
|
|
|
|
2018-04-30 12:42:51 -05:00
|
|
|
def self.make_anonymous(user, actor = nil, opts = nil)
|
|
|
|
self.new(user, actor, opts).make_anonymous
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_anonymous
|
|
|
|
User.transaction do
|
2018-04-30 12:42:51 -05:00
|
|
|
@prev_email = @user.email
|
|
|
|
@prev_username = @user.username
|
2015-03-06 15:44:54 -06:00
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
unless UsernameChanger.new(@user, make_anon_username).change(run_update_job: false)
|
|
|
|
raise "Failed to change username"
|
|
|
|
end
|
2015-03-06 15:44:54 -06:00
|
|
|
|
|
|
|
@user.reload
|
|
|
|
@user.password = SecureRandom.hex
|
2016-03-17 01:43:21 -05:00
|
|
|
@user.name = SiteSetting.full_name_required ? @user.username : nil
|
2015-03-06 15:44:54 -06:00
|
|
|
@user.date_of_birth = nil
|
|
|
|
@user.title = nil
|
2018-06-08 07:31:15 -05:00
|
|
|
@user.uploaded_avatar_id = nil
|
2018-04-30 12:42:51 -05:00
|
|
|
|
2018-06-07 08:59:51 -05:00
|
|
|
if @opts.has_key?(:anonymize_ip)
|
|
|
|
@user.ip_address = @opts[:anonymize_ip]
|
|
|
|
@user.registration_ip_address = @opts[:anonymize_ip]
|
|
|
|
end
|
2018-04-30 12:42:51 -05:00
|
|
|
|
2019-01-30 08:14:36 -06:00
|
|
|
@user.save!
|
2023-03-22 13:51:42 -05:00
|
|
|
@user.primary_email.update_attribute(:email, "#{@user.username}#{EMAIL_SUFFIX}")
|
2015-03-06 15:44:54 -06:00
|
|
|
|
2016-02-16 22:46:19 -06:00
|
|
|
options = @user.user_option
|
|
|
|
options.mailing_list_mode = false
|
|
|
|
options.email_digests = false
|
2019-03-15 09:55:11 -05:00
|
|
|
options.email_level = UserOption.email_level_types[:never]
|
|
|
|
options.email_messages_level = UserOption.email_level_types[:never]
|
2019-01-30 08:14:36 -06:00
|
|
|
options.save!
|
2016-02-16 22:46:19 -06:00
|
|
|
|
2018-06-08 08:24:15 -05:00
|
|
|
if profile = @user.user_profile
|
2019-04-28 22:58:52 -05:00
|
|
|
profile.update!(
|
|
|
|
location: nil,
|
|
|
|
website: nil,
|
|
|
|
bio_raw: nil,
|
|
|
|
bio_cooked: nil,
|
|
|
|
profile_background_upload: nil,
|
|
|
|
card_background_upload: nil,
|
|
|
|
)
|
2018-06-08 08:24:15 -05:00
|
|
|
end
|
2015-03-06 15:44:54 -06:00
|
|
|
|
2023-05-22 00:18:09 -05:00
|
|
|
@user.clear_status!
|
|
|
|
|
2021-01-25 08:07:22 -06:00
|
|
|
@user.user_avatar&.destroy!
|
|
|
|
@user.single_sign_on_record&.destroy!
|
|
|
|
@user.oauth2_user_infos.destroy_all
|
|
|
|
@user.user_associated_accounts.destroy_all
|
|
|
|
@user.api_keys.destroy_all
|
|
|
|
@user.user_api_keys.destroy_all
|
2019-01-30 08:14:36 -06:00
|
|
|
@user.user_emails.secondary.destroy_all
|
2015-03-06 15:44:54 -06:00
|
|
|
|
2018-06-07 08:59:51 -05:00
|
|
|
@user_history = log_action
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|
2018-05-02 11:25:06 -05:00
|
|
|
|
2018-06-08 07:31:15 -05:00
|
|
|
UsernameChanger.update_username(
|
|
|
|
user_id: @user.id,
|
|
|
|
old_username: @prev_username,
|
|
|
|
new_username: @user.username,
|
|
|
|
avatar_template: @user.avatar_template,
|
|
|
|
)
|
|
|
|
|
2018-06-07 08:59:51 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:anonymize_user,
|
|
|
|
user_id: @user.id,
|
|
|
|
prev_email: @prev_email,
|
|
|
|
anonymize_ip: @opts[:anonymize_ip],
|
|
|
|
)
|
|
|
|
|
2018-05-02 11:25:06 -05:00
|
|
|
DiscourseEvent.trigger(:user_anonymized, user: @user, opts: @opts)
|
2015-03-06 15:44:54 -06:00
|
|
|
@user
|
|
|
|
end
|
|
|
|
|
2018-04-30 12:42:51 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def make_anon_username
|
|
|
|
100.times do
|
|
|
|
new_username = "anon#{(SecureRandom.random_number * 100_000_000).to_i}"
|
|
|
|
return new_username unless User.where(username_lower: new_username).exists?
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|
2018-04-30 12:42:51 -05:00
|
|
|
raise "Failed to generate an anon username"
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2018-04-30 12:42:51 -05:00
|
|
|
|
2018-06-07 08:59:51 -05:00
|
|
|
def log_action
|
|
|
|
history_details = {
|
|
|
|
action: UserHistory.actions[:anonymize_user],
|
|
|
|
target_user_id: @user.id,
|
|
|
|
acting_user_id: @actor ? @actor.id : @user.id,
|
|
|
|
}
|
2018-04-30 12:42:51 -05:00
|
|
|
|
2018-06-07 08:59:51 -05:00
|
|
|
if SiteSetting.log_anonymizer_details?
|
|
|
|
history_details[:email] = @prev_email
|
|
|
|
history_details[:details] = "username: #{@prev_username}"
|
|
|
|
end
|
2018-04-30 12:42:51 -05:00
|
|
|
|
2019-01-30 08:14:36 -06:00
|
|
|
UserHistory.create!(history_details)
|
2018-04-30 12:42:51 -05:00
|
|
|
end
|
2015-03-06 15:44:54 -06:00
|
|
|
end
|