Compare commits

...

11 Commits

Author SHA1 Message Date
Matt Jankowski
7eccd9b8bf
Merge ee8ff20e31 into e7c30cd072 2025-09-04 14:05:17 +00:00
Matt Jankowski
ee8ff20e31 Pull out method for downgrade check 2025-08-15 08:53:54 -04:00
Matt Jankowski
66d282ea5e Wording improvement on transparent upgrade 2025-08-15 08:53:54 -04:00
Matt Jankowski
7c2847e12d Use shorthand 2025-08-15 08:53:54 -04:00
Matt Jankowski
bf24de759a Pull out method for alert i18n 2025-08-15 08:53:54 -04:00
Matt Jankowski
0dfa014fba Extract confirmation check to before action 2025-08-15 08:53:54 -04:00
Matt Jankowski
9326490325 Extract upgrade to before action 2025-08-15 08:53:54 -04:00
Matt Jankowski
36f6fb98c3 Extract downgrade check to before action 2025-08-15 08:53:54 -04:00
Matt Jankowski
db2930a46f Use before action to set up i-var in create action 2025-08-15 08:53:54 -04:00
Matt Jankowski
3b087708a7 Extract existing_domain_block private method 2025-08-15 08:53:53 -04:00
Matt Jankowski
523e7ee2cd Move authorize to before action 2025-08-15 08:53:53 -04:00

View File

@ -3,6 +3,13 @@
module Admin
class DomainBlocksController < BaseController
before_action :set_domain_block, only: [:destroy, :edit, :update]
before_action :authorize_domain_block_create, only: [:batch, :new, :create]
with_options only: :create do
before_action :populate_domain_block_from_params
before_action :prevent_downgrade, if: :attempting_downgrade?
before_action :perform_transparent_upgrade, if: :existing_domain_block_matches_domain?
before_action :verify_confirmation_needed
end
PERMITTED_PARAMS = %i(
domain
@ -17,7 +24,6 @@ module Admin
PERMITTED_UPDATE_PARAMS = PERMITTED_PARAMS.without(:domain).freeze
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
@ -31,7 +37,6 @@ module Admin
end
def new
authorize :domain_block, :create?
@domain_block = DomainBlock.new(domain: params[:_domain])
end
@ -40,28 +45,6 @@ module Admin
end
def create
authorize :domain_block, :create?
@domain_block = DomainBlock.new(resource_params)
existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
# Disallow accidentally downgrading a domain block
if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
@domain_block.validate
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
@domain_block.errors.delete(:domain)
return render :new
end
# Allow transparently upgrading a domain block
if existing_domain_block.present? && existing_domain_block.domain == TagManager.instance.normalize_domain(@domain_block.domain.strip)
@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
@ -97,10 +80,58 @@ module Admin
private
def authorize_domain_block_create
authorize :domain_block, :create?
end
def populate_domain_block_from_params
@domain_block = DomainBlock.new(resource_params)
end
def prevent_downgrade
@domain_block.validate
flash.now.alert = unblock_first_alert
@domain_block.errors.delete(:domain)
render :new
end
def attempting_downgrade?
# Existing domain block exists and the new value is less strict than the existing
existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
end
def perform_transparent_upgrade
@domain_block = existing_domain_block
@domain_block.assign_attributes(resource_params)
end
def existing_domain_block_matches_domain?
# Existing domain block exists and the domain is exact match for the value from incoming params
existing_domain_block.present? &&
existing_domain_block.domain == TagManager.instance.normalize_domain(@domain_block.domain.strip)
end
def verify_confirmation_needed
# Require explicit confirmation when suspending
render :confirm_suspension if requires_confirmation?
end
def existing_domain_block
@existing_domain_block ||= DomainBlock.rule_for(resource_params[:domain]) if resource_params[:domain].present?
end
def set_domain_block
@domain_block = DomainBlock.find(params[:id])
end
def unblock_first_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
end
def update_params
params
.require(:domain_block)