2016-12-13 23:42:10 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
module Admin
|
|
|
|
class DomainBlocksController < BaseController
|
2023-03-21 06:03:44 +11:00
|
|
|
before_action :set_domain_block, only: [:destroy, :edit, :update]
|
2017-05-07 01:03:34 +10:00
|
|
|
|
2024-06-11 19:40:47 +10:00
|
|
|
PERMITTED_PARAMS = %i(
|
|
|
|
domain
|
|
|
|
obfuscate
|
|
|
|
private_comment
|
|
|
|
public_comment
|
|
|
|
reject_media
|
|
|
|
reject_reports
|
|
|
|
severity
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
PERMITTED_UPDATE_PARAMS = PERMITTED_PARAMS.without(:domain).freeze
|
|
|
|
|
2022-11-17 21:05:09 +11:00
|
|
|
def batch
|
|
|
|
authorize :domain_block, :create?
|
|
|
|
@form = Form::DomainBlockBatch.new(form_domain_block_batch_params.merge(current_account: current_account, action: action_from_button))
|
|
|
|
@form.save
|
|
|
|
rescue ActionController::ParameterMissing
|
|
|
|
flash[:alert] = I18n.t('admin.domain_blocks.no_domain_block_selected')
|
|
|
|
rescue Mastodon::NotPermittedError
|
|
|
|
flash[:alert] = I18n.t('admin.domain_blocks.not_permitted')
|
|
|
|
else
|
|
|
|
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
|
|
|
|
end
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
def new
|
2017-11-12 06:23:33 +11:00
|
|
|
authorize :domain_block, :create?
|
2019-01-08 23:39:49 +11:00
|
|
|
@domain_block = DomainBlock.new(domain: params[:_domain])
|
2017-04-11 05:27:03 +10:00
|
|
|
end
|
2017-04-04 02:55:06 +10:00
|
|
|
|
2019-08-08 04:20:23 +10:00
|
|
|
def edit
|
|
|
|
authorize :domain_block, :create?
|
|
|
|
end
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
def create
|
2017-11-12 06:23:33 +11:00
|
|
|
authorize :domain_block, :create?
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
@domain_block = DomainBlock.new(resource_params)
|
2019-06-22 08:13:10 +10:00
|
|
|
existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
|
2017-04-04 02:55:06 +10:00
|
|
|
|
2023-06-01 17:37:38 +10:00
|
|
|
# Disallow accidentally downgrading a domain block
|
2019-05-04 04:36:36 +10:00
|
|
|
if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
|
2023-10-31 20:40:30 +11:00
|
|
|
@domain_block.validate
|
2023-05-04 19:56:24 +10:00
|
|
|
flash.now[:alert] = I18n.t('admin.domain_blocks.existing_domain_block_html', name: existing_domain_block.domain, unblock_url: admin_domain_block_path(existing_domain_block)).html_safe
|
2021-03-19 12:45:34 +11:00
|
|
|
@domain_block.errors.delete(:domain)
|
2023-06-01 17:37:38 +10:00
|
|
|
return render :new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Allow transparently upgrading a domain block
|
2023-08-09 17:39:36 +10:00
|
|
|
if existing_domain_block.present? && existing_domain_block.domain == TagManager.instance.normalize_domain(@domain_block.domain.strip)
|
2023-06-01 17:37:38 +10:00
|
|
|
@domain_block = existing_domain_block
|
|
|
|
@domain_block.assign_attributes(resource_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Require explicit confirmation when suspending
|
|
|
|
return render :confirm_suspension if requires_confirmation?
|
|
|
|
|
|
|
|
if @domain_block.save
|
|
|
|
DomainBlockWorker.perform_async(@domain_block.id)
|
|
|
|
log_action :create, @domain_block
|
|
|
|
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
|
2019-05-04 04:36:36 +10:00
|
|
|
else
|
2023-06-01 17:37:38 +10:00
|
|
|
render :new
|
2017-04-11 05:27:03 +10:00
|
|
|
end
|
2017-04-04 02:55:06 +10:00
|
|
|
end
|
|
|
|
|
2019-08-08 04:20:23 +10:00
|
|
|
def update
|
2020-12-14 19:06:34 +11:00
|
|
|
authorize :domain_block, :update?
|
2019-08-08 04:20:23 +10:00
|
|
|
|
2023-06-01 17:37:38 +10:00
|
|
|
@domain_block.assign_attributes(update_params)
|
|
|
|
|
|
|
|
# Require explicit confirmation when suspending
|
|
|
|
return render :confirm_suspension if requires_confirmation?
|
|
|
|
|
|
|
|
if @domain_block.save
|
2022-12-16 03:45:02 +11:00
|
|
|
DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?)
|
2020-12-14 19:06:34 +11:00
|
|
|
log_action :update, @domain_block
|
2019-08-08 04:20:23 +10:00
|
|
|
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
|
|
|
|
else
|
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-16 20:51:30 +10:00
|
|
|
def destroy
|
2017-11-12 06:23:33 +11:00
|
|
|
authorize @domain_block, :destroy?
|
2019-05-15 03:05:02 +10:00
|
|
|
UnblockDomainService.new.call(@domain_block)
|
2017-11-24 12:05:53 +11:00
|
|
|
log_action :destroy, @domain_block
|
2019-01-08 23:39:49 +11:00
|
|
|
redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.destroyed_msg')
|
2017-04-16 20:51:30 +10:00
|
|
|
end
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
private
|
2017-04-04 02:55:06 +10:00
|
|
|
|
2017-05-07 01:03:34 +10:00
|
|
|
def set_domain_block
|
|
|
|
@domain_block = DomainBlock.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2019-08-08 04:20:23 +10:00
|
|
|
def update_params
|
2024-06-11 19:40:47 +10:00
|
|
|
params
|
|
|
|
.require(:domain_block)
|
|
|
|
.slice(*PERMITTED_UPDATE_PARAMS)
|
|
|
|
.permit(*PERMITTED_UPDATE_PARAMS)
|
2019-08-08 04:20:23 +10:00
|
|
|
end
|
|
|
|
|
2017-04-11 05:27:03 +10:00
|
|
|
def resource_params
|
2024-06-11 19:40:47 +10:00
|
|
|
params
|
|
|
|
.require(:domain_block)
|
|
|
|
.slice(*PERMITTED_PARAMS)
|
|
|
|
.permit(*PERMITTED_PARAMS)
|
2017-04-30 08:25:38 +10:00
|
|
|
end
|
2022-11-17 21:05:09 +11:00
|
|
|
|
|
|
|
def form_domain_block_batch_params
|
|
|
|
params.require(:form_domain_block_batch).permit(domain_blocks_attributes: [:enabled, :domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate])
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_from_button
|
2023-02-18 22:37:47 +11:00
|
|
|
'save' if params[:save]
|
2022-11-17 21:05:09 +11:00
|
|
|
end
|
2023-06-01 17:37:38 +10:00
|
|
|
|
|
|
|
def requires_confirmation?
|
|
|
|
@domain_block.valid? && (@domain_block.new_record? || @domain_block.severity_changed?) && @domain_block.severity.to_s == 'suspend' && !params[:confirm]
|
|
|
|
end
|
2016-12-13 23:42:10 +11:00
|
|
|
end
|
|
|
|
end
|