domain_blocks_controller.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. module Admin
  3. class DomainBlocksController < BaseController
  4. before_action :set_domain_block, only: [:destroy, :edit, :update]
  5. def batch
  6. authorize :domain_block, :create?
  7. @form = Form::DomainBlockBatch.new(form_domain_block_batch_params.merge(current_account: current_account, action: action_from_button))
  8. @form.save
  9. rescue ActionController::ParameterMissing
  10. flash[:alert] = I18n.t('admin.domain_blocks.no_domain_block_selected')
  11. rescue Mastodon::NotPermittedError
  12. flash[:alert] = I18n.t('admin.domain_blocks.not_permitted')
  13. else
  14. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  15. end
  16. def new
  17. authorize :domain_block, :create?
  18. @domain_block = DomainBlock.new(domain: params[:_domain])
  19. end
  20. def edit
  21. authorize :domain_block, :create?
  22. end
  23. def create
  24. authorize :domain_block, :create?
  25. @domain_block = DomainBlock.new(resource_params)
  26. existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
  27. # Disallow accidentally downgrading a domain block
  28. if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
  29. @domain_block.validate
  30. 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
  31. @domain_block.errors.delete(:domain)
  32. return render :new
  33. end
  34. # Allow transparently upgrading a domain block
  35. if existing_domain_block.present? && existing_domain_block.domain == TagManager.instance.normalize_domain(@domain_block.domain.strip)
  36. @domain_block = existing_domain_block
  37. @domain_block.assign_attributes(resource_params)
  38. end
  39. # Require explicit confirmation when suspending
  40. return render :confirm_suspension if requires_confirmation?
  41. if @domain_block.save
  42. DomainBlockWorker.perform_async(@domain_block.id)
  43. log_action :create, @domain_block
  44. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  45. else
  46. render :new
  47. end
  48. end
  49. def update
  50. authorize :domain_block, :update?
  51. @domain_block.assign_attributes(update_params)
  52. # Require explicit confirmation when suspending
  53. return render :confirm_suspension if requires_confirmation?
  54. if @domain_block.save
  55. DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?)
  56. log_action :update, @domain_block
  57. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  58. else
  59. render :edit
  60. end
  61. end
  62. def destroy
  63. authorize @domain_block, :destroy?
  64. UnblockDomainService.new.call(@domain_block)
  65. log_action :destroy, @domain_block
  66. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.destroyed_msg')
  67. end
  68. private
  69. def set_domain_block
  70. @domain_block = DomainBlock.find(params[:id])
  71. end
  72. def update_params
  73. params.require(:domain_block).permit(:severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  74. end
  75. def resource_params
  76. params.require(:domain_block).permit(:domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  77. end
  78. def form_domain_block_batch_params
  79. params.require(:form_domain_block_batch).permit(domain_blocks_attributes: [:enabled, :domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate])
  80. end
  81. def action_from_button
  82. 'save' if params[:save]
  83. end
  84. def requires_confirmation?
  85. @domain_block.valid? && (@domain_block.new_record? || @domain_block.severity_changed?) && @domain_block.severity.to_s == 'suspend' && !params[:confirm]
  86. end
  87. end
  88. end