2015-02-10 12:38:59 -06:00
|
|
|
require_dependency 'ip_addr'
|
|
|
|
|
2013-10-21 13:49:51 -05:00
|
|
|
class Admin::ScreenedIpAddressesController < Admin::AdminController
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
before_filter :fetch_screened_ip_address, only: [:update, :destroy]
|
|
|
|
|
2013-10-21 13:49:51 -05:00
|
|
|
def index
|
2015-02-10 12:38:59 -06:00
|
|
|
filter = params[:filter]
|
|
|
|
filter = IPAddr.handle_wildcards(filter)
|
|
|
|
|
|
|
|
screened_ip_addresses = ScreenedIpAddress
|
2017-02-06 15:10:48 -06:00
|
|
|
screened_ip_addresses = screened_ip_addresses.where("cidr :filter >>= ip_address", filter: filter) if filter.present?
|
2015-02-10 12:38:59 -06:00
|
|
|
screened_ip_addresses = screened_ip_addresses.limit(200).order('match_count desc')
|
|
|
|
|
|
|
|
begin
|
|
|
|
screened_ip_addresses = screened_ip_addresses.to_a
|
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
# postgresql throws a PG::InvalidTextRepresentation exception when filter isn't a valid cidr expression
|
|
|
|
screened_ip_addresses = []
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
render_serialized(screened_ip_addresses, ScreenedIpAddressSerializer)
|
|
|
|
end
|
|
|
|
|
2013-10-24 16:18:10 -05:00
|
|
|
def create
|
|
|
|
screened_ip_address = ScreenedIpAddress.new(allowed_params)
|
|
|
|
if screened_ip_address.save
|
|
|
|
render_serialized(screened_ip_address, ScreenedIpAddressSerializer)
|
|
|
|
else
|
|
|
|
render_json_error(screened_ip_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
def update
|
|
|
|
if @screened_ip_address.update_attributes(allowed_params)
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
render_json_error(@screened_ip_address)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@screened_ip_address.destroy
|
|
|
|
render json: success_json
|
2013-10-21 13:49:51 -05:00
|
|
|
end
|
|
|
|
|
2014-11-27 12:29:30 -06:00
|
|
|
def roll_up
|
2015-03-09 12:55:17 -05:00
|
|
|
subnets = ScreenedIpAddress.roll_up(current_user)
|
2017-07-27 20:20:09 -05:00
|
|
|
render json: success_json.merge!(subnets: subnets)
|
2014-11-24 10:25:48 -06:00
|
|
|
end
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def allowed_params
|
|
|
|
params.require(:ip_address)
|
|
|
|
params.permit(:ip_address, :action_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_screened_ip_address
|
|
|
|
@screened_ip_address = ScreenedIpAddress.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2013-10-21 13:49:51 -05:00
|
|
|
end
|