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.

59 lines
1.9 KiB

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