2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-31 07:02:26 -05:00
|
|
|
class Admin::ScreenedIpAddressesController < Admin::StaffController
|
2023-01-09 06:20:10 -06:00
|
|
|
before_action :fetch_screened_ip_address, only: %i[update destroy]
|
2013-10-22 15:30:30 -05:00
|
|
|
|
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
|
2023-01-09 06:20:10 -06:00
|
|
|
screened_ip_addresses =
|
|
|
|
screened_ip_addresses.where(
|
|
|
|
"cidr :filter >>= ip_address OR ip_address >>= cidr :filter",
|
|
|
|
filter: filter,
|
|
|
|
) if filter.present?
|
|
|
|
screened_ip_addresses = screened_ip_addresses.limit(200).order("match_count desc")
|
2015-02-10 12:38:59 -06:00
|
|
|
|
|
|
|
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
|
2019-04-29 02:32:25 -05:00
|
|
|
if @screened_ip_address.update(allowed_params)
|
2018-08-20 10:37:30 -05:00
|
|
|
render_serialized(@screened_ip_address, ScreenedIpAddressSerializer)
|
2013-10-22 15:30:30 -05:00
|
|
|
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
|
|
|
|
|
2013-10-22 15:30:30 -05:00
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def allowed_params
|
|
|
|
params.require(:ip_address)
|
|
|
|
params.permit(:ip_address, :action_name)
|
|
|
|
end
|
2013-10-22 15:30:30 -05:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def fetch_screened_ip_address
|
|
|
|
@screened_ip_address = ScreenedIpAddress.find(params[:id])
|
|
|
|
end
|
2013-10-21 13:49:51 -05:00
|
|
|
end
|