Sha256: 18fc9bd9330f3bbdcced1cd7913cfa0b85d1972f5860794749840c77b65ec517

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 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
        redirect_to root_path, success: "Added: #{ban.ip}"
      else
        redirect_to root_path, alert: "Failed to add: #{ban.errors.full_messages}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack_attack_admin-0.1.0 app/controllers/rack_attack_admin/banned_ips_controller.rb