2013-04-12 04:04:20 +08:00
|
|
|
# Responsible for destroying a User record
|
|
|
|
class UserDestroyer
|
|
|
|
|
|
|
|
class PostsExistError < RuntimeError; end
|
|
|
|
|
2013-07-27 03:40:08 +08:00
|
|
|
def initialize(staff)
|
|
|
|
@staff = staff
|
|
|
|
raise Discourse::InvalidParameters.new('staff user is nil') unless @staff and @staff.is_a?(User)
|
|
|
|
raise Discourse::InvalidAccess unless @staff.staff?
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns false if the user failed to be deleted.
|
|
|
|
# Returns a frozen instance of the User if the delete succeeded.
|
2013-07-25 01:48:55 +08:00
|
|
|
def destroy(user, opts={})
|
2013-04-12 04:04:20 +08:00
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
2013-07-25 01:48:55 +08:00
|
|
|
raise PostsExistError if !opts[:delete_posts] && user.post_count != 0
|
2013-04-15 23:39:30 +08:00
|
|
|
User.transaction do
|
2013-07-25 01:48:55 +08:00
|
|
|
if opts[:delete_posts]
|
|
|
|
user.posts.each do |post|
|
2013-08-14 23:05:53 +08:00
|
|
|
if opts[:block_urls]
|
|
|
|
post.topic_links.each do |link|
|
|
|
|
unless link.internal or Oneboxer.oneboxer_exists_for_url?(link.url)
|
2013-08-23 07:04:17 +08:00
|
|
|
ScreenedUrl.watch(link.url, link.domain, ip_address: user.ip_address).try(:record_match!)
|
2013-08-14 23:05:53 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-27 03:40:08 +08:00
|
|
|
PostDestroyer.new(@staff, post).destroy
|
2013-09-05 03:35:10 +08:00
|
|
|
if post.topic and post.post_number == 1
|
|
|
|
Topic.unscoped.where(id: post.topic.id).update_all(user_id: nil)
|
|
|
|
end
|
2013-07-25 01:48:55 +08:00
|
|
|
end
|
|
|
|
raise PostsExistError if user.reload.post_count != 0
|
|
|
|
end
|
2013-04-15 23:39:30 +08:00
|
|
|
user.destroy.tap do |u|
|
|
|
|
if u
|
2013-07-26 03:30:03 +08:00
|
|
|
if opts[:block_email]
|
2013-08-23 07:04:17 +08:00
|
|
|
b = ScreenedEmail.block(u.email, ip_address: u.ip_address)
|
2013-07-26 03:30:03 +08:00
|
|
|
b.record_match! if b
|
|
|
|
end
|
2013-10-22 02:49:51 +08:00
|
|
|
if opts[:block_ip]
|
|
|
|
b = ScreenedIpAddress.watch(u.ip_address)
|
|
|
|
b.record_match! if b
|
|
|
|
end
|
2013-09-04 05:19:29 +08:00
|
|
|
Post.with_deleted.where(user_id: user.id).update_all("user_id = NULL")
|
2013-11-02 04:55:56 +08:00
|
|
|
|
|
|
|
# If this user created categories, fix those up:
|
2013-11-06 03:35:28 +08:00
|
|
|
categories = Category.where(user_id: user.id)
|
2013-11-02 04:55:56 +08:00
|
|
|
categories.each do |c|
|
|
|
|
c.user_id = Discourse.system_user.id
|
|
|
|
c.save!
|
|
|
|
if topic = Topic.with_deleted.where(id: c.topic_id).first
|
|
|
|
topic.try(:recover!)
|
|
|
|
topic.user_id = Discourse.system_user.id
|
|
|
|
topic.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-27 03:40:08 +08:00
|
|
|
StaffActionLogger.new(@staff).log_user_deletion(user, opts.slice(:context))
|
2013-04-29 23:38:43 +08:00
|
|
|
DiscourseHub.unregister_nickname(user.username) if SiteSetting.call_discourse_hub?
|
2013-04-15 23:39:30 +08:00
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
|
|
|
|
end
|
2013-04-12 04:04:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-06 03:35:28 +08:00
|
|
|
end
|