2014-11-21 12:16:06 -05:00
|
|
|
require_dependency 'ip_addr'
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
# Responsible for destroying a User record
|
|
|
|
class UserDestroyer
|
|
|
|
|
|
|
|
class PostsExistError < RuntimeError; end
|
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
def initialize(actor)
|
|
|
|
@actor = actor
|
|
|
|
raise Discourse::InvalidParameters.new('acting user is nil') unless @actor and @actor.is_a?(User)
|
|
|
|
@guardian = Guardian.new(actor)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns false if the user failed to be deleted.
|
|
|
|
# Returns a frozen instance of the User if the delete succeeded.
|
2013-07-24 13:48:55 -04:00
|
|
|
def destroy(user, opts={})
|
2013-04-11 16:04:20 -04:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
2014-02-13 11:42:35 -05:00
|
|
|
@guardian.ensure_can_delete_user!(user)
|
2014-08-18 12:07:21 -04:00
|
|
|
raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2013-04-15 11:39:30 -04:00
|
|
|
User.transaction do
|
2015-04-24 16:04:44 -04:00
|
|
|
|
2015-08-24 02:05:08 -04:00
|
|
|
Draft.where(user_id: user.id).delete_all
|
2015-04-24 16:04:44 -04:00
|
|
|
QueuedPost.where(user_id: user.id).delete_all
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
if opts[:delete_posts]
|
|
|
|
user.posts.each do |post|
|
2014-10-20 10:59:06 -04:00
|
|
|
# agree with flags
|
|
|
|
PostAction.agree_flags!(post, @actor) if opts[:delete_as_spammer]
|
|
|
|
|
|
|
|
# block all external urls
|
2013-08-14 11:05:53 -04:00
|
|
|
if opts[:block_urls]
|
|
|
|
post.topic_links.each do |link|
|
|
|
|
unless link.internal or Oneboxer.oneboxer_exists_for_url?(link.url)
|
2013-08-22 19:04:17 -04:00
|
|
|
ScreenedUrl.watch(link.url, link.domain, ip_address: user.ip_address).try(:record_match!)
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2014-02-13 15:51:19 -05:00
|
|
|
PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
if post.topic and post.is_first_post?
|
2013-09-04 15:35:10 -04:00
|
|
|
Topic.unscoped.where(id: post.topic.id).update_all(user_id: nil)
|
|
|
|
end
|
2013-07-24 13:48:55 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2014-03-31 14:06:25 -04:00
|
|
|
user.post_actions.each do |post_action|
|
|
|
|
post_action.remove_act!(Discourse.system_user)
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2013-04-15 11:39:30 -04:00
|
|
|
user.destroy.tap do |u|
|
|
|
|
if u
|
2014-11-21 12:16:06 -05:00
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
if opts[:block_email]
|
2013-08-22 19:04:17 -04:00
|
|
|
b = ScreenedEmail.block(u.email, ip_address: u.ip_address)
|
2013-07-25 15:30:03 -04:00
|
|
|
b.record_match! if b
|
|
|
|
end
|
2014-11-21 12:16:06 -05:00
|
|
|
|
2014-04-29 14:37:56 -04:00
|
|
|
if opts[:block_ip] && u.ip_address
|
2014-11-21 12:16:06 -05:00
|
|
|
b = ScreenedIpAddress.watch(u.ip_address)
|
|
|
|
b.record_match! if b
|
2014-04-29 14:37:56 -04:00
|
|
|
if u.registration_ip_address && u.ip_address != u.registration_ip_address
|
2014-11-21 12:16:06 -05:00
|
|
|
b = ScreenedIpAddress.watch(u.registration_ip_address)
|
|
|
|
b.record_match! if b
|
2014-04-29 14:37:56 -04:00
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
2014-11-21 12:16:06 -05:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
Post.with_deleted.where(user_id: user.id).update_all("user_id = NULL")
|
2013-11-01 16:55:56 -04:00
|
|
|
|
|
|
|
# If this user created categories, fix those up:
|
2013-11-05 14:35:28 -05:00
|
|
|
categories = Category.where(user_id: user.id)
|
2013-11-01 16:55:56 -04:00
|
|
|
categories.each do |c|
|
|
|
|
c.user_id = Discourse.system_user.id
|
|
|
|
c.save!
|
2014-05-06 09:41:59 -04:00
|
|
|
if topic = Topic.with_deleted.find_by(id: c.topic_id)
|
2013-11-01 16:55:56 -04:00
|
|
|
topic.try(:recover!)
|
|
|
|
topic.user_id = Discourse.system_user.id
|
|
|
|
topic.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-13 11:42:35 -05:00
|
|
|
StaffActionLogger.new(@actor == user ? Discourse.system_user : @actor).log_user_deletion(user, opts.slice(:context))
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
|
2013-04-15 11:39:30 -04:00
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-05 14:35:28 -05:00
|
|
|
end
|