Sha256: b7f3835e53377a43f0b94c7cc6516e8ba3995d0a5d70ae39c6c1904ed3bd4025

Contents?: true

Size: 961 Bytes

Versions: 2

Compression:

Stored size: 961 Bytes

Contents

# Inspired by: https://www.backerkit.com/blog/building-a-rackattack-dashboard/

module RackAttackAdmin
  class BannedIpsController < KeysController
    def create
      ban = Rack::Attack::BannedIp.new(
        params.require(Rack::Attack::BannedIp.model_name.param_key).
          permit(:ip, :bantime)
      )
      case ban.bantime
      when /m$/
        ban.bantime = ban.bantime.to_i * ActiveSupport::Duration::SECONDS_PER_MINUTE
      when /h$/
        ban.bantime = ban.bantime.to_i * ActiveSupport::Duration::SECONDS_PER_HOUR
      when /d$/
        ban.bantime = ban.bantime.to_i * ActiveSupport::Duration::SECONDS_PER_DAY
      else
        ban.bantime = ban.bantime.to_i
      end
      if ban.valid?
        Rack::Attack::BannedIps.ban! ban.ip, ban.bantime
        flash[:success] = "Added #{ban.ip}"
      else
        flash[:alert] = "Failed to add: #{ban.errors.full_messages.join('. ')}"
      end
      redirect_to root_path
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack_attack_admin-0.1.2 app/controllers/rack_attack_admin/banned_ips_controller.rb
rack_attack_admin-0.1.1 app/controllers/rack_attack_admin/banned_ips_controller.rb