2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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 && @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 && user.is_a?(User)
|
2014-08-18 12:07:21 -04:00
|
|
|
raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
|
2017-03-17 17:01:45 -04:00
|
|
|
@guardian.ensure_can_delete_user!(user)
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2019-01-16 15:54:21 -05:00
|
|
|
# default to using a transaction
|
|
|
|
opts[:transaction] = true if opts[:transaction] != false
|
|
|
|
|
2019-01-18 10:04:29 -05:00
|
|
|
prepare_for_destroy(user) if opts[:prepare_for_destroy] == true
|
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
result = nil
|
|
|
|
|
2019-01-16 15:54:21 -05:00
|
|
|
optional_transaction(open_transaction: opts[:transaction]) do
|
2015-04-24 16:04:44 -04:00
|
|
|
|
2020-02-25 14:07:57 -05:00
|
|
|
UserSecurityKey.where(user_id: user.id).delete_all
|
2020-03-11 20:16:00 -04:00
|
|
|
Bookmark.where(user_id: user.id).delete_all
|
2015-08-24 02:05:08 -04:00
|
|
|
Draft.where(user_id: user.id).delete_all
|
2019-01-03 12:03:01 -05:00
|
|
|
Reviewable.where(created_by_id: user.id).delete_all
|
2015-04-24 16:04:44 -04:00
|
|
|
|
2020-12-22 22:19:30 -05:00
|
|
|
category_topic_ids = Category.where("topic_id IS NOT NULL").pluck(:topic_id)
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
if opts[:delete_posts]
|
|
|
|
user.posts.each do |post|
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2014-10-20 10:59:06 -04:00
|
|
|
# agree with flags
|
2019-01-03 12:03:01 -05:00
|
|
|
if opts[:delete_as_spammer] && reviewable = post.reviewable_flag
|
|
|
|
reviewable.perform(@actor, :agree_and_keep)
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
|
|
|
# block all external urls
|
2013-08-14 11:05:53 -04:00
|
|
|
if opts[:block_urls]
|
|
|
|
post.topic_links.each do |link|
|
2017-07-25 11:44:46 -04:00
|
|
|
next if link.internal
|
2020-07-26 20:23:54 -04:00
|
|
|
next if Oneboxer.engine(link.url) != Onebox::Engine::AllowlistedGenericOnebox
|
2017-07-25 11:44:46 -04:00
|
|
|
ScreenedUrl.watch(link.url, link.domain, ip_address: user.ip_address)&.record_match!
|
2013-08-14 11:05:53 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2020-12-22 22:19:30 -05:00
|
|
|
if post.is_first_post? && category_topic_ids.include?(post.topic_id)
|
|
|
|
post.update!(user: Discourse.system_user)
|
|
|
|
else
|
|
|
|
PostDestroyer.new(@actor.staff? ? @actor : Discourse.system_user, post).destroy
|
|
|
|
end
|
2014-10-20 10:59:06 -04:00
|
|
|
|
2017-01-05 21:01:14 -05:00
|
|
|
if post.topic && post.is_first_post?
|
2017-07-25 11:44:46 -04:00
|
|
|
Topic.unscoped.where(id: post.topic_id).update_all(user_id: nil)
|
2013-09-04 15:35:10 -04:00
|
|
|
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
|
|
|
|
2018-10-02 13:46:43 -04:00
|
|
|
# Add info about the user to staff action logs
|
|
|
|
UserHistory.staff_action_records(
|
|
|
|
Discourse.system_user, acting_user: user.username
|
|
|
|
).each do |log|
|
|
|
|
log.details ||= ''
|
|
|
|
log.details = (log.details.split("\n") +
|
|
|
|
["user_id: #{user.id}", "username: #{user.username}"]
|
|
|
|
).join("\n")
|
|
|
|
log.save!
|
|
|
|
end
|
|
|
|
|
2017-07-25 11:44:46 -04:00
|
|
|
# keep track of emails used
|
|
|
|
user_emails = user.user_emails.pluck(:email)
|
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
if result = user.destroy
|
|
|
|
if opts[:block_email]
|
|
|
|
user_emails.each do |email|
|
|
|
|
ScreenedEmail.block(email, ip_address: result.ip_address)&.record_match!
|
2013-07-25 15:30:03 -04:00
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
end
|
2014-11-21 12:16:06 -05:00
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
if opts[:block_ip] && result.ip_address
|
|
|
|
ScreenedIpAddress.watch(result.ip_address)&.record_match!
|
|
|
|
if result.registration_ip_address && result.ip_address != result.registration_ip_address
|
|
|
|
ScreenedIpAddress.watch(result.registration_ip_address)&.record_match!
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
end
|
2014-11-21 12:16:06 -05:00
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
Post.unscoped.where(user_id: result.id).update_all(user_id: nil)
|
2013-11-01 16:55:56 -04:00
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
# If this user created categories, fix those up:
|
|
|
|
Category.where(user_id: result.id).each do |c|
|
|
|
|
c.user_id = Discourse::SYSTEM_USER_ID
|
|
|
|
c.save!
|
|
|
|
if topic = Topic.unscoped.find_by(id: c.topic_id)
|
|
|
|
topic.recover!
|
|
|
|
topic.user_id = Discourse::SYSTEM_USER_ID
|
|
|
|
topic.save!
|
2013-11-01 16:55:56 -04:00
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
end
|
2013-11-01 16:55:56 -04:00
|
|
|
|
2019-04-10 11:00:14 -04:00
|
|
|
unless opts[:quiet]
|
|
|
|
if @actor == user
|
|
|
|
deleted_by = Discourse.system_user
|
|
|
|
opts[:context] = I18n.t("staff_action_logs.user_delete_self", url: opts[:context])
|
|
|
|
else
|
|
|
|
deleted_by = @actor
|
2017-10-03 11:28:41 -04:00
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
StaffActionLogger.new(deleted_by).log_user_deletion(user, opts.slice(:context))
|
2013-04-15 11:39:30 -04:00
|
|
|
end
|
2019-08-22 11:42:45 -04:00
|
|
|
MessageBus.publish "/logout", result.id, user_ids: [result.id]
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
|
|
|
|
# After the user is deleted, remove the reviewable
|
2019-09-04 13:12:48 -04:00
|
|
|
if reviewable = ReviewableUser.pending.find_by(target: user)
|
2019-04-17 11:26:43 -04:00
|
|
|
reviewable.perform(@actor, :reject_user_delete)
|
2019-04-10 11:00:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 15:54:21 -05:00
|
|
|
protected
|
|
|
|
|
2019-01-18 10:04:29 -05:00
|
|
|
def prepare_for_destroy(user)
|
|
|
|
PostAction.where(user_id: user.id).delete_all
|
|
|
|
UserAction.where('user_id = :user_id OR target_user_id = :user_id OR acting_user_id = :user_id', user_id: user.id).delete_all
|
|
|
|
PostTiming.where(user_id: user.id).delete_all
|
|
|
|
TopicViewItem.where(user_id: user.id).delete_all
|
|
|
|
TopicUser.where(user_id: user.id).delete_all
|
|
|
|
TopicAllowedUser.where(user_id: user.id).delete_all
|
|
|
|
Notification.where(user_id: user.id).delete_all
|
|
|
|
end
|
|
|
|
|
2019-01-16 15:54:21 -05:00
|
|
|
def optional_transaction(open_transaction: true)
|
|
|
|
if open_transaction
|
|
|
|
User.transaction { yield }
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-05 14:35:28 -05:00
|
|
|
end
|