domain_blocks_controller.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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[:domain].clear
  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, :create?
  37. @domain_block.update(update_params)
  38. severity_changed = @domain_block.severity_changed?
  39. if @domain_block.save
  40. DomainBlockWorker.perform_async(@domain_block.id, severity_changed)
  41. log_action :create, @domain_block
  42. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.created_msg')
  43. else
  44. render :edit
  45. end
  46. end
  47. def show
  48. authorize @domain_block, :show?
  49. end
  50. def destroy
  51. authorize @domain_block, :destroy?
  52. UnblockDomainService.new.call(@domain_block)
  53. log_action :destroy, @domain_block
  54. redirect_to admin_instances_path(limited: '1'), notice: I18n.t('admin.domain_blocks.destroyed_msg')
  55. end
  56. private
  57. def set_domain_block
  58. @domain_block = DomainBlock.find(params[:id])
  59. end
  60. def update_params
  61. params.require(:domain_block).permit(:severity, :reject_media, :reject_reports, :private_comment, :public_comment)
  62. end
  63. def resource_params
  64. params.require(:domain_block).permit(:domain, :severity, :reject_media, :reject_reports, :private_comment, :public_comment)
  65. end
  66. end
  67. end