ip_blocks_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. module Admin
  3. class IpBlocksController < BaseController
  4. def index
  5. authorize :ip_block, :index?
  6. @ip_blocks = IpBlock.order(ip: :asc).page(params[:page])
  7. @form = Form::IpBlockBatch.new
  8. end
  9. def new
  10. authorize :ip_block, :create?
  11. @ip_block = IpBlock.new(ip: '', severity: :no_access, expires_in: 1.year)
  12. end
  13. def create
  14. authorize :ip_block, :create?
  15. @ip_block = IpBlock.new(resource_params)
  16. if @ip_block.save
  17. log_action :create, @ip_block
  18. redirect_to admin_ip_blocks_path, notice: I18n.t('admin.ip_blocks.created_msg')
  19. else
  20. render :new
  21. end
  22. end
  23. def batch
  24. authorize :ip_block, :index?
  25. @form = Form::IpBlockBatch.new(form_ip_block_batch_params.merge(current_account: current_account, action: action_from_button))
  26. @form.save
  27. rescue ActionController::ParameterMissing
  28. flash[:alert] = I18n.t('admin.ip_blocks.no_ip_block_selected')
  29. rescue Mastodon::NotPermittedError
  30. flash[:alert] = I18n.t('admin.custom_emojis.not_permitted')
  31. ensure
  32. redirect_to admin_ip_blocks_path
  33. end
  34. private
  35. def resource_params
  36. params.require(:ip_block).permit(:ip, :severity, :comment, :expires_in)
  37. end
  38. def action_from_button
  39. 'delete' if params[:delete]
  40. end
  41. def form_ip_block_batch_params
  42. params.require(:form_ip_block_batch).permit(ip_block_ids: [])
  43. end
  44. end
  45. end