2013-04-11 16:04:20 -04:00
|
|
|
require_dependency 'admin_logger'
|
|
|
|
|
|
|
|
# Responsible for destroying a User record
|
|
|
|
class UserDestroyer
|
|
|
|
|
|
|
|
class PostsExistError < RuntimeError; end
|
|
|
|
|
|
|
|
def initialize(admin)
|
|
|
|
@admin = admin
|
|
|
|
raise Discourse::InvalidParameters.new('admin is nil') unless @admin and @admin.is_a?(User)
|
|
|
|
raise Discourse::InvalidAccess unless @admin.admin?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns false if the user failed to be deleted.
|
|
|
|
# Returns a frozen instance of the User if the delete succeeded.
|
|
|
|
def destroy(user)
|
|
|
|
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
|
|
|
|
raise PostsExistError if user.post_count != 0
|
2013-04-15 11:39:30 -04:00
|
|
|
User.transaction do
|
|
|
|
user.destroy.tap do |u|
|
|
|
|
if u
|
2013-07-10 16:52:34 -04:00
|
|
|
Post.with_deleted.where(user_id: user.id).update_all("nuked_user = true")
|
2013-04-15 11:39:30 -04:00
|
|
|
AdminLogger.new(@admin).log_user_deletion(user)
|
2013-04-29 11:38:43 -04:00
|
|
|
DiscourseHub.unregister_nickname(user.username) if SiteSetting.call_discourse_hub?
|
2013-04-15 11:39:30 -04:00
|
|
|
MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id]
|
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|