From 51a62a8df65a75f7515be2e1d69a3a6acdce4bcc Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 29 Apr 2013 22:47:08 -0700 Subject: [PATCH] add support for deleting user accounts --- app/models/favorite.rb | 6 +++++- app/models/user.rb | 30 ++++++++++++++++++++++++++++++ test/unit/user_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 6ffd664f9..d3c84890e 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -4,7 +4,11 @@ class Favorite < ActiveRecord::Base # this is necessary because there's no trigger for deleting favorites def self.destroy_all(hash) - connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}") + if hash[:user_id] && hash[:post_id] + connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}") + elsif hash[:user_id] + connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]}") + end end def self.add(post, user) diff --git a/app/models/user.rb b/app/models/user.rb index 5df350e43..d5eed17a8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -616,6 +616,35 @@ class User < ActiveRecord::Base end end + module DeletionMethods + def delete! + scramble_password + remove_all_favorites + rename_for_delete + end + + def rename_for_delete + alt_name = "user#{id}" + n = 0 + while User.where(:name => alt_name).exists? + alt_name = "user#{id}_#{n}" + n += 1 + end + update_attribute(:name, alt_name) + + end + + def scramble_password + update_attribute(:bcrypt_password_hash, User.bcrypt(SecureRandom.hex(16))) + end + + def remove_all_favorites + Post.tag_match("fav:#{name}").find_each do |post| + Favorite.remove(post, self) + end + end + end + include BanMethods include NameMethods include PasswordMethods @@ -629,6 +658,7 @@ class User < ActiveRecord::Base include InvitationMethods include ApiMethods extend SearchMethods + include DeletionMethods def initialize_default_image_size self.default_image_size = "large" diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index e16945146..79c324cad 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -14,6 +14,29 @@ class UserTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end + context "that has been deleted" do + setup do + @post = FactoryGirl.create(:post) + Favorite.add(@post, @user) + @user.delete! + @post.reload + end + + should "rename the user" do + assert_equal("user#{@user.id}", @user.name) + end + + should "remove all favorites" do + assert_equal(0, @user.favorite_count) + assert_equal(0, Favorite.for_user(@user.id).count) + assert_equal("", @post.fav_string) + end + + should "reset the password" do + assert_nil(User.authenticate(@user.name, "password")) + end + end + context "favoriting a post" do setup do @user.update_column(:favorite_count, 999)