2013-10-21 14:49:51 -04:00
|
|
|
require_dependency 'screening_model'
|
|
|
|
|
|
|
|
# A ScreenedIpAddress record represents an IP address or subnet that is being watched,
|
|
|
|
# and possibly blocked from creating accounts.
|
|
|
|
class ScreenedIpAddress < ActiveRecord::Base
|
|
|
|
|
|
|
|
include ScreeningModel
|
|
|
|
|
|
|
|
default_action :block
|
|
|
|
|
2013-10-22 16:30:30 -04:00
|
|
|
validates :ip_address, ip_address_format: true, presence: true
|
2013-10-21 14:49:51 -04:00
|
|
|
|
|
|
|
def self.watch(ip_address, opts={})
|
|
|
|
match_for_ip_address(ip_address) || create(opts.slice(:action_type).merge(ip_address: ip_address))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.match_for_ip_address(ip_address)
|
|
|
|
# The <<= operator on inet columns means "is contained within or equal to".
|
|
|
|
#
|
|
|
|
# Read more about PostgreSQL's inet data type here:
|
|
|
|
#
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/datatype-net-types.html
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/functions-net.html
|
2013-10-22 18:55:30 -04:00
|
|
|
where("'#{ip_address.to_s}' <<= ip_address").first
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.should_block?(ip_address)
|
|
|
|
b = match_for_ip_address(ip_address)
|
|
|
|
b.record_match! if b
|
|
|
|
!!b and b.action_type == actions[:block]
|
|
|
|
end
|
|
|
|
end
|