email_domain_blocks_controller.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # frozen_string_literal: true
  2. module Admin
  3. class EmailDomainBlocksController < BaseController
  4. before_action :set_email_domain_block, only: [:show, :destroy]
  5. def index
  6. authorize :email_domain_block, :index?
  7. @email_domain_blocks = EmailDomainBlock.where(parent_id: nil).includes(:children).order(id: :desc).page(params[:page])
  8. @form = Form::EmailDomainBlockBatch.new
  9. end
  10. def batch
  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.custom_emojis.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_email_domain_block
  48. @email_domain_block = EmailDomainBlock.find(params[:id])
  49. end
  50. def set_resolved_records
  51. Resolv::DNS.open do |dns|
  52. dns.timeouts = 5
  53. @resolved_records = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a
  54. end
  55. end
  56. def resource_params
  57. params.require(:email_domain_block).permit(:domain, other_domains: [])
  58. end
  59. def form_email_domain_block_batch_params
  60. params.require(:form_email_domain_block_batch).permit(email_domain_block_ids: [])
  61. end
  62. def action_from_button
  63. if params[:delete]
  64. 'delete'
  65. elsif params[:save]
  66. 'save'
  67. end
  68. end
  69. end
  70. end