Class: RackAttackAdmin::BannedIpsController

Inherits:
KeysController show all
Defined in:
app/controllers/rack_attack_admin/banned_ips_controller.rb

Instance Method Summary collapse

Methods inherited from KeysController

#destroy

Methods inherited from ApplicationController

#toggle_flags

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/rack_attack_admin/banned_ips_controller.rb', line 5

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