FEATURE: Anonymize User. A way to remove a user but keep their topics and posts.

This commit is contained in:
Neil Lalonde
2015-03-06 16:44:54 -05:00
parent a68512bebf
commit 608647d02f
16 changed files with 401 additions and 100 deletions

View File

@@ -0,0 +1,63 @@
class UserAnonymizer
def initialize(user, actor=nil)
@user = user
@actor = actor
end
def self.make_anonymous(user, actor=nil)
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"
@user.name = nil
@user.date_of_birth = nil
@user.title = nil
@user.email_digests = false
@user.email_private_messages = false
@user.email_direct = false
@user.email_always = false
@user.mailing_list_mode = false
@user.save
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)
@user.user_open_ids.find_each { |x| x.destroy }
@user.api_key.try(:destroy)
UserHistory.create( action: UserHistory.actions[:anonymize_user],
target_user_id: @user.id,
acting_user_id: @actor ? @actor.id : @user.id,
email: prev_email,
details: "username: #{prev_username}" )
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

View File

@@ -0,0 +1,23 @@
class UsernameChanger
def initialize(user, new_username, actor=nil)
@user = user
@new_username = new_username
@actor = actor
end
def self.change(user, new_username, actor=nil)
self.new(user, new_username, actor).change
end
def change
if @actor && @actor != @user
StaffActionLogger.new(@actor).log_username_change(@user, @user.username, @new_username)
end
# future work: update mentions and quotes
@user.username = @new_username
@user.save
end
end