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.

69 lines
2.2 KiB

  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. end
  9. def new
  10. authorize :email_domain_block, :create?
  11. @email_domain_block = EmailDomainBlock.new(domain: params[:_domain])
  12. end
  13. def create
  14. authorize :email_domain_block, :create?
  15. @email_domain_block = EmailDomainBlock.new(resource_params)
  16. if @email_domain_block.save
  17. log_action :create, @email_domain_block
  18. if @email_domain_block.with_dns_records?
  19. hostnames = []
  20. ips = []
  21. Resolv::DNS.open do |dns|
  22. dns.timeouts = 5
  23. hostnames = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }
  24. ([@email_domain_block.domain] + hostnames).uniq.each do |hostname|
  25. ips.concat(dns.getresources(hostname, Resolv::DNS::Resource::IN::A).to_a.map { |e| e.address.to_s })
  26. ips.concat(dns.getresources(hostname, Resolv::DNS::Resource::IN::AAAA).to_a.map { |e| e.address.to_s })
  27. end
  28. end
  29. (hostnames + ips).each do |hostname|
  30. another_email_domain_block = EmailDomainBlock.new(domain: hostname, parent: @email_domain_block)
  31. log_action :create, another_email_domain_block if another_email_domain_block.save
  32. end
  33. end
  34. redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.created_msg')
  35. else
  36. render :new
  37. end
  38. end
  39. def destroy
  40. authorize @email_domain_block, :destroy?
  41. @email_domain_block.destroy!
  42. log_action :destroy, @email_domain_block
  43. redirect_to admin_email_domain_blocks_path, notice: I18n.t('admin.email_domain_blocks.destroyed_msg')
  44. end
  45. private
  46. def set_email_domain_block
  47. @email_domain_block = EmailDomainBlock.find(params[:id])
  48. end
  49. def resource_params
  50. params.require(:email_domain_block).permit(:domain, :with_dns_records)
  51. end
  52. end
  53. end