domain_blocks_controller.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: true
  2. module Admin
  3. class DomainBlocksController < BaseController
  4. before_action :set_domain_block, only: [:show, :destroy, :edit, :update]
  5. def new
  6. authorize :domain_block, :create?
  7. @domain_block = DomainBlock.new(domain: params[:_domain])
  8. end
  9. def edit
  10. authorize :domain_block, :create?
  11. end
  12. def create
  13. authorize :domain_block, :create?
  14. @domain_block = DomainBlock.new(resource_params)
  15. existing_domain_block = resource_params[:domain].present? ? DomainBlock.rule_for(resource_params[:domain]) : nil
  16. if existing_domain_block.present? && !@domain_block.stricter_than?(existing_domain_block)
  17. @domain_block.save
  18. 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 # rubocop:disable Rails/OutputSafety
  19. @domain_block.errors.delete(:domain)
  20. render :new
  21. else
  22. if existing_domain_block.present?
  23. @domain_block = existing_domain_block
  24. @domain_block.update(resource_params)
  25. end
  26. if @domain_block.save
  27. DomainBlockWorker.perform_async(@domain_block.id)
  28. log_action :create, @domain_block
  29. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  30. else
  31. render :new
  32. end
  33. end
  34. end
  35. def update
  36. authorize :domain_block, :update?
  37. if @domain_block.update(update_params)
  38. DomainBlockWorker.perform_async(@domain_block.id, @domain_block.severity_previously_changed?)
  39. log_action :update, @domain_block
  40. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  41. else
  42. render :edit
  43. end
  44. end
  45. def destroy
  46. authorize @domain_block, :destroy?
  47. UnblockDomainService.new.call(@domain_block)
  48. log_action :destroy, @domain_block
  49. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.destroyed_msg')
  50. end
  51. private
  52. def set_domain_block
  53. @domain_block = DomainBlock.find(params[:id])
  54. end
  55. def update_params
  56. params.require(:domain_block).permit(:severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  57. end
  58. def resource_params
  59. params.require(:domain_block).permit(:domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment, :obfuscate)
  60. end
  61. end
  62. end