2015-03-06 16:44:54 -05:00
|
|
|
class UserAnonymizer
|
2018-03-05 14:38:18 -05:00
|
|
|
|
|
|
|
attr_reader :user_history
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(user, actor = nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
@user = user
|
|
|
|
@actor = actor
|
2018-03-05 14:38:18 -05:00
|
|
|
@user_history = nil
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.make_anonymous(user, actor = nil)
|
2015-03-06 16:44:54 -05:00
|
|
|
self.new(user, actor).make_anonymous
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_anonymous
|
|
|
|
User.transaction do
|
|
|
|
prev_email = @user.email
|
|
|
|
prev_username = @user.username
|
|
|
|
|
|
|
|
if !UsernameChanger.change(@user, make_anon_username)
|
|
|
|
raise "Failed to change username"
|
|
|
|
end
|
|
|
|
|
|
|
|
@user.reload
|
|
|
|
@user.password = SecureRandom.hex
|
|
|
|
@user.email = "#{@user.username}@example.com"
|
2016-03-17 02:43:21 -04:00
|
|
|
@user.name = SiteSetting.full_name_required ? @user.username : nil
|
2015-03-06 16:44:54 -05:00
|
|
|
@user.date_of_birth = nil
|
|
|
|
@user.title = nil
|
2015-11-27 14:37:20 -05:00
|
|
|
@user.uploaded_avatar_id = nil
|
2015-03-06 16:44:54 -05:00
|
|
|
@user.save
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
options = @user.user_option
|
|
|
|
options.email_always = false
|
|
|
|
options.mailing_list_mode = false
|
|
|
|
options.email_digests = false
|
|
|
|
options.email_private_messages = false
|
|
|
|
options.email_direct = false
|
|
|
|
options.save
|
|
|
|
|
2015-03-06 16:44:54 -05:00
|
|
|
profile = @user.user_profile
|
|
|
|
profile.destroy if profile
|
|
|
|
@user.create_user_profile
|
|
|
|
|
|
|
|
@user.user_avatar.try(:destroy)
|
|
|
|
@user.twitter_user_info.try(:destroy)
|
|
|
|
@user.google_user_info.try(:destroy)
|
|
|
|
@user.github_user_info.try(:destroy)
|
|
|
|
@user.facebook_user_info.try(:destroy)
|
|
|
|
@user.single_sign_on_record.try(:destroy)
|
|
|
|
@user.oauth2_user_info.try(:destroy)
|
2018-03-01 06:10:27 -05:00
|
|
|
@user.instagram_user_info.try(:destroy)
|
2015-03-06 16:44:54 -05:00
|
|
|
@user.user_open_ids.find_each { |x| x.destroy }
|
|
|
|
@user.api_key.try(:destroy)
|
|
|
|
|
2018-03-05 14:38:18 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
@user_history = UserHistory.create(history_details)
|
2015-03-06 16:44:54 -05:00
|
|
|
end
|
|
|
|
@user
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_anon_username
|
|
|
|
100.times do
|
|
|
|
new_username = "anon#{(SecureRandom.random_number * 100000000).to_i}"
|
|
|
|
return new_username unless User.where(username_lower: new_username).exists?
|
|
|
|
end
|
|
|
|
raise "Failed to generate an anon username"
|
|
|
|
end
|
|
|
|
end
|