You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.5 KiB

Support for import/export of instance-level domain blocks/allows for 4.x w/ additional fixes (#20597) * Allow import/export of instance-level domain blocks/allows (#1754) * Allow import/export of instance-level domain blocks/allows. Fixes #15095 * Pacify circleci * Address simple code review feedback * Add headers to exported CSV * Extract common import/export functionality to AdminExportControllerConcern * Add additional fields to instance-blocked domain export * Address review feedback * Split instance domain block/allow import/export into separate pages/controllers * Address code review feedback * Pacify DeepSource * Work around Paperclip::HasAttachmentFile for Rails 6 * Fix deprecated API warning in export tests * Remove after_commit workaround (cherry picked from commit 94e98864e39c010635e839fea984f2b4893bef1a) * Add confirmation page when importing blocked domains (#1773) * Move glitch-soc-specific strings to glitch-soc-specific locale files * Add confirmation page when importing blocked domains (cherry picked from commit b91196f4b73fff91997b8077619ae25b6d04a59e) * Fix authorization check in domain blocks controller (cherry picked from commit 75279377583c6e2aa04cc8d7380c593979630b38) * Fix error strings for domain blocks and email-domain blocks Corrected issue with non-error message used for Mastodon:NotPermittedError in Domain Blocks Corrected issue Domain Blocks using the Email Domain Blocks message on ActionContoller::ParameterMissing Corrected issue with Email Domain Blocks using the not_permitted string from "custom emojii's" * Ran i18n-tasks normalize to address test failure * Removed unused admin.export_domain_blocks.not_permitted string Removing unused string as indicated by Check i18n * Fix tests (cherry picked from commit 9094c2f52c24e1c00b594e7c11cd00e4a07eb431) * Fix domain block export not exporting blocks with only media rejection (cherry picked from commit 26ff48ee48a5c03a2a4b0bd03fd322529e6bd960) * Fix various issues with domain block import - stop using Paperclip for processing domain allow/block imports - stop leaving temporary files - better error handling - assume CSV files are UTF-8-encoded (cherry picked from commit cad824d8f501b95377e4f0a957e5a00d517a1902) Co-authored-by: Levi Bard <taktaktaktaktaktaktaktaktaktak@gmail.com> Co-authored-by: Claire <claire.github-309c@sitedethib.com>
1 year ago
  1. # frozen_string_literal: true
  2. module Admin
  3. class EmailDomainBlocksController < BaseController
  4. def index
  5. authorize :email_domain_block, :index?
  6. @email_domain_blocks = EmailDomainBlock.where(parent_id: nil).includes(:children).order(id: :desc).page(params[:page])
  7. @form = Form::EmailDomainBlockBatch.new
  8. end
  9. def batch
  10. authorize :email_domain_block, :index?
  11. @form = Form::EmailDomainBlockBatch.new(form_email_domain_block_batch_params.merge(current_account: current_account, action: action_from_button))
  12. @form.save
  13. rescue ActionController::ParameterMissing
  14. flash[:alert] = I18n.t('admin.email_domain_blocks.no_email_domain_block_selected')
  15. rescue Mastodon::NotPermittedError
  16. flash[:alert] = I18n.t('admin.email_domain_blocks.not_permitted')
  17. ensure
  18. redirect_to admin_email_domain_blocks_path
  19. end
  20. def new
  21. authorize :email_domain_block, :create?
  22. @email_domain_block = EmailDomainBlock.new(domain: params[:_domain])
  23. end
  24. def create
  25. authorize :email_domain_block, :create?
  26. @email_domain_block = EmailDomainBlock.new(resource_params)
  27. if action_from_button == 'save'
  28. EmailDomainBlock.transaction do
  29. @email_domain_block.save!
  30. log_action :create, @email_domain_block
  31. (@email_domain_block.other_domains || []).uniq.each do |domain|
  32. next if EmailDomainBlock.where(domain: domain).exists?
  33. other_email_domain_block = EmailDomainBlock.create!(domain: domain, parent: @email_domain_block)
  34. log_action :create, other_email_domain_block
  35. end
  36. end
  37. redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg')
  38. else
  39. set_resolved_records
  40. render :new
  41. end
  42. rescue ActiveRecord::RecordInvalid
  43. set_resolved_records
  44. render :new
  45. end
  46. private
  47. def set_resolved_records
  48. Resolv::DNS.open do |dns|
  49. dns.timeouts = 5
  50. @resolved_records = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a
  51. end
  52. end
  53. def resource_params
  54. params.require(:email_domain_block).permit(:domain, other_domains: [])
  55. end
  56. def form_email_domain_block_batch_params
  57. params.require(:form_email_domain_block_batch).permit(email_domain_block_ids: [])
  58. end
  59. def action_from_button
  60. if params[:delete]
  61. 'delete'
  62. elsif params[:save]
  63. 'save'
  64. end
  65. end
  66. end
  67. end