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